diff --git a/kmicms/kmicms/configuration.dev.py b/kmicms/kmicms/configuration.dev.py index 1c8e45a..08279c3 100644 --- a/kmicms/kmicms/configuration.dev.py +++ b/kmicms/kmicms/configuration.dev.py @@ -28,3 +28,6 @@ DISCORD_APP_CLIENT_ID = "" DISCORD_APP_CLIENT_SECRET = "" + +RECAPTCHA_PUBLIC_KEY = "" +RECAPTCHA_PRIVATE_KEY = "" diff --git a/kmicms/kmicms/configuration.prod.py b/kmicms/kmicms/configuration.prod.py index f3f404a..9d4e106 100644 --- a/kmicms/kmicms/configuration.prod.py +++ b/kmicms/kmicms/configuration.prod.py @@ -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" diff --git a/kmicms/kmicms/settings.py b/kmicms/kmicms/settings.py index 9fa8ca1..2d6e6f8 100644 --- a/kmicms/kmicms/settings.py +++ b/kmicms/kmicms/settings.py @@ -87,7 +87,6 @@ EMAIL_TIMEOUT = EMAIL.get("TIMEOUT", 10) SERVER_EMAIL = EMAIL.get("FROM_EMAIL") - INSTALLED_APPS = [ "accounts", "core", @@ -96,6 +95,7 @@ "pages.standard_page", # 3rd party "compressor", + "django_recaptcha", "crispy_forms", "crispy_bootstrap5", # Wagtail / Django @@ -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) diff --git a/kmicms/pages/contact/forms.py b/kmicms/pages/contact/forms.py new file mode 100644 index 0000000..c65589a --- /dev/null +++ b/kmicms/pages/contact/forms.py @@ -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) diff --git a/kmicms/pages/contact/models.py b/kmicms/pages/contact/models.py index 1a01184..856793a 100644 --- a/kmicms/pages/contact/models.py +++ b/kmicms/pages/contact/models.py @@ -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") @@ -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, @@ -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 = [] diff --git a/requirements-dev.txt b/requirements-dev.txt index 8f4ebfc..4f6813b 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -60,6 +60,7 @@ django==4.2.7 # django-filter # django-modelcluster # django-permissionedforms + # django-recaptcha # django-taggit # django-treebeard # djangorestframework @@ -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 diff --git a/requirements.in b/requirements.in index 4ed6079..0cee1cb 100644 --- a/requirements.in +++ b/requirements.in @@ -7,6 +7,7 @@ django-libsass crispy-bootstrap5 authlib>=1.2.1,<2 +django-recaptcha>=4,<5 pydantic>=2.4,<3 requests diff --git a/requirements.txt b/requirements.txt index a48ffc2..9a92016 100644 --- a/requirements.txt +++ b/requirements.txt @@ -35,6 +35,7 @@ django==4.2.7 # django-filter # django-modelcluster # django-permissionedforms + # django-recaptcha # django-taggit # django-treebeard # djangorestframework @@ -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