Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updated django-contact-form to 1.0.

  • Loading branch information...
commit 05000184adfb8edae8e5b93a020f840a11fba34c 1 parent c59477f
@jacobian jacobian authored
View
39 contact/forms.py
@@ -1,9 +1,12 @@
+from akismet import Akismet
from django import forms
-from contact_form.forms import AkismetContactForm
+from django.conf import settings
+from django.contrib.sites.models import Site
+from contact_form.forms import ContactForm
attrs = {'class': 'required'}
-class BaseContactForm(AkismetContactForm):
+class BaseContactForm(ContactForm):
message_subject = forms.CharField(max_length=100, widget=forms.TextInput(attrs=attrs), label=u'Message subject')
def subject(self):
@@ -12,18 +15,24 @@ def subject(self):
def message(self):
return u"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 hasattr(settings, 'AKISMET_API_KEY') and settings.AKISMET_API_KEY:
+ akismet_api = Akismet(key=settings.AKISMET_API_KEY,
+ blog_url='http://%s/' % Site.objects.get_current().domain)
+ if akismet_api.verify_key():
+ akismet_data = {'comment_type': 'comment',
+ 'referer': self.request.META.get('HTTP_REFERER', ''),
+ 'user_ip': self.request.META.get('REMOTE_ADDR', ''),
+ 'user_agent': self.request.META.get('HTTP_USER_AGENT', '')}
+ if akismet_api.comment_check(self.cleaned_data['body'], data=akismet_data, build_data=True):
+ raise forms.ValidationError(u"Akismet thinks this message is spam")
+ return self.cleaned_data['body']
+
class FoundationContactForm(BaseContactForm):
recipient_list = ["dsf-board@googlegroups.com"]
-
-class CoCFeedbackForm(BaseContactForm):
- recipient_list = ['alex+django-coc@djangoproject.com',
- 'jacob+django-coc@jacobian.org']
-
- def __init__(self, *args, **kwargs):
- super(BaseContactForm, self).__init__(*args, **kwargs)
- del self.fields['message_subject']
- self.fields['name'].required = False
- self.fields['email'].required = False
-
- def subject(self):
- return "Django Code of Conduct feedback"
View
19 contact/tests.py
@@ -14,22 +14,3 @@ def test_foundation_contact(self):
resp = self.client.post('/contact/foundation/', data)
self.assertRedirects(resp, '/contact/sent/')
self.assertEqual(mail.outbox[-1].subject, '[Contact form] Hello')
-
- def test_coc_contact(self):
- data = {
- 'name': 'A. Random Hacker',
- 'email': 'a.random@example.com',
- 'body': 'Hello, World'
- }
- resp = self.client.post('/contact/code-of-conduct/', data)
- self.assertRedirects(resp, '/conduct/')
- self.assertEqual(mail.outbox[-1].subject, 'Django Code of Conduct feedback')
-
- def test_coc_contact_unicode(self):
- data = {
- 'name': 'A. Random Hacker',
- 'email': 'a.random@example.com',
- 'body': u'Hello, \u2603!'
- }
- resp = self.client.post('/contact/code-of-conduct/', data)
- self.assertRedirects(resp, '/conduct/')
View
5 contact/urls.py
@@ -3,10 +3,9 @@
from django.conf.urls import patterns, url
from django.views.generic import TemplateView
-from . import views
+from .views import ContactFoundation
urlpatterns = patterns('',
- url(r'^foundation/$', views.contact_foundation, name='contact_foundation'),
+ url(r'^foundation/$', ContactFoundation.as_view(), name='contact_foundation'),
url(r'^sent/$', TemplateView.as_view(template_name='contact/sent.html'), name='contact_form_sent'),
- url(r'^code-of-conduct/$', views.contact_coc, name='contact_coc'),
)
View
25 contact/views.py
@@ -1,21 +1,10 @@
-from django.contrib import messages
-
from django.core import urlresolvers
-from django.http import HttpResponseRedirect
-from contact_form.views import contact_form
-from .forms import FoundationContactForm, CoCFeedbackForm
+from contact_form.views import ContactFormView
+from .forms import FoundationContactForm
-def contact_foundation(request):
- return contact_form(request,
- form_class = FoundationContactForm,
- template_name = 'contact/foundation.html',
- success_url = urlresolvers.reverse('contact_form_sent'))
+class ContactFoundation(ContactFormView):
+ form_class = FoundationContactForm
+ template_name = 'contact/foundation.html'
-def contact_coc(request):
- resp = contact_form(request,
- form_class = CoCFeedbackForm,
- template_name = 'contact/coc.html',
- success_url = urlresolvers.reverse('code_of_conduct'))
- if isinstance(resp, HttpResponseRedirect):
- messages.success(request, "Thanks for your feedback! If you provided an email address, we'll get back to you.")
- return resp
+ def get_success_url(self):
+ return urlresolvers.reverse('contact_form_sent')
View
3  deploy-requirements.txt
@@ -2,8 +2,7 @@
akismet == 0.2.0
Django == 1.5.2
-# django-contact-form doesn't have a release compatible with Django 1.2.
-http://bitbucket.org/ubernostrum/django-contact-form/get/tip.bz2
+django-contact-form == 1.0
django-haystack == 1.2.7
django-push == 0.4
django-registration==1.0

0 comments on commit 0500018

Please sign in to comment.
Something went wrong with that request. Please try again.