Updated recaptcha error handling
authorThierry Florac <tflorac@ulthar.net>
Fri, 26 Jul 2019 19:11:53 +0200
changeset 454 90b0f5d3f2df
parent 453 d5f2be8c16ad
child 455 4e8a06768170
Updated recaptcha error handling
src/pyams_default_theme/shared/form/__init__.py
--- a/src/pyams_default_theme/shared/form/__init__.py	Fri Jul 26 15:39:58 2019 +0200
+++ b/src/pyams_default_theme/shared/form/__init__.py	Fri Jul 26 19:11:53 2019 +0200
@@ -14,7 +14,7 @@
 from pyramid.csrf import get_csrf_token
 from z3c.form import button, field
 from z3c.form.interfaces import HIDDEN_MODE
-from zope.interface import Interface
+from zope.interface import Interface, Invalid
 from zope.schema import TextLine
 
 from pyams_content.features.renderer.interfaces import ISharedContentRenderer
@@ -44,6 +44,9 @@
 CSRF_FIELD_NAME = 'csrf_token'
 RECAPTCHA_FIELD_NAME = 'g-recaptcha-response'
 
+MISSING_TOKEN_ERROR = _("Missing recaptcha token!")
+INVALID_TOKEN_ERROR = _("Can't verify recaptcha token! Are you a robot?")
+
 
 class FormFieldContainerInputForm(AddForm):
     """Form fields container display form"""
@@ -112,7 +115,7 @@
                     del data[CSRF_FIELD_NAME]
                 if self.context.use_captcha:
                     if RECAPTCHA_FIELD_NAME not in data:
-                        self.status = self.request.localizer.translate(_("Missing recaptcha token!"))
+                        self.add_error(Invalid(MISSING_TOKEN_ERROR), RECAPTCHA_FIELD_NAME)
                         return
                     proxies = {'https': self.context.captcha_proxy} if self.context.captcha_proxy else {}
                     recaptcha_verify_api = self.request.registry.settings.get('pyams.recaptcha.verify')
@@ -123,8 +126,7 @@
                         'response': data[RECAPTCHA_FIELD_NAME]
                     }, proxies=proxies).json()
                     if not verify['success']:
-                        self.status = self.request.localizer.translate(
-                            _("Can't verify recaptcha token! Are you a robot?"))
+                        self.add_error(INVALID_TOKEN_ERROR, RECAPTCHA_FIELD_NAME)
                         return
                     del data[RECAPTCHA_FIELD_NAME]
                 handler.handle(form, data)
@@ -200,7 +202,7 @@
 class FormSubmitPortletRenderer(ViewContentProvider):
     """Form submit message portlet renderer"""
 
-    use_portlets_cache = True
+    use_portlets_cache = False
 
     def render(self):
         form = self.view.input_form