Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for reCAPTCHA v3 #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ tests/testapp/var/media/
.coverage
coverage
coverage_html_report/
tests/testapp/testapp/local.py
11 changes: 9 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <http://madewithwagtail.org/>`_ (`github.com/springload/madewithwagtail <https://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
-----------

Expand Down
5 changes: 5 additions & 0 deletions wagtailcaptcha/conf.py
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 6 additions & 1 deletion wagtailcaptcha/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@

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
else:
from wagtail.wagtailforms.forms import FormBuilder


ReCaptchaWidget = ReCaptchaV2Checkbox if WAGTAIL_RECAPTCHA_VERSION == 2 else ReCaptchaV3

class WagtailCaptchaFormBuilder(FormBuilder):
CAPTCHA_FIELD_NAME = 'wagtailcaptcha'

@property
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

Expand Down