From 8c238ccbc3961c5abd67ed0b42a13b9c0d9c7a42 Mon Sep 17 00:00:00 2001 From: Jayprajapati19 Date: Tue, 7 Jan 2025 23:58:56 +0530 Subject: [PATCH 1/2] Refactor contact form to use built-in Akismet functionality --- contact/forms.py | 43 +++---------------------------------------- 1 file changed, 3 insertions(+), 40 deletions(-) diff --git a/contact/forms.py b/contact/forms.py index b68211608..648240883 100644 --- a/contact/forms.py +++ b/contact/forms.py @@ -1,20 +1,17 @@ import logging - import django from django import forms from django.conf import settings from django.contrib.sites.models import Site from django.utils.encoding import force_bytes from django.utils.translation import gettext_lazy as _ -from django_contact_form.forms import ContactForm +from django_contact_form.forms import AkismetContactForm # Use AkismetContactForm instead of ContactForm from django_recaptcha.fields import ReCaptchaField from django_recaptcha.widgets import ReCaptchaV3 -from pykismet3 import Akismet, AkismetServerError logger = logging.getLogger(__name__) - -class BaseContactForm(ContactForm): +class BaseContactForm(AkismetContactForm): # Inherit from AkismetContactForm message_subject = forms.CharField( max_length=100, widget=forms.TextInput( @@ -43,41 +40,7 @@ def subject(self): def message(self): return "From: {name} <{email}>\n\n{body}".format(**self.cleaned_data) - def clean_body(self): - """ - Check spam against Akismet. - - Backported from django-contact-form pre-1.0; 1.0 dropped built-in - Akismet support. - """ - if "body" in self.cleaned_data and getattr(settings, "AKISMET_API_KEY", None): - try: - akismet_api = Akismet( - api_key=settings.AKISMET_API_KEY, - blog_url="http://%s/" % Site.objects.get_current().domain, - user_agent="Django {}.{}.{}".format(*django.VERSION), - ) - - akismet_data = { - "user_ip": self.request.META.get("REMOTE_ADDR", ""), - "user_agent": self.request.headers.get("user-agent", ""), - "referrer": self.request.headers.get("referer", ""), - "comment_content": force_bytes(self.cleaned_data["body"]), - "comment_author": self.cleaned_data.get("name", ""), - "comment_author_email": self.cleaned_data.get("email", ""), - "comment_type": "contact-form", - } - if getattr(settings, "AKISMET_TESTING", None): - # Adding test argument to the request in order to - # tell akismet that they should ignore the request - # so that test runs affect the heuristics - akismet_data["test"] = 1 - if akismet_api.check(akismet_data): - raise forms.ValidationError("Akismet thinks this message is spam") - except AkismetServerError: - logger.error("Akismet server error") - return self.cleaned_data["body"] - + # Remove the custom clean_body() method. The spam check is now handled by AkismetContactForm class FoundationContactForm(BaseContactForm): recipient_list = ["dsf-board@googlegroups.com"] From 3dd4725cb243002d875da3bbad01910d027d88d2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 7 Jan 2025 18:32:22 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- contact/forms.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/contact/forms.py b/contact/forms.py index 648240883..637a068eb 100644 --- a/contact/forms.py +++ b/contact/forms.py @@ -1,16 +1,20 @@ import logging + import django from django import forms from django.conf import settings from django.contrib.sites.models import Site from django.utils.encoding import force_bytes from django.utils.translation import gettext_lazy as _ -from django_contact_form.forms import AkismetContactForm # Use AkismetContactForm instead of ContactForm +from django_contact_form.forms import ( # Use AkismetContactForm instead of ContactForm + AkismetContactForm, +) from django_recaptcha.fields import ReCaptchaField from django_recaptcha.widgets import ReCaptchaV3 logger = logging.getLogger(__name__) + class BaseContactForm(AkismetContactForm): # Inherit from AkismetContactForm message_subject = forms.CharField( max_length=100, @@ -42,5 +46,6 @@ def message(self): # Remove the custom clean_body() method. The spam check is now handled by AkismetContactForm + class FoundationContactForm(BaseContactForm): recipient_list = ["dsf-board@googlegroups.com"]