src/pyams_content/shared/form/zmi/properties.py
changeset 1346 88b5ce31afdc
parent 1343 530cbb970243
child 1362 60f8b160341c
equal deleted inserted replaced
1345:9b406fb98cfa 1346:88b5ce31afdc
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
    10 # FOR A PARTICULAR PURPOSE.
    10 # FOR A PARTICULAR PURPOSE.
    11 #
    11 #
    12 
    12 from pyramid.events import subscriber
    13 from z3c.form import field
    13 from z3c.form import field
    14 from z3c.form.browser.checkbox import SingleCheckBoxFieldWidget
    14 from z3c.form.browser.checkbox import SingleCheckBoxFieldWidget
    15 from zope.interface import Interface
    15 from z3c.form.interfaces import IDataExtractedEvent, INPUT_MODE
       
    16 from zope.interface import Interface, Invalid
    16 
    17 
    17 from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
    18 from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
    18 from pyams_content.shared.common.zmi.properties import SharedContentPropertiesEditForm
    19 from pyams_content.shared.common.zmi.properties import SharedContentPropertiesEditForm
    19 from pyams_content.shared.form.interfaces import IWfForm
    20 from pyams_content.shared.form.interfaces import IWfForm
    20 from pyams_form.form import ajax_config
    21 from pyams_form.form import ajax_config
    56     fieldset_class = 'bordered no-x-margin margin-y-10'
    57     fieldset_class = 'bordered no-x-margin margin-y-10'
    57 
    58 
    58     fields = field.Fields(IWfForm).select('form_header', 'user_title', 'auth_only',
    59     fields = field.Fields(IWfForm).select('form_header', 'user_title', 'auth_only',
    59                                           'submit_label', 'submit_message', 'handler',
    60                                           'submit_label', 'submit_message', 'handler',
    60                                           'use_captcha', 'client_captcha_key',
    61                                           'use_captcha', 'client_captcha_key',
    61                                           'server_captcha_key', 'captcha_proxy')
    62                                           'server_captcha_key', 'captcha_proxy',
       
    63                                           'rgpd_consent', 'rgpd_warning', 'rgpd_user_rights')
    62     fields['use_captcha'].widgetFactory = SingleCheckBoxFieldWidget
    64     fields['use_captcha'].widgetFactory = SingleCheckBoxFieldWidget
       
    65     fields['rgpd_consent'].widgetFactory = SingleCheckBoxFieldWidget
    63 
    66 
    64     weight = 1
    67     weight = 1
       
    68 
       
    69     def updateWidgets(self, prefix=None):
       
    70         super(FormPropertiesInnerEditForm, self).updateWidgets(prefix)
       
    71         if self.mode == INPUT_MODE:
       
    72             translate = self.request.localizer.translate
       
    73             if 'rgpd_warning' in self.widgets:
       
    74                 self.widgets['rgpd_warning'].after_widget_notice = \
       
    75                     '<div class="alert-info padding-5">{0}</div>'.format(
       
    76                         translate(_("Text samples:<br />"
       
    77                                     "- By submitting this form, I agree that the information "
       
    78                                     "entered may be used for the purpose of my request and the "
       
    79                                     "business relationship that may result from it.")))
       
    80             if 'rgpd_user_rights' in self.widgets:
       
    81                 self.widgets['rgpd_user_rights'].after_widget_notice = \
       
    82                     '<div class="alert-info padding-5">{0}</div>'.format(
       
    83                         translate(_("Text samples:<br />"
       
    84                                     "- To know and enforce your rights, including the right to "
       
    85                                     "withdraw your consent to the use of the data collected by "
       
    86                                     "this form, please consult our privacy policy.")))
    65 
    87 
    66     def updateGroups(self):
    88     def updateGroups(self):
    67         self.add_group(NamedWidgetsGroup(self, 'head', self.widgets,
    89         self.add_group(NamedWidgetsGroup(self, 'head', self.widgets,
    68                                          ('form_header', 'user_title', 'auth_only',
    90                                          ('form_header', 'user_title', 'auth_only',
    69                                           'submit_label', 'submit_message', 'handler')))
    91                                           'submit_label', 'submit_message', 'handler')))
    74                                          legend=_("Add captcha"),
    96                                          legend=_("Add captcha"),
    75                                          css_class='inner',
    97                                          css_class='inner',
    76                                          switch=True,
    98                                          switch=True,
    77                                          checkbox_switch=True,
    99                                          checkbox_switch=True,
    78                                          checkbox_field=IWfForm['use_captcha']))
   100                                          checkbox_field=IWfForm['use_captcha']))
       
   101         self.add_group(NamedWidgetsGroup(self, 'rgpd', self.widgets,
       
   102                                          ('rgpd_consent', 'rgpd_warning', 'rgpd_user_rights'),
       
   103                                          fieldset_class='inner bordered',
       
   104                                          legend=_("Add RGPD warning"),
       
   105                                          css_class='inner',
       
   106                                          switch=True,
       
   107                                          checkbox_switch=True,
       
   108                                          checkbox_field=IWfForm['rgpd_consent']))
    79         super(FormPropertiesInnerEditForm, self).updateGroups()
   109         super(FormPropertiesInnerEditForm, self).updateGroups()
    80 
   110 
    81     def get_ajax_output(self, changes):
   111     def get_ajax_output(self, changes):
    82         if 'handler' in changes.get(IWfForm, ()):
   112         if 'handler' in changes.get(IWfForm, ()):
    83             return {
   113             return {
    84                 'status': 'reload',
   114                 'status': 'reload',
    85                 'message': self.request.localizer.translate(self.successMessage)
   115                 'message': self.request.localizer.translate(self.successMessage)
    86             }
   116             }
    87         else:
   117         else:
    88             return super(FormPropertiesInnerEditForm, self).get_ajax_output(changes)
   118             return super(FormPropertiesInnerEditForm, self).get_ajax_output(changes)
       
   119 
       
   120 
       
   121 @subscriber(IDataExtractedEvent, form_selector=FormPropertiesInnerEditForm)
       
   122 def check_form_properties_data(event):
       
   123     """Check form properties input data"""
       
   124     data = event.data
       
   125     if data.get('rgpd_consent'):
       
   126         for attr in ('rgpd_warning', 'rgpd_user_rights'):
       
   127             attr_ok = False
       
   128             for lang, value in data.get(attr, {}).items():
       
   129                 if value:
       
   130                     attr_ok = True
       
   131                     break
       
   132             if not attr_ok:
       
   133                 event.form.widgets.errors += (Invalid(_("You MUST set an RGPD consent text and "
       
   134                                                         "RGPD user rights to enable RGPD!")),)
       
   135                 return
    89 
   136 
    90 
   137 
    91 @adapter_config(name='handler-settings',
   138 @adapter_config(name='handler-settings',
    92                 context=(IWfForm, IPyAMSLayer, SharedContentPropertiesEditForm),
   139                 context=(IWfForm, IPyAMSLayer, SharedContentPropertiesEditForm),
    93                 provides=IInnerSubForm)
   140                 provides=IInnerSubForm)