Skip to content

Commit

Permalink
apps/cms/contacts: add captcha to contact form if URL exists
Browse files Browse the repository at this point in the history
  • Loading branch information
m4ra committed Jul 5, 2023
1 parent db29abc commit 6e4f1a1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 28 deletions.
16 changes: 9 additions & 7 deletions apps/cms/contacts/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.contrib import messages
from django.db import models
from django.shortcuts import redirect
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion apps/users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
20 changes: 0 additions & 20 deletions tests/users/test_signup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 6e4f1a1

Please sign in to comment.