# HG changeset patch # User Brian Neal # Date 1411239952 18000 # Node ID 38db6ec61af37339261cdd88bfe79a5fbe6ae409 # Parent cf486a8e8b43d39d6ea34ff56aeb49c5cf583ceb Contact form can prepopulate subject field now. Also updated code for PEP-8 and added tests. diff -r cf486a8e8b43 -r 38db6ec61af3 contact/forms.py --- a/contact/forms.py Sat Sep 13 16:19:46 2014 -0500 +++ b/contact/forms.py Sat Sep 20 14:05:52 2014 -0500 @@ -8,40 +8,38 @@ class ContactForm(forms.Form): - """Form used to contact the website admins""" - name = forms.CharField(label = "Your Name", max_length = 61, - widget = forms.TextInput(attrs = {'size' : 50 })) - email = forms.EmailField(label = "Your Email", - widget = forms.TextInput(attrs = {'size' : 50 })) - subject = forms.CharField(max_length = 64, - widget = forms.TextInput(attrs = {'size' : 50 })) - honeypot = forms.CharField(max_length = 64, required = False, - label = 'If you enter anything in this field your message will be treated as spam') - message = forms.CharField(label = "Your Message", - widget = forms.Textarea(attrs = {'rows' : 16, 'cols' : 50}), - max_length = 3000) + """Form used to contact the website admins""" + name=forms.CharField(label="Your Name", max_length=61, + widget=forms.TextInput(attrs={'size': 50 })) + email=forms.EmailField(label="Your Email", + widget=forms.TextInput(attrs={'size': 50 })) + subject=forms.CharField(max_length=64, + widget=forms.TextInput(attrs={'size': 50 })) + honeypot=forms.CharField(max_length=64, required=False, + label='If you enter anything in this field your message will be treated as spam') + message=forms.CharField(label="Your Message", + widget=forms.Textarea(attrs={'rows': 16, 'cols': 50}), + max_length=3000) - recipient_list = [mail_tuple[1] for mail_tuple in settings.MANAGERS] + recipient_list = [mail_tuple[1] for mail_tuple in settings.MANAGERS] - def clean_honeypot(self): - value = self.cleaned_data['honeypot'] - if value: - raise forms.ValidationError(self.fields['honeypot'].label) - return value + def clean_honeypot(self): + value = self.cleaned_data['honeypot'] + if value: + raise forms.ValidationError(self.fields['honeypot'].label) + return value - def save(self): - # Send the feedback message email + def save(self): + # Send the feedback message email - site = Site.objects.get_current() + site = Site.objects.get_current() - msg = render_to_string('contact/contact_email.txt', - { - 'site_name' : site.name, - 'user_name' : self.cleaned_data['name'], - 'user_email' : self.cleaned_data['email'], - 'message' : self.cleaned_data['message'], - }) + msg = render_to_string('contact/contact_email.txt', { + 'site_name': site.name, + 'user_name': self.cleaned_data['name'], + 'user_email': self.cleaned_data['email'], + 'message': self.cleaned_data['message'], + }) - subject = site.name + ' Feedback: ' + self.cleaned_data['subject'] - send_mail(subject, msg, self.cleaned_data['email'], self.recipient_list) - + subject = site.name + ' Feedback: ' + self.cleaned_data['subject'] + send_mail(subject, msg, self.cleaned_data['email'], self.recipient_list) diff -r cf486a8e8b43 -r 38db6ec61af3 contact/tests/test_views.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/contact/tests/test_views.py Sat Sep 20 14:05:52 2014 -0500 @@ -0,0 +1,49 @@ +""" +Unit tests for the contact application views. + +""" +from django.test import TestCase +from django.core.urlresolvers import reverse +from django.core import mail + + +class BaseTestCase(TestCase): + """Simple tests to ensure basic functionality.""" + + def test_usage(self): + url = reverse('contact-form') + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + + post_data = { + 'name': 'John Doe', + 'email': 'jdoe@example.com', + 'subject': 'Test message', + 'message': 'Testing contact form.', + } + response = self.client.post(url, data=post_data, follow=True) + self.assertRedirects(response, reverse('contact-thanks')) + + self.assertEqual(len(mail.outbox), 1) + email = mail.outbox[0] + self.assertEqual(len(email.recipients()), 1) + self.assertEqual(email.from_email, post_data['email']) + self.assertEqual(email.recipients()[0], 'admin@surfguitar101.com') + self.assertTrue(post_data['subject'] in email.subject) + msg = email.message().as_string() + self.assertTrue(post_data['name'] in msg) + self.assertTrue(post_data['email'] in msg) + self.assertTrue(post_data['message'] in msg) + + def test_honeypot(self): + url = reverse('contact-form') + post_data = { + 'name': 'John Doe', + 'email': 'jdoe@example.com', + 'subject': 'Test message', + 'message': 'Testing contact form.', + 'honeypot': 'some spam', + } + response = self.client.post(url, data=post_data) + self.assertEqual(response.status_code, 200) + self.assertEqual(len(mail.outbox), 0) diff -r cf486a8e8b43 -r 38db6ec61af3 contact/urls.py --- a/contact/urls.py Sat Sep 13 16:19:46 2014 -0500 +++ b/contact/urls.py Sat Sep 20 14:05:52 2014 -0500 @@ -1,7 +1,10 @@ """urls for the contact application""" from django.conf.urls import patterns, url +from django.views.generic import TemplateView -urlpatterns = patterns('contact.views', - url(r'^$', 'contact_form', name='contact-form'), - (r'^thanks/$', 'contact_thanks'), +urlpatterns = patterns('', + url(r'^$', 'contact.views.contact_form', name='contact-form'), + url(r'^thanks/$', + TemplateView.as_view(template_name='contact/contact_thanks.html'), + name='contact-thanks'), ) diff -r cf486a8e8b43 -r 38db6ec61af3 contact/views.py --- a/contact/views.py Sat Sep 13 16:19:46 2014 -0500 +++ b/contact/views.py Sat Sep 20 14:05:52 2014 -0500 @@ -1,33 +1,27 @@ -# Create your views here. +"""Views for the contact application.""" -from django.shortcuts import render_to_response -from django.template import RequestContext -from django.http import HttpResponseRedirect -from django.core.urlresolvers import reverse +from django.shortcuts import redirect, render from contact.forms import ContactForm from core.functions import get_full_name def contact_form(request): - if request.method == 'POST': - form = ContactForm(request.POST) - if form.is_valid(): - form.save() - return HttpResponseRedirect(reverse('contact.views.contact_thanks')) - else: - initial_data = {} - if request.user.is_authenticated(): - name = get_full_name(request.user) - initial_data = {'name' : name, 'email' : request.user.email} + if request.method == 'POST': + form = ContactForm(request.POST) + if form.is_valid(): + form.save() + return redirect('contact-thanks') + else: + initial_data = {} + if request.user.is_authenticated(): + name = get_full_name(request.user) + initial_data = {'name': name, 'email': request.user.email} - form = ContactForm(initial = initial_data) + subject = request.GET.get('subject') + if subject: + initial_data['subject'] = subject - return render_to_response('contact/contact_form.html', - {'form' : form}, - context_instance = RequestContext(request)) + form = ContactForm(initial=initial_data) - -def contact_thanks(request): - return render_to_response('contact/contact_thanks.html', - context_instance = RequestContext(request)) + return render(request, 'contact/contact_form.html', {'form': form})