Add settings to Google/UserReport extensions to disable extensions when cookies are rejected
authorThierry Florac <tflorac@ulthar.net>
Wed, 16 Jan 2019 11:56:44 +0100
changeset 506 08196fe15f4a
parent 505 f6b70a0c19e3
child 507 1c7bd4ccfb32
Add settings to Google/UserReport extensions to disable extensions when cookies are rejected
src/pyams_skin/extension.py
src/pyams_skin/interfaces/extension.py
src/pyams_skin/viewlet/extension/analytics.py
src/pyams_skin/viewlet/extension/tagmanager.py
src/pyams_skin/viewlet/extension/user_report.py
--- a/src/pyams_skin/extension.py	Fri Dec 28 10:58:27 2018 +0100
+++ b/src/pyams_skin/extension.py	Wed Jan 16 11:56:44 2019 +0100
@@ -12,27 +12,24 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
+from persistent import Persistent
+from zope.schema.fieldproperty import FieldProperty
 
-# import interfaces
-from pyams_skin.interfaces.extension import \
-    IGoogleAnalyticsInfo, GOOGLE_ANALYTICS_INFO_KEY, \
-    IUserReportInfo, USER_REPORT_INFO_KEY, IGoogleTagManagerInfo, GOOGLE_TAGS_INFO_KEY
+from pyams_skin.interfaces.extension import GOOGLE_ANALYTICS_INFO_KEY, GOOGLE_TAGS_INFO_KEY, IGoogleAnalyticsInfo, \
+    IGoogleTagManagerInfo, IUserReportInfo, USER_REPORT_INFO_KEY
+from pyams_utils.adapter import adapter_config, get_annotation_adapter
+from pyams_utils.factory import factory_config
 from pyams_utils.interfaces.site import ISiteRoot
 
-# import packages
-from persistent import Persistent
-from pyams_utils.adapter import adapter_config, get_annotation_adapter
-from zope.interface import implementer
-from zope.schema.fieldproperty import FieldProperty
 
-
-@implementer(IGoogleTagManagerInfo)
+@factory_config(IGoogleTagManagerInfo)
 class GoogleTagManagerInfo(Persistent):
     """Google tag manager settings class"""
 
     enabled = FieldProperty(IGoogleTagManagerInfo['enabled'])
+    on_accepted_cookie = FieldProperty(IGoogleTagManagerInfo['on_accepted_cookie'])
+    cookie_name = FieldProperty(IGoogleTagManagerInfo['cookie_name'])
+    rejected_cookie_value = FieldProperty(IGoogleTagManagerInfo['rejected_cookie_value'])
     container_id = FieldProperty(IGoogleTagManagerInfo['container_id'])
     activation_mode = FieldProperty(IGoogleTagManagerInfo['activation_mode'])
 
@@ -40,15 +37,18 @@
 @adapter_config(context=ISiteRoot, provides=IGoogleTagManagerInfo)
 def google_tag_manager_info_factory(context):
     """Google Tag Manager settings factory"""
