src/pyams_portal/zmi/page.py
changeset 177 27cf196f066c
parent 129 481310ae09ad
child 178 a1cd70c6496f
--- a/src/pyams_portal/zmi/page.py	Tue Sep 04 14:48:00 2018 +0200
+++ b/src/pyams_portal/zmi/page.py	Tue Sep 04 17:31:38 2018 +0200
@@ -9,6 +9,7 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from collections import OrderedDict
 
 __docformat__ = 'restructuredtext'
 
@@ -16,7 +17,7 @@
 # import standard library
 
 # import interfaces
-from pyams_form.interfaces.form import IWidgetForm, IFormHelp, IInnerSubForm
+from pyams_form.interfaces.form import IWidgetForm, IFormHelp
 from pyams_portal.interfaces import IPortalContext, IPortalPage, IPortalTemplateConfiguration, \
     MANAGE_TEMPLATE_PERMISSION
 from pyams_portal.zmi.interfaces import IPortalContextTemplatePropertiesMenu
@@ -29,23 +30,22 @@
 
 # import packages
 from pyams_form.form import ajax_config
-from pyams_form.group import NamedWidgetsGroup, FormWidgetsGroup
 from pyams_form.help import FormHelp
 from pyams_pagelet.pagelet import pagelet_config
 from pyams_portal.zmi.layout import PortalTemplateLayoutView, PortalTemplatePortletEditForm, \
     PortalTemplatePortletAJAXEditForm
 from pyams_portal.zmi.template import PortalTemplateHeaderAdapter
 from pyams_skin.viewlet.menu import MenuItem
+from pyams_template.template import template_config
 from pyams_utils.adapter import adapter_config
 from pyams_utils.url import absolute_url
 from pyams_viewlet.manager import viewletmanager_config
 from pyams_viewlet.viewlet import viewlet_config
-from pyams_zmi.form import AdminEditForm, InnerAdminEditForm
+from pyams_zmi.form import AdminEditForm
 from pyams_zmi.zmi.site import PropertiesEditFormHeaderAdapter
 from pyramid.events import subscriber
 from pyramid.view import view_config
 from z3c.form import field
-from z3c.form.browser.checkbox import SingleCheckBoxFieldWidget
 from zope.interface import implementer, Interface, Invalid
 
 from pyams_portal import _
@@ -68,55 +68,14 @@
     url = '#template-properties.html'
 
 
-class InheritedTemplateWidgetsGroup(FormWidgetsGroup):
-    """Inherit template widgets groups"""
-
-
-@adapter_config(context=(IPortalContext, IPyAMSLayer, InheritedTemplateWidgetsGroup), provides=IInnerSubForm)
-class InheritedPortalContextTemplatePropertiesEditForm(InnerAdminEditForm):
-    """Inherited portal context template properties"""
-
-    def __new__(cls, context, request, group):
-        if not group.form.getContent().can_inherit:
-            return None
-        return InnerAdminEditForm.__new__(cls)
-
-    legend = None
-
-    fields = field.Fields(IPortalPage).select('use_shared_template', 'shared_template')
-    fields['use_shared_template'].widgetFactory = SingleCheckBoxFieldWidget
-
-    edit_permission = MANAGE_TEMPLATE_PERMISSION
-
-    def getContent(self):
-        return IPortalPage(self.context)
-
-    def updateGroups(self):
-        self.add_group(NamedWidgetsGroup(self, 'inherited_template_properties', self.widgets,
-                                         ('use_shared_template', 'shared_template',),
-                                         legend=_("Use shared template"),
-                                         css_class='inner',
-                                         switch=True,
-                                         checkbox_switch=True,
-                                         checkbox_field=IPortalPage['use_shared_template']))
-        super(InheritedPortalContextTemplatePropertiesEditForm, self).updateGroups()
-
-
-@subscriber(IDataExtractedEvent, form_selector=InheritedPortalContextTemplatePropertiesEditForm)
-def handle_inherited_template_proterties_extract_event(event):
-    """Handle template properties extract event"""
-    form = event.form
-    parent = form.parent_form  # parent group!
-    form_data, form_errors = parent.form.extractData()
-    if form_data.get('override_parent'):
-        data = event.data
-        if data.get('use_shared_template') and not data.get('shared_template'):
-            parent.form.widgets.errors += (Invalid(_("You must choose to use a local template or select a shared "
-                                                     "one!")),)
+TEMPLATE_INHERIT_MODE = 'inherit'
+TEMPLATE_SHARED_MODE = 'shared'
+TEMPLATE_LOCAL_MODE = 'local'
 
 
 @pagelet_config(name='template-properties.html', context=IPortalContext, layer=IPyAMSLayer,
                 permission=MANAGE_TEMPLATE_PERMISSION)
