From 6e4f1a11dc8e370acc14cdfe500a23fa9e37b165 Mon Sep 17 00:00:00 2001 From: Mara Karagianni Date: Tue, 4 Jul 2023 19:02:51 +0300 Subject: [PATCH] apps/cms/contacts: add captcha to contact form if URL exists --- apps/cms/contacts/models.py | 16 +++++++++------- apps/users/forms.py | 2 +- tests/users/test_signup.py | 20 -------------------- 3 files changed, 10 insertions(+), 28 deletions(-) diff --git a/apps/cms/contacts/models.py b/apps/cms/contacts/models.py index eb5cba6745..7d9761d3e9 100644 --- a/apps/cms/contacts/models.py +++ b/apps/cms/contacts/models.py @@ -1,3 +1,4 @@ +from django.conf import settings from django.contrib import messages from django.db import models from django.shortcuts import redirect @@ -26,14 +27,15 @@ class FormField(AbstractFormField): class WagtailCaptchaFormBuilder(FormBuilder): @property def formfields(self): - # Add captcha to formfields property fields = super().formfields - fields["captcha"] = CaptcheckCaptchaField( - label=_("I am not a robot"), - help_text=_( - "If you are having difficulty please contact" "us, details adjacent" - ), - ) + # Add captcha to formfields property if the URL exists in settings + if settings.CAPTCHA_URL: + fields["captcha"] = CaptcheckCaptchaField( + label=_("I am not a robot"), + help_text=_( + "If you are having difficulty please contact" "us, details adjacent" + ), + ) return fields diff --git a/apps/users/forms.py b/apps/users/forms.py index fcca11eefd..f800bb58b1 100644 --- a/apps/users/forms.py +++ b/apps/users/forms.py @@ -57,7 +57,7 @@ def __init__(self, *args, **kwargs): self.fields["email"].widget.attrs["autocomplete"] = "username" self.fields["password1"].widget.attrs["autocomplete"] = "new-password" self.fields["password2"].widget.attrs["autocomplete"] = "new-password" - if not hasattr(settings, "CAPTCHA_URL") or not settings.CAPTCHA_URL: + if not (hasattr(settings, "CAPTCHA_URL") and settings.CAPTCHA_URL): del self.fields["captcha"] else: self.fields["captcha"].help_text = helpers.add_email_link_to_helptext( diff --git a/tests/users/test_signup.py b/tests/users/test_signup.py index 9dfdb754fe..d7b89584ad 100644 --- a/tests/users/test_signup.py +++ b/tests/users/test_signup.py @@ -80,26 +80,6 @@ def test_signup_user_without_captcha(client): assert user.get_newsletters -@override_settings() -@pytest.mark.django_db -def test_signup_user_when_captcha_is_none(client): - settings.CAPTCHA_URL = None - resp = client.post( - reverse("account_signup"), - { - "username": "dauser", - "email": "mail@example.com", - "get_newsletters": "on", - "password1": "password", - "password2": "password", - "terms_of_use": "on", - }, - ) - assert resp.status_code == 302 - user = User.objects.get() - assert user.get_newsletters - - @override_settings() @pytest.mark.django_db def test_signup_user_when_not_captcha(client):