From 1dae7dbb2916b30855ee88e03de11b5bb897d5e4 Mon Sep 17 00:00:00 2001 From: Arkadiusz Adamski Date: Thu, 14 Oct 2021 18:37:01 +0200 Subject: [PATCH] Add support for reCAPTCHA v3 Closes #33 --- .gitignore | 1 + README.rst | 11 +++++++++-- wagtailcaptcha/conf.py | 5 +++++ wagtailcaptcha/forms.py | 7 ++++++- 4 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 wagtailcaptcha/conf.py diff --git a/.gitignore b/.gitignore index 6686e66..7b4e6aa 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ tests/testapp/var/media/ .coverage coverage coverage_html_report/ +tests/testapp/testapp/local.py diff --git a/README.rst b/README.rst index 7293a7b..a390c56 100644 --- a/README.rst +++ b/README.rst @@ -39,12 +39,12 @@ Example from wagtail.contrib.forms.models import AbstractFormField from wagtail.admin.edit_handlers import FieldPanel, InlinePanel, MultiFieldPanel from wagtail.core.fields import RichTextField - + # Or, if using Wagtail < 2.0 #from wagtail.wagtailforms.models import AbstractFormField #from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel, MultiFieldPanel #from wagtail.wagtailcore.fields import RichTextField - + from modelcluster.fields import ParentalKey from wagtailcaptcha.models import WagtailCaptchaEmailForm @@ -95,6 +95,13 @@ If you need to customise the behaviour of the form builder, make sure to inherit For a more thorough example, `Made with Wagtail `_ (`github.com/springload/madewithwagtail `_) is an example of an open-source site using this module. +Settings +~~~~~~~~ + +By default ``WagtailCaptchaEmailForm`` and ``WagtailCaptchaForm`` use reCAPTCHA v2. +If you want use reCAPTCHA in version 3 please add ``WAGTAIL_RECAPTCHA_VERSION = 3`` to your ``settings.py``. + + Development ----------- diff --git a/wagtailcaptcha/conf.py b/wagtailcaptcha/conf.py new file mode 100644 index 0000000..882c9d8 --- /dev/null +++ b/wagtailcaptcha/conf.py @@ -0,0 +1,5 @@ +from __future__ import absolute_import, unicode_literals + +from django.conf import settings + +WAGTAIL_RECAPTCHA_VERSION = getattr(settings, 'WAGTAIL_RECAPTCHA_VERSION', 2) diff --git a/wagtailcaptcha/forms.py b/wagtailcaptcha/forms.py index ccd0354..f928845 100644 --- a/wagtailcaptcha/forms.py +++ b/wagtailcaptcha/forms.py @@ -2,6 +2,9 @@ import wagtail from captcha.fields import ReCaptchaField +from captcha.widgets import ReCaptchaV2Checkbox, ReCaptchaV3 + +from wagtailcaptcha.conf import WAGTAIL_RECAPTCHA_VERSION if wagtail.VERSION >= (2, 0): from wagtail.contrib.forms.forms import FormBuilder @@ -9,6 +12,8 @@ from wagtail.wagtailforms.forms import FormBuilder +ReCaptchaWidget = ReCaptchaV2Checkbox if WAGTAIL_RECAPTCHA_VERSION == 2 else ReCaptchaV3 + class WagtailCaptchaFormBuilder(FormBuilder): CAPTCHA_FIELD_NAME = 'wagtailcaptcha' @@ -16,7 +21,7 @@ class WagtailCaptchaFormBuilder(FormBuilder): def formfields(self): # Add wagtailcaptcha to formfields property fields = super(WagtailCaptchaFormBuilder, self).formfields - fields[self.CAPTCHA_FIELD_NAME] = ReCaptchaField(label='') + fields[self.CAPTCHA_FIELD_NAME] = ReCaptchaField(label='', widget=ReCaptchaWidget()) return fields