-    return get_annotation_adapter(context, GOOGLE_TAGS_INFO_KEY, GoogleTagManagerInfo,
+    return get_annotation_adapter(context, GOOGLE_TAGS_INFO_KEY, IGoogleTagManagerInfo,
                                   notify=False, locate=False)
 
 
-@implementer(IGoogleAnalyticsInfo)
+@factory_config(IGoogleAnalyticsInfo)
 class GoogleAnalyticsInfo(Persistent):
     """Google Analytics settings class"""
 
     enabled = FieldProperty(IGoogleAnalyticsInfo['enabled'])
+    on_accepted_cookie = FieldProperty(IGoogleAnalyticsInfo['on_accepted_cookie'])
+    cookie_name = FieldProperty(IGoogleAnalyticsInfo['cookie_name'])
+    rejected_cookie_value = FieldProperty(IGoogleAnalyticsInfo['rejected_cookie_value'])
     website_id = FieldProperty(IGoogleAnalyticsInfo['website_id'])
     verification_code = FieldProperty(IGoogleAnalyticsInfo['verification_code'])
     activation_mode = FieldProperty(IGoogleAnalyticsInfo['activation_mode'])
@@ -57,15 +57,18 @@
 @adapter_config(context=ISiteRoot, provides=IGoogleAnalyticsInfo)
 def google_analytics_info_factory(context):
     """Google Analytics settings factory"""
-    return get_annotation_adapter(context, GOOGLE_ANALYTICS_INFO_KEY, GoogleAnalyticsInfo,
+    return get_annotation_adapter(context, GOOGLE_ANALYTICS_INFO_KEY, IGoogleAnalyticsInfo,
                                   notify=False, locate=False)
 
 
-@implementer(IUserReportInfo)
+@factory_config(IUserReportInfo)
 class UserReportInfo(Persistent):
     """User report settings class"""
 
     enabled = FieldProperty(IUserReportInfo['enabled'])
+    on_accepted_cookie = FieldProperty(IUserReportInfo['on_accepted_cookie'])
+    cookie_name = FieldProperty(IUserReportInfo['cookie_name'])
+    rejected_cookie_value = FieldProperty(IUserReportInfo['rejected_cookie_value'])
     account_id = FieldProperty(IUserReportInfo['account_id'])
     activation_mode = FieldProperty(IUserReportInfo['activation_mode'])
 
@@ -73,5 +76,5 @@
 @adapter_config(context=ISiteRoot, provides=IUserReportInfo)
 def user_report_info_factory(context):
     """User report settings factory"""
-    return get_annotation_adapter(context, USER_REPORT_INFO_KEY, UserReportInfo,
+    return get_annotation_adapter(context, USER_REPORT_INFO_KEY, IUserReportInfo,
                                   notify=False, locate=False)
--- a/src/pyams_skin/interfaces/extension.py	Fri Dec 28 10:58:27 2018 +0100
+++ b/src/pyams_skin/interfaces/extension.py	Wed Jan 16 11:56:44 2019 +0100
@@ -12,15 +12,9 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
-
-# import interfaces
-
-# import packages
 from zope.interface import Interface
-from zope.schema import TextLine, Bool, Choice
-from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
+from zope.schema import Bool, Choice, TextLine
+from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
 
 from pyams_skin import _
 
@@ -47,6 +41,19 @@
                    required=True,
                    default=False)
 
+    on_accepted_cookie = Bool(title=_("Activate only if not rejected cookies?"),
+                              description=_("If 'no', plug-in will be activated without waiting for accepted cookies"),
+                              required=False,
+                              default=False)
+
+    cookie_name = TextLine(title=_("Cookie name"),
+                           description=_("Name of cookie checked to verify if cookies have been rejected"),
+                           required=False)
+
+    rejected_cookie_value = TextLine(title=_("Rejected cookie value"),
+                                     description=_("Cookie value matching user's cookies reject"),
+                                     required=False)
+
     container_id = TextLine(title=_("Container ID"),
                             description=_("Google Tag Manager container ID (may start with 'GTM-')"),
                             required=False)
@@ -64,19 +71,32 @@
 class IGoogleAnalyticsInfo(Interface):
     """Google Analytics account info"""
 
+    verification_code = TextLine(title=_("Web site verification code"),
+                                 description=_("Google site verification code"),
+                                 required=False)
+
     enabled = Bool(title=_("Activate Google Analytics?"),
                    description=_("Are Google Analytics statistics activated?"),
                    required=True,
                    default=False)
 
+    on_accepted_cookie = Bool(title=_("Activate only if not rejected cookies?"),
+                              description=_("If 'no', plug-in will be activated without waiting for accepted cookies"),
+                              required=False,
+                              default=False)
+
+    cookie_name = TextLine(title=_("Cookie name"),
+                           description=_("Name of cookie checked to verify if cookies have been rejected"),
+                           required=False)
+
+    rejected_cookie_value = TextLine(title=_("Rejected cookie value"),
+                                     description=_("Cookie value matching user's cookies reject"),
+                                     required=False)
+
     website_id = TextLine(title=_("Web site ID"),
                           description=_("Google Analytics web site ID"),
                           required=False)
 
-    verification_code = TextLine(title=_("Web site verification code"),
-                                 description=_("Google site verification code"),
-                                 required=False)
-
     activation_mode = Choice(title=_("Activation mode"),
                              description=_("Mode(s) in which statistics are activated"),
                              vocabulary=ACTIVATION_MODES,
@@ -95,6 +115,19 @@
                    required=True,
                    default=False)
 
+    on_accepted_cookie = Bool(title=_("Activate only if not rejected cookies?"),
+                              description=_("If 'no', plug-in will be activated without waiting for accepted cookies"),
+                              required=False,
+                              default=False)
+
+    cookie_name = TextLine(title=_("Cookie name"),
+                           description=_("Name of cookie checked to verify if cookies have been rejected"),
+                           required=False)
+
+    rejected_cookie_value = TextLine(title=_("Rejected cookie value"),
+                                     description=_("Cookie value matching user's cookies reject"),
+                                     required=False)
+
     account_id = TextLine(title=_("Account ID"),
                           description=_("UserReport account ID, available in 'initSite' code snippet"),
                           required=False)
--- a/src/pyams_skin/viewlet/extension/analytics.py	Fri Dec 28 10:58:27 2018 +0100
+++ b/src/pyams_skin/viewlet/extension/analytics.py	Wed Jan 16 11:56:44 2019 +0100
@@ -12,22 +12,17 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
-
-# import interfaces
-from pyams_skin.interfaces.extension import IGoogleAnalyticsInfo, ACTIVATED_ON_FRONT, ACTIVATED_ON_BACK
+from pyams_skin.interfaces.extension import ACTIVATED_ON_BACK, ACTIVATED_ON_FRONT, IGoogleAnalyticsInfo
 from pyams_skin.interfaces.viewlet import IJSExtensionsViewletManager
 from pyams_skin.layer import IPyAMSLayer
+from pyams_template.template import template_config
+from pyams_viewlet.viewlet import Viewlet, viewlet_config
+
 try:
     from pyams_zmi.layer import IAdminLayer
 except ImportError:
     IAdminLayer = None
 
-# import packages
-from pyams_template.template import template_config
-from pyams_viewlet.viewlet import viewlet_config, Viewlet
-
 
 @viewlet_config(name='analytics', manager=IJSExtensionsViewletManager)
 @template_config(template='templates/analytics.pt', layer=IPyAMSLayer)
@@ -45,6 +40,10 @@
             if ((info.activation_mode == ACTIVATED_ON_FRONT) and IAdminLayer.providedBy(request)) or \
                ((info.activation_mode == ACTIVATED_ON_BACK) and not IAdminLayer.providedBy(request)):
                 return None
+        if info.on_accepted_cookie:
+            cookie_value = request.cookies.get(info.cookie_name)
+            if cookie_value == info.rejected_cookie_value:
+                return None
         return Viewlet.__new__(cls)
 
     @property
--- a/src/pyams_skin/viewlet/extension/tagmanager.py	Fri Dec 28 10:58:27 2018 +0100
+++ b/src/pyams_skin/viewlet/extension/tagmanager.py	Wed Jan 16 11:56:44 2019 +0100
@@ -12,22 +12,17 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
-
-# import interfaces
-from pyams_skin.interfaces.extension import IGoogleTagManagerInfo, ACTIVATED_ON_FRONT, ACTIVATED_ON_BACK
+from pyams_skin.interfaces.extension import ACTIVATED_ON_BACK, ACTIVATED_ON_FRONT, IGoogleTagManagerInfo
 from pyams_skin.interfaces.viewlet import IJSExtensionsViewletManager
 from pyams_skin.layer import IPyAMSLayer
+from pyams_template.template import template_config
+from pyams_viewlet.viewlet import Viewlet, viewlet_config
+
 try:
     from pyams_zmi.layer import IAdminLayer
 except ImportError:
     IAdminLayer = None
 
-# import packages
-from pyams_template.template import template_config
-from pyams_viewlet.viewlet import viewlet_config, Viewlet
-
 
 @viewlet_config(name='tag-manager', manager=IJSExtensionsViewletManager)
 @template_config(template='templates/tag-manager.pt', layer=IPyAMSLayer)
@@ -45,6 +40,10 @@
             if ((info.activation_mode == ACTIVATED_ON_FRONT) and IAdminLayer.providedBy(request)) or \
                ((info.activation_mode == ACTIVATED_ON_BACK) and not IAdminLayer.providedBy(request)):
                 return None
+        if info.on_accepted_cookie:
+            cookie_value = request.cookies.get(info.cookie_name)
+            if cookie_value == info.rejected_cookie_value:
+                return None
         return Viewlet.__new__(cls)
 
     @property
--- a/src/pyams_skin/viewlet/extension/user_report.py	Fri Dec 28 10:58:27 2018 +0100
+++ b/src/pyams_skin/viewlet/extension/user_report.py	Wed Jan 16 11:56:44 2019 +0100
@@ -12,22 +12,17 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
-
-# import interfaces
-from pyams_skin.interfaces.extension import IUserReportInfo, ACTIVATED_ON_FRONT, ACTIVATED_ON_BACK
+from pyams_skin.interfaces.extension import ACTIVATED_ON_BACK, ACTIVATED_ON_FRONT, IUserReportInfo
 from pyams_skin.interfaces.viewlet import IJSExtensionsViewletManager
 from pyams_skin.layer import IPyAMSLayer
+from pyams_template.template import template_config
+from pyams_viewlet.viewlet import Viewlet, viewlet_config
+
 try:
     from pyams_zmi.layer import IAdminLayer
 except ImportError:
     IAdminLayer = None
 
-# import packages
-from pyams_template.template import template_config
-from pyams_viewlet.viewlet import viewlet_config, Viewlet
-
 
 @viewlet_config(name='user-report', manager=IJSExtensionsViewletManager)
 @template_config(template='templates/user_report.pt', layer=IPyAMSLayer)
@@ -45,6 +40,10 @@
             if ((info.activation_mode == ACTIVATED_ON_FRONT) and IAdminLayer.providedBy(request)) or \
                ((info.activation_mode == ACTIVATED_ON_BACK) and not IAdminLayer.providedBy(request)):
                 return None
+        if info.on_accepted_cookie:
+            cookie_value = request.cookies.get(info.cookie_name)
+            if cookie_value == info.rejected_cookie_value:
+                return None
         return Viewlet.__new__(cls)
 
     @property