+@template_config(template='templates/template-properties.pt', layer=IPyAMSLayer)
 @ajax_config(name='template-properties.json', context=IPortalContext, layer=IPyAMSLayer)
 @implementer(IWidgetForm, IInnerPage)
 class PortalContextTemplatePropertiesEditForm(AdminEditForm):
@@ -127,44 +86,28 @@
         return self.context.title
 
     legend = _("Edit template configuration")
-    override_legend = _("Override parent template")
+    inherit_legend = _("Inherit parent template")
 
-    @property
-    def fields(self):
-        if self.getContent().can_inherit:
-            fields = field.Fields(IPortalPage).select('override_parent')
-            fields['override_parent'].widgetFactory = SingleCheckBoxFieldWidget
-        else:
-            fields = field.Fields(IPortalPage).select('use_shared_template', 'shared_template')
-            fields['use_shared_template'].widgetFactory = SingleCheckBoxFieldWidget
-        return fields
+    fields = field.Fields(IPortalPage).select('shared_template')
 
     edit_permission = MANAGE_TEMPLATE_PERMISSION
 
     def getContent(self):
         return IPortalPage(self.context)
 
-    def updateGroups(self):
-        if self.getContent().can_inherit:
-            group = NamedWidgetsGroup(self, 'template_properties', self.widgets,
-                                      ('override_parent',),
-                                      legend=self.override_legend,
-                                      css_class='inner',
-                                      switch=True,
-                                      checkbox_switch=True,
-                                      checkbox_mode='disable',
-                                      checkbox_field=IPortalPage['override_parent'],
-                                      factory=InheritedTemplateWidgetsGroup)
+    def update_content(self, content, data):
+        data = data.get(self, data)
+        template_mode = self.request.params.get('template_mode')
+        if template_mode == TEMPLATE_INHERIT_MODE:
+            content.inherit_parent = True
         else:
-            group = NamedWidgetsGroup(self, 'template_properties', self.widgets,
-                                      ('use_shared_template', 'shared_template',),
-                                      legend=_("Use shared template"),
-                                      css_class='inner',
-                                      switch=True,
-                                      checkbox_switch=True,
-                                      checkbox_field=IPortalPage['use_shared_template'])
-        self.add_group(group)
-        super(PortalContextTemplatePropertiesEditForm, self).updateGroups()
+            content.inherit_parent = False
+            if template_mode == TEMPLATE_SHARED_MODE:
+                content.shared_template = data.get('shared_template')
+                content.use_local_template = False
+            elif template_mode == TEMPLATE_LOCAL_MODE:
+                content.use_local_template = True
+        return {IPortalPage: ('inherit_parent', 'use_local_template', 'shared_template')}
 
     def get_ajax_output(self, changes):
         output = super(self.__class__, self).get_ajax_output(changes)
@@ -180,13 +123,13 @@
 
 
 @subscriber(IDataExtractedEvent, form_selector=PortalContextTemplatePropertiesEditForm)
-def handle_template_proterties_extract_event(event):
+def handle_template_properties_extract_event(event):
     """Handle template properties extract event"""
     form = event.form
     if not form.getContent().can_inherit:
         data = event.data
-        if data.get('use_shared_template') and not data.get('shared_template'):
-            form.widgets.errors += (Invalid(_("You must choose to use a local template or select a shared one!")),)
+        if (form.request.params.get('template_mode') == TEMPLATE_SHARED_MODE) and not data.get('shared_template'):
+            form.widgets.errors += (Invalid(_("You must select which shared template to use!")),)
 
 
 @adapter_config(context=(Interface, IPyAMSLayer, PortalContextTemplatePropertiesEditForm), provides=IPageHeader)
@@ -208,7 +151,7 @@
 
 
 #
-#
+# Portal context template configuration
 #
 
 @viewlet_config(name='template-config.menu', context=IPortalContext, layer=IAdminLayer,