Added Google's Recaptcha support
authorThierry Florac <tflorac@ulthar.net>
Fri, 26 Jul 2019 12:59:07 +0200
changeset 1335 e090134614d2
parent 1334 fdbb7da1f7af
child 1336 c988a8a803a1
Added Google's Recaptcha support
src/pyams_content/shared/form/__init__.py
src/pyams_content/shared/form/zmi/properties.py
--- a/src/pyams_content/shared/form/__init__.py	Fri Jul 26 12:58:31 2019 +0200
+++ b/src/pyams_content/shared/form/__init__.py	Fri Jul 26 12:59:07 2019 +0200
@@ -10,20 +10,12 @@
 # FOR A PARTICULAR PURPOSE.
 #
 
-__docformat__ = 'restructuredtext'
-
-
-# import standard library
-
 from zope.interface import alsoProvides, implementer, noLongerProvides, provider
 from zope.schema.fieldproperty import FieldProperty
 
-from pyams_content import _
-# import interfaces
 from pyams_content.features.checker.interfaces import ERROR_VALUE, IContentChecker
 from pyams_content.features.preview.interfaces import IPreviewTarget
 from pyams_content.features.review.interfaces import IReviewTarget
-# import packages
 from pyams_content.shared.common import IWfSharedContentFactory, SharedContent, WfSharedContent, WfSharedContentChecker, \
     register_content_type
 from pyams_content.shared.form.interfaces import FORM_CONTENT_NAME, FORM_CONTENT_TYPE, IForm, IFormFieldContainer, \
@@ -32,6 +24,11 @@
 from pyams_utils.registry import get_global_registry
 
 
+__docformat__ = 'restructuredtext'
+
+from pyams_content import _
+
+
 @implementer(IWfForm, IFormFieldContainerTarget,
              IPreviewTarget, IReviewTarget)
 class WfForm(WfSharedContent):
@@ -41,10 +38,15 @@
     content_name = FORM_CONTENT_NAME
 
     user_title = FieldProperty(IWfForm['user_title'])
+    auth_only = FieldProperty(IWfForm['auth_only'])
+    form_header = FieldProperty(IWfForm['form_header'])
+    submit_label = FieldProperty(IWfForm['submit_label'])
+    submit_message = FieldProperty(IWfForm['submit_message'])
     _handler = FieldProperty(IWfForm['handler'])
-    auth_only = FieldProperty(IWfForm['auth_only'])
     use_captcha = FieldProperty(IWfForm['use_captcha'])
-    submit_label = FieldProperty(IWfForm['submit_label'])
+    client_captcha_key = FieldProperty(IWfForm['client_captcha_key'])
+    server_captcha_key = FieldProperty(IWfForm['server_captcha_key'])
+    captcha_proxy = FieldProperty(IWfForm['captcha_proxy'])
 
     @property
     def handler(self):
--- a/src/pyams_content/shared/form/zmi/properties.py	Fri Jul 26 12:58:31 2019 +0200
+++ b/src/pyams_content/shared/form/zmi/properties.py	Fri Jul 26 12:59:07 2019 +0200
@@ -10,22 +10,20 @@
 # FOR A PARTICULAR PURPOSE.
 #
 
-__docformat__ = 'restructuredtext'
+from z3c.form import field
+from z3c.form.browser.checkbox import SingleCheckBoxFieldWidget
+from zope.interface import Interface
+
+from pyams_content.shared.common.zmi.properties import SharedContentPropertiesEditForm
+from pyams_content.shared.form.interfaces import IWfForm
+from pyams_form.group import NamedWidgetsGroup
+from pyams_form.interfaces.form import IInnerSubForm
+from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.adapter import adapter_config
+from pyams_zmi.form import InnerAdminEditForm
 
 
-# import standard library
-
-# import interfaces
-from pyams_content.shared.form.interfaces import IWfForm
-from pyams_form.interfaces.form import IInnerSubForm
-from pyams_skin.layer import IPyAMSLayer
-
-# import packages
-from pyams_content.shared.common.zmi.properties import SharedContentPropertiesEditForm
-from pyams_utils.adapter import adapter_config
-from pyams_zmi.form import InnerAdminEditForm
-from z3c.form import field
-from zope.interface import Interface
+__docformat__ = 'restructuredtext'
 
 from pyams_content import _
 
@@ -41,9 +39,27 @@
     legend = _("Main form settings")
     fieldset_class = 'bordered no-x-margin margin-y-10'
 
-    fields = field.Fields(IWfForm).select('user_title', 'header', 'handler', 'auth_only', 'use_captcha', 'submit_label')
+    fields = field.Fields(IWfForm).select('user_title', 'form_header', 'auth_only', 'submit_label',
+                                          'submit_message', 'handler', 'use_captcha', 'client_captcha_key',
+                                          'server_captcha_key', 'captcha_proxy')
+    fields['use_captcha'].widgetFactory = SingleCheckBoxFieldWidget
+
     weight = 1
 
+    def updateGroups(self):
+        self.add_group(NamedWidgetsGroup(self, 'head', self.widgets,
+                                         ('user_title', 'form_header', 'auth_only',
+                                          'submit_label', 'submit_message', 'handler')))
+        self.add_group(NamedWidgetsGroup(self, 'captcha', self.widgets,
+                                         ('use_captcha', 'client_captcha_key', 'server_captcha_key', 'captcha_proxy'),
+                                         fieldset_class='inner bordered',
+                                         legend=_("Add captcha"),
+                                         css_class='inner',
+                                         switch=True,
+                                         checkbox_switch=True,
+                                         checkbox_field=IWfForm['use_captcha']))
+        super(FormPropertiesEditForm, self).updateGroups()
+
     def get_ajax_output(self, changes):
         if 'handler' in changes.get(IWfForm, ()):
             return {
@@ -62,7 +78,6 @@
 
     prefix = 'form_handler.'
 
-    legend = _("Form handler settings")
     fieldset_class = 'bordered no-x-margin margin-y-10'
 
     def __new__(cls, context, request, view):
@@ -72,6 +87,15 @@
         return InnerAdminEditForm.__new__(cls)
 
     @property
+    def legend(self):
+        handler = self.context.query_handler()
+        if handler is not None:
+            translate = self.request.localizer.translate
+            return translate(_("« {handler} » form handler settings")).format(handler=translate(handler.label))
+        else:
+            return _("Form handler settings")
+
+    @property
     def fields(self):
         handler = self.context.query_handler()
         if handler is None: