Skip to content

Commit

Permalink
Merge pull request #2 from sown/recaptcha-forms
Browse files Browse the repository at this point in the history
Implement reCAPTCHA for contact forms
  • Loading branch information
trickeydan authored Dec 13, 2023
2 parents 2301caf + 91fb3ef commit 7eb7de8
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 1 deletion.
3 changes: 3 additions & 0 deletions kmicms/kmicms/configuration.dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@

DISCORD_APP_CLIENT_ID = ""
DISCORD_APP_CLIENT_SECRET = ""

RECAPTCHA_PUBLIC_KEY = ""
RECAPTCHA_PRIVATE_KEY = ""
3 changes: 3 additions & 0 deletions kmicms/kmicms/configuration.prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
DISCORD_APP_CLIENT_ID = os.environ.get("DISCORD_APP_CLIENT_ID")
DISCORD_APP_CLIENT_SECRET = os.environ.get("DISCORD_APP_CLIENT_SECRET")

RECAPTCHA_PUBLIC_KEY = os.environ.get("RECAPTCHA_PUBLIC_KEY")
RECAPTCHA_PRIVATE_KEY = os.environ.get("RECAPTCHA_PRIVATE_KEY")

TIME_ZONE = "Europe/London"

WAGTAILADMIN_BASE_URL = "https://kmicms.containers-1.sown.org.uk"
7 changes: 6 additions & 1 deletion kmicms/kmicms/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
EMAIL_TIMEOUT = EMAIL.get("TIMEOUT", 10)
SERVER_EMAIL = EMAIL.get("FROM_EMAIL")


INSTALLED_APPS = [
"accounts",
"core",
Expand All @@ -96,6 +95,7 @@
"pages.standard_page",
# 3rd party
"compressor",
"django_recaptcha",
"crispy_forms",
"crispy_bootstrap5",
# Wagtail / Django
Expand Down Expand Up @@ -311,3 +311,8 @@

CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
CRISPY_TEMPLATE_PACK = "bootstrap5"

# ReCAPTCHA

RECAPTCHA_PUBLIC_KEY = getattr(configuration, "RECAPTCHA_PUBLIC_KEY", None)
RECAPTCHA_PRIVATE_KEY = getattr(configuration, "RECAPTCHA_PRIVATE_KEY", None)
21 changes: 21 additions & 0 deletions kmicms/pages/contact/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django import forms
from django_recaptcha.fields import ReCaptchaField
from wagtail.contrib.forms.forms import FormBuilder


class ContactFormBuilder(FormBuilder):
CAPTCHA_FIELD_NAME = "captcha"

@property
def formfields(self) -> dict:
# Add wagtailcaptcha to formfields property
fields = super().formfields
fields[self.CAPTCHA_FIELD_NAME] = ReCaptchaField(label="")

return fields


def remove_captcha_field(form: forms.Form) -> None:
if form.is_valid():
form.fields.pop(ContactFormBuilder.CAPTCHA_FIELD_NAME, None)
form.cleaned_data.pop(ContactFormBuilder.CAPTCHA_FIELD_NAME, None)
5 changes: 5 additions & 0 deletions kmicms/pages/contact/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from integrations.discord import submit_discord_webhook_for_form

from .forms import ContactFormBuilder, remove_captcha_field


class FormField(AbstractFormField):
page = ParentalKey("ContactFormPage", on_delete=models.CASCADE, related_name="form_fields")
Expand Down Expand Up @@ -37,6 +39,8 @@ class Meta:
]

def process_form_submission(self, form: Any) -> Any:
remove_captcha_field(form)

submission = super().process_form_submission(form)
submit_discord_webhook_for_form(
self.discord_webhook,
Expand All @@ -50,6 +54,7 @@ class ContactFormPage(AbstractDiscordFormPage):
intro = RichTextField(blank=True)
thank_you_text = RichTextField(blank=True)

form_builder = ContactFormBuilder
parent_page_types = ["home.HomePage", "standard_page.StandardPage"]
subpage_types = []

Expand Down
3 changes: 3 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ django==4.2.7
# django-filter
# django-modelcluster
# django-permissionedforms
# django-recaptcha
# django-taggit
# django-treebeard
# djangorestframework
Expand Down Expand Up @@ -92,6 +93,8 @@ django-permissionedforms==0.1
# via
# -r requirements.txt
# wagtail
django-recaptcha==4.0.0
# via -r requirements.txt
django-taggit==4.0.0
# via
# -r requirements.txt
Expand Down
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ django-libsass
crispy-bootstrap5

authlib>=1.2.1,<2
django-recaptcha>=4,<5
pydantic>=2.4,<3
requests

Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ django==4.2.7
# django-filter
# django-modelcluster
# django-permissionedforms
# django-recaptcha
# django-taggit
# django-treebeard
# djangorestframework
Expand All @@ -57,6 +58,8 @@ django-modelcluster==6.1
# via wagtail
django-permissionedforms==0.1
# via wagtail
django-recaptcha==4.0.0
# via -r requirements.in
django-taggit==4.0.0
# via wagtail
django-treebeard==4.7
Expand Down

0 comments on commit 7eb7de8

Please sign in to comment.