Skip to content

Commit

Permalink
Add support for reCAPTCHA v3
Browse files Browse the repository at this point in the history
  • Loading branch information
ar4s committed Oct 14, 2021
1 parent da39697 commit 1dae7db
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
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

0 comments on commit 1dae7db

Please sign in to comment.