Changed behaviour of template selection form
authorThierry Florac <thierry.florac@onf.fr>
Wed, 28 Jun 2017 16:44:18 +0200
changeset 35 8b2e5695535a
parent 34 0a7a5155d352
child 36 c41baac58e21
Changed behaviour of template selection form
src/pyams_portal/interfaces/__init__.py
src/pyams_portal/page.py
src/pyams_portal/zmi/page.py
--- a/src/pyams_portal/interfaces/__init__.py	Wed Jun 28 11:45:31 2017 +0200
+++ b/src/pyams_portal/interfaces/__init__.py	Wed Jun 28 16:44:18 2017 +0200
@@ -401,6 +401,11 @@
                               required=True,
                               default=False)
 
+    use_shared_template = Bool(title=_("Use shared template?"),
+                               description=_("If 'yes', you can select a shared template"),
+                               required=True,
+                               default=True)
+
     shared_template = Choice(title=_("Page template"),
                              description=_("Template used for this page"),
                              vocabulary='PyAMS portal templates',
--- a/src/pyams_portal/page.py	Wed Jun 28 11:45:31 2017 +0200
+++ b/src/pyams_portal/page.py	Wed Jun 28 16:44:18 2017 +0200
@@ -87,6 +87,14 @@
             noLongerProvides(self, ILocalTemplateHandler)
 
     @property
+    def use_shared_template(self):
+        return not self.use_local_template
+
+    @use_shared_template.setter
+    def use_shared_template(self, value):
+        self.use_local_template = not value
+
+    @property
     def shared_template(self):
         return IPortalPage(self.parent).shared_template if self.inherit_parent else self._shared_template
 
--- a/src/pyams_portal/zmi/page.py	Wed Jun 28 11:45:31 2017 +0200
+++ b/src/pyams_portal/zmi/page.py	Wed Jun 28 16:44:18 2017 +0200
@@ -16,10 +16,10 @@
 # import standard library
 
 # import interfaces
-from pyams_form.interfaces.form import IWidgetForm
+from pyams_form.interfaces.form import IWidgetForm, IFormHelp
 from pyams_portal.interfaces import IPortalContext, IPortalPage, IPortalTemplateConfiguration
 from pyams_portal.zmi.interfaces import IPortalContextTemplatePropertiesMenu
-from pyams_skin.interfaces import IPageHeader, IInnerPage
+from pyams_skin.interfaces import IPageHeader, IInnerPage, IContentHelp
 from pyams_skin.layer import IPyAMSLayer
 from pyams_utils.interfaces import MANAGE_PERMISSION, VIEW_SYSTEM_PERMISSION
 from pyams_zmi.interfaces.menu import ISiteManagementMenu
@@ -28,6 +28,8 @@
 # import packages
 from pyramid.view import view_config
 from pyams_form.form import AJAXEditForm
+from pyams_form.group import NamedWidgetsGroup
+from pyams_form.help import FormHelp
 from pyams_pagelet.pagelet import pagelet_config
 from pyams_portal.zmi.layout import PortalTemplateLayoutView, PortalTemplatePortletEditForm, \
     PortalTemplatePortletAJAXEditForm
@@ -40,6 +42,7 @@
 from pyams_zmi.form import AdminEditForm
 from pyams_zmi.site import PropertiesEditFormHeaderAdapter
 from z3c.form import field
+from z3c.form.browser.checkbox import SingleCheckBoxFieldWidget
 from zope.interface import implementer, Interface
 
 from pyams_portal import _
@@ -76,9 +79,10 @@
 
     @property
     def fields(self):
-        fields = field.Fields(IPortalPage).select('inherit_parent', 'use_local_template', 'shared_template')
+        fields = field.Fields(IPortalPage).select('inherit_parent', 'use_shared_template', 'shared_template')
         if not self.getContent().can_inherit:
             fields = fields.omit('inherit_parent')
+        fields['use_shared_template'].widgetFactory = SingleCheckBoxFieldWidget
         return fields
 
     ajax_handler = 'template-properties.json'
@@ -87,6 +91,16 @@
     def getContent(self):
         return IPortalPage(self.context)
 
+    def update(self):
+        super(PortalContextTemplatePropertiesEditForm, self).update()
+        self.add_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']))
+
 
 @view_config(name='template-properties.json', context=IPortalContext, request_type=IPyAMSLayer,
              permission=MANAGE_PERMISSION, renderer='json', xhr=True)
@@ -94,7 +108,7 @@
     """Portal context template properties edit form, JSON renderer"""
 
     def get_ajax_output(self, changes):
-        if 'use_local_template' in changes.get(IPortalPage, ()):
+        if 'use_shared_template' in changes.get(IPortalPage, ()):
             return {'status': 'redirect'}
         else:
             return super(PortalContextTemplatePropertiesAJAXEditForm, self).get_ajax_output(changes)
@@ -107,6 +121,17 @@
     icon_class = 'fa fa-fw fa-columns'
 
 
+@adapter_config(context=(IPortalContext, IAdminLayer, PortalContextTemplatePropertiesEditForm), provides=IFormHelp)
+class PortalContextPropertiesEditFormHelpAdapter(FormHelp):
+    """Portal context properties edit form help adapter"""
+
+    message = _("If you choose a shared template, you can only adjust settings of "
+                "each portlet individually but can't change portlets list or page configuration.\n"
+                "If you use a local template, you can define a whole custom "
+                "configuration but the template definition can't be reused anywhere...""")
+    message_format = 'text'
+
+
 #
 #
 #