Add settings to Google/UserReport extensions to disable extensions when cookies are rejected
authorThierry Florac <tflorac@ulthar.net>
Wed, 16 Jan 2019 12:06:25 +0100
changeset 106 21d0b2d4d27b
parent 105 d72c466be7d4
child 107 99eb0e812717
Add settings to Google/UserReport extensions to disable extensions when cookies are rejected
src/pyams_zmi/zmi/extension.py
--- a/src/pyams_zmi/zmi/extension.py	Fri Dec 14 18:34:45 2018 +0100
+++ b/src/pyams_zmi/zmi/extension.py	Wed Jan 16 12:06:25 2019 +0100
@@ -12,28 +12,23 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
+from z3c.form import field
+from z3c.form.browser.checkbox import SingleCheckBoxFieldWidget
 
-# import interfaces
-from pyams_skin.interfaces.extension import IGoogleTagManagerInfo, IGoogleAnalyticsInfo, IUserReportInfo
+from pyams_form.form import ajax_config
+from pyams_form.group import NamedWidgetsGroup
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_skin.interfaces.extension import IGoogleAnalyticsInfo, IGoogleTagManagerInfo, IUserReportInfo
 from pyams_skin.layer import IPyAMSLayer
+from pyams_skin.viewlet.menu import MenuDivider, MenuItem
 from pyams_utils.interfaces import MANAGE_SYSTEM_PERMISSION
 from pyams_utils.interfaces.site import ISiteRoot
+from pyams_viewlet.viewlet import viewlet_config
+from pyams_zmi import _
+from pyams_zmi.form import AdminDialogEditForm
 from pyams_zmi.interfaces.menu import IPropertiesMenu
 from pyams_zmi.layer import IAdminLayer
 
-# import packages
-from pyams_form.form import AJAXEditForm
-from pyams_pagelet.pagelet import pagelet_config
-from pyams_skin.viewlet.menu import MenuItem, MenuDivider
-from pyams_viewlet.viewlet import viewlet_config
-from pyams_zmi.form import AdminDialogEditForm
-from pyramid.view import view_config
-from z3c.form import field
-
-from pyams_zmi import _
-
 
 @viewlet_config(name='extensions-divider.menu', context=ISiteRoot, layer=IAdminLayer, manager=IPropertiesMenu,
                 permission=MANAGE_SYSTEM_PERMISSION, weight=9)
@@ -58,25 +53,35 @@
 
 @pagelet_config(name='tag-manager-properties.html', context=ISiteRoot, layer=IPyAMSLayer,
                 permission=MANAGE_SYSTEM_PERMISSION)
+@ajax_config(name='tag-manager-properties.json', context=ISiteRoot, layer=IPyAMSLayer)
 class GoogleTagManagerPropertiesEditForm(AdminDialogEditForm):
     """Google Tag Manager properties edit form"""
 
-    prefix = 'tagmanager_properties.'
+    prefix = 'tag_manager_properties.'
 
     legend = _("Update Google Tag Manager properties")
-    ajax_handler = 'tag-manager-properties.json'
     edit_permission = MANAGE_SYSTEM_PERMISSION
 
     fields = field.Fields(IGoogleTagManagerInfo)
+    fields['on_accepted_cookie'].widgetFactory = SingleCheckBoxFieldWidget
 
     label_css_class = 'control-label col-md-4'
     input_css_class = 'col-md-8'
 
