Mercurial > public > sg101
comparison contact/forms.py @ 819:38db6ec61af3
Contact form can prepopulate subject field now.
Also updated code for PEP-8 and added tests.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 20 Sep 2014 14:05:52 -0500 |
parents | ee87ea74d46b |
children | 79a71b9d0a2a |
comparison
equal
deleted
inserted
replaced
818:cf486a8e8b43 | 819:38db6ec61af3 |
---|---|
6 from django.contrib.sites.models import Site | 6 from django.contrib.sites.models import Site |
7 from core.functions import send_mail | 7 from core.functions import send_mail |
8 | 8 |
9 | 9 |
10 class ContactForm(forms.Form): | 10 class ContactForm(forms.Form): |
11 """Form used to contact the website admins""" | 11 """Form used to contact the website admins""" |
12 name = forms.CharField(label = "Your Name", max_length = 61, | 12 name=forms.CharField(label="Your Name", max_length=61, |
13 widget = forms.TextInput(attrs = {'size' : 50 })) | 13 widget=forms.TextInput(attrs={'size': 50 })) |
14 email = forms.EmailField(label = "Your Email", | 14 email=forms.EmailField(label="Your Email", |
15 widget = forms.TextInput(attrs = {'size' : 50 })) | 15 widget=forms.TextInput(attrs={'size': 50 })) |
16 subject = forms.CharField(max_length = 64, | 16 subject=forms.CharField(max_length=64, |
17 widget = forms.TextInput(attrs = {'size' : 50 })) | 17 widget=forms.TextInput(attrs={'size': 50 })) |
18 honeypot = forms.CharField(max_length = 64, required = False, | 18 honeypot=forms.CharField(max_length=64, required=False, |
19 label = 'If you enter anything in this field your message will be treated as spam') | 19 label='If you enter anything in this field your message will be treated as spam') |
20 message = forms.CharField(label = "Your Message", | 20 message=forms.CharField(label="Your Message", |
21 widget = forms.Textarea(attrs = {'rows' : 16, 'cols' : 50}), | 21 widget=forms.Textarea(attrs={'rows': 16, 'cols': 50}), |
22 max_length = 3000) | 22 max_length=3000) |
23 | 23 |
24 recipient_list = [mail_tuple[1] for mail_tuple in settings.MANAGERS] | 24 recipient_list = [mail_tuple[1] for mail_tuple in settings.MANAGERS] |
25 | 25 |
26 def clean_honeypot(self): | 26 def clean_honeypot(self): |
27 value = self.cleaned_data['honeypot'] | 27 value = self.cleaned_data['honeypot'] |
28 if value: | 28 if value: |
29 raise forms.ValidationError(self.fields['honeypot'].label) | 29 raise forms.ValidationError(self.fields['honeypot'].label) |
30 return value | 30 return value |
31 | 31 |
32 def save(self): | 32 def save(self): |
33 # Send the feedback message email | 33 # Send the feedback message email |
34 | 34 |
35 site = Site.objects.get_current() | 35 site = Site.objects.get_current() |
36 | 36 |
37 msg = render_to_string('contact/contact_email.txt', | 37 msg = render_to_string('contact/contact_email.txt', { |
38 { | 38 'site_name': site.name, |
39 'site_name' : site.name, | 39 'user_name': self.cleaned_data['name'], |
40 'user_name' : self.cleaned_data['name'], | 40 'user_email': self.cleaned_data['email'], |
41 'user_email' : self.cleaned_data['email'], | 41 'message': self.cleaned_data['message'], |
42 'message' : self.cleaned_data['message'], | 42 }) |
43 }) | |
44 | 43 |
45 subject = site.name + ' Feedback: ' + self.cleaned_data['subject'] | 44 subject = site.name + ' Feedback: ' + self.cleaned_data['subject'] |
46 send_mail(subject, msg, self.cleaned_data['email'], self.recipient_list) | 45 send_mail(subject, msg, self.cleaned_data['email'], self.recipient_list) |
47 |