Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1559: Refactor contact form to use built-in Akismet functionality #1865

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 5 additions & 37 deletions contact/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
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 ( # Use AkismetContactForm instead of ContactForm
AkismetContactForm,
)
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(
Expand Down Expand Up @@ -43,40 +44,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):
Expand Down