-
-@view_config(name='tag-manager-properties.json', context=ISiteRoot, request_type=IPyAMSLayer,
-             permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
-class GoogleTagManagerPropertiesAJAXEditForm(AJAXEditForm, GoogleTagManagerPropertiesEditForm):
-    """Google Tag Manager properties edit form, JSON renderer"""
+    def updateGroups(self):
+        self.add_group(NamedWidgetsGroup(self, 'cookie_accept', self.widgets,
+                                         ('on_accepted_cookie', 'cookie_name', 'rejected_cookie_value'),
+                                         legend=_("Activate only if not rejected cookies"),
+                                         css_class='inner',
+                                         help=_("If you have a cookie acceptance banner allowing your website users "
+                                                "to accept or deny cookies, this extension will be activated only if "
+                                                "cookies have not been rejected."),
+                                         switch=True,
+                                         checkbox_switch=True,
+                                         checkbox_field=IGoogleTagManagerInfo['on_accepted_cookie']))
+        self.add_group(NamedWidgetsGroup(self, 'container_id', self.widgets,
+                                         ('container_id', 'activation_mode')))
+        super(GoogleTagManagerPropertiesEditForm, self).updateGroups()
 
 
 #
@@ -96,25 +101,35 @@
 
 @pagelet_config(name='analytics-properties.html', context=ISiteRoot, layer=IPyAMSLayer,
                 permission=MANAGE_SYSTEM_PERMISSION)
+@ajax_config(name='analytics-properties.json', context=ISiteRoot, layer=IPyAMSLayer)
 class GoogleAnalyticsPropertiesEditForm(AdminDialogEditForm):
     """Google Analytics properties edit form"""
 
     prefix = 'analytics_properties.'
 
     legend = _("Update Google Analytics properties")
-    ajax_handler = 'analytics-properties.json'
     edit_permission = MANAGE_SYSTEM_PERMISSION
 
     fields = field.Fields(IGoogleAnalyticsInfo)
+    fields['on_accepted_cookie'].widgetFactory = SingleCheckBoxFieldWidget
 
     label_css_class = 'control-label col-md-4'
     input_css_class = 'col-md-8'
 
-
-@view_config(name='analytics-properties.json', context=ISiteRoot, request_type=IPyAMSLayer,
-             permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
-class GoogleAnalyticsPropertiesAJAXEditForm(AJAXEditForm, GoogleAnalyticsPropertiesEditForm):
-    """Google Analytics properties edit form, JSON renderer"""
+    def updateGroups(self):
+        self.add_group(NamedWidgetsGroup(self, 'cookie_accept', self.widgets,
+                                         ('on_accepted_cookie', 'cookie_name', 'rejected_cookie_value'),
+                                         legend=_("Activate only if not rejected cookies"),
+                                         css_class='inner',
+                                         help=_("If you have a cookie acceptance banner allowing your website users "
+                                                "to accept or deny cookies, this extension will be activated only if "
+                                                "cookies have not been rejected."),
+                                         switch=True,
+                                         checkbox_switch=True,
+                                         checkbox_field=IGoogleAnalyticsInfo['on_accepted_cookie']))
+        self.add_group(NamedWidgetsGroup(self, 'website_id', self.widgets,
+                                         ('website_id', 'activation_mode')))
+        super(GoogleAnalyticsPropertiesEditForm, self).updateGroups()
 
 
 #
@@ -128,28 +143,38 @@
 
     label = _("UserReport settings...")
     icon_class = 'fa-comments'
-    url = 'userreport-properties.html'
+    url = 'user-report-properties.html'
     modal_target = True
 
 
-@pagelet_config(name='userreport-properties.html', context=ISiteRoot, layer=IPyAMSLayer,
+@pagelet_config(name='user-report-properties.html', context=ISiteRoot, layer=IPyAMSLayer,
                 permission=MANAGE_SYSTEM_PERMISSION)
+@ajax_config(name='user-report-properties.json', context=ISiteRoot, layer=IPyAMSLayer)
 class UserReportPropertiesEditForm(AdminDialogEditForm):
     """UserReport properties edit form"""
 
-    prefix = 'userreport_properties.'
+    prefix = 'user_report_properties.'
 
     legend = _("Update UserReport service properties")
-    ajax_handler = 'user-report.json'
     edit_permission = MANAGE_SYSTEM_PERMISSION
 
     fields = field.Fields(IUserReportInfo)
+    fields['on_accepted_cookie'].widgetFactory = SingleCheckBoxFieldWidget
 
     label_css_class = 'control-label col-md-4'
     input_css_class = 'col-md-8'
 
-
-@view_config(name='user-report.json', context=ISiteRoot, request_type=IPyAMSLayer,
-             permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
-class UserReportPropertiesAJAXEditForm(AJAXEditForm, UserReportPropertiesEditForm):
-    """UserReport properties edit form, JSON renderer"""
+    def updateGroups(self):
+        self.add_group(NamedWidgetsGroup(self, 'cookie_accept', self.widgets,
+                                         ('on_accepted_cookie', 'cookie_name', 'rejected_cookie_value'),
+                                         legend=_("Activate only if not rejected cookies"),
+                                         css_class='inner',
+                                         help=_("If you have a cookie acceptance banner allowing your website users "
+                                                "to accept or deny cookies, this extension will be activated only if "
+                                                "cookies have not been rejected."),
+                                         switch=True,
+                                         checkbox_switch=True,
+                                         checkbox_field=IUserReportInfo['on_accepted_cookie']))
+        self.add_group(NamedWidgetsGroup(self, 'account_id', self.widgets,
+                                         ('account_id', 'activation_mode')))
+        super(UserReportPropertiesEditForm, self).updateGroups()