Mercurial > public > madeira
comparison email_list/forms.py @ 71:e2868ad47a1e
For Django 1.4, using the new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 14 Apr 2012 16:40:29 -0500 |
parents | madeira/email_list/forms.py@9b40a8d300a4 |
children | 0a8942306b04 |
comparison
equal
deleted
inserted
replaced
70:f26cdda0ad8b | 71:e2868ad47a1e |
---|---|
1 """ | |
2 Forms for the email_list application. | |
3 | |
4 """ | |
5 from django import forms | |
6 from django.conf import settings | |
7 from django.core.urlresolvers import reverse | |
8 from django.core.mail import send_mail, send_mass_mail | |
9 from django.template.loader import render_to_string | |
10 | |
11 from email_list.models import Subscriber | |
12 | |
13 | |
14 SUBSCRIBE_OPTS = [('sub', 'Subscribe'), ('unsub', 'Unsubscribe')] | |
15 | |
16 ALREADY_SUBSCRIBED = "This email address is already subscribed." | |
17 NOT_SUBSCRIBED = "This email address is not on our list." | |
18 | |
19 | |
20 class SubscriberForm(forms.Form): | |
21 name = forms.CharField(max_length=64, required=False) | |
22 email = forms.EmailField() | |
23 location = forms.CharField(max_length=64, required=False) | |
24 option = forms.ChoiceField(choices=SUBSCRIBE_OPTS) | |
25 | |
26 def clean(self): | |
27 """ | |
28 This method ensures the appropriate action can be carried out and raises | |
29 a validation error if not. | |
30 | |
31 """ | |
32 email = self.cleaned_data['email'] | |
33 | |
34 if self.cleaned_data['option'] == 'sub': | |
35 # is the user already subscribed (active)? | |
36 try: | |
37 subscriber = Subscriber.objects.get(email=email) | |
38 except Subscriber.DoesNotExist: | |
39 subscriber = Subscriber(email=email, | |
40 name=self.cleaned_data['name'], | |
41 location=self.cleaned_data['location']) | |
42 else: | |
43 if subscriber.is_active(): | |
44 raise forms.ValidationError(ALREADY_SUBSCRIBED) | |
45 else: | |
46 # is the user already unsubscribed or not subscribed? | |
47 try: | |
48 subscriber = Subscriber.objects.get(email=email) | |
49 except Subscriber.DoesNotExist: | |
50 raise forms.ValidationError(NOT_SUBSCRIBED) | |
51 | |
52 # save the subscriber away for a future process() call | |
53 self.subscriber = subscriber | |
54 | |
55 return self.cleaned_data | |
56 | |
57 def is_subscribe(self): | |
58 """ | |
59 This function can be called after an is_valid() call to determine if the | |
60 request was for a subscribe or unsubscribe. | |
61 | |
62 """ | |
63 return self.cleaned_data['option'] == 'sub' | |
64 | |
65 def process(self): | |
66 """ | |
67 Call this function if is_valid() returns True. It carries out the user's | |
68 subscription request. | |
69 | |
70 """ | |
71 if self.is_subscribe(): | |
72 self.subscriber.set_pending() | |
73 else: | |
74 self.subscriber.set_leaving() | |
75 | |
76 self.subscriber.save() | |
77 send_email(self.subscriber) | |
78 | |
79 | |
80 class AdminEmailForm(forms.Form): | |
81 subject = forms.CharField(max_length=255, required=True, label='Subject:', | |
82 widget=forms.TextInput(attrs={'class': 'vTextField required', | |
83 'size': '120'})) | |
84 message = forms.CharField(label='Message:', | |
85 widget=forms.Textarea(attrs={'class': 'vLargeTextField required'})) | |
86 | |
87 def __init__(self, *args, **kwargs): | |
88 initial = kwargs.pop('initial', {}) | |
89 if 'subject' not in initial: | |
90 initial['subject'] = '[%s] ' % settings.BAND_CONFIG['BAND_NAME'] | |
91 kwargs['initial'] = initial | |
92 | |
93 super(AdminEmailForm, self).__init__(*args, **kwargs) | |
94 | |
95 def save(self): | |
96 """ | |
97 Call this function if is_valid() to send the mass email. | |
98 Returns the number of mails sent. | |
99 | |
100 """ | |
101 subject = self.cleaned_data['subject'] | |
102 message = self.cleaned_data['message'] | |
103 return send_mail_to_subscribers(subject, message) | |
104 | |
105 | |
106 def send_email(subscriber): | |
107 """ | |
108 This function sends out the appropriate email for the given subscriber. | |
109 | |
110 """ | |
111 config = settings.BAND_CONFIG | |
112 band = config['BAND_NAME'] | |
113 from_email = config['BAND_EMAIL'] | |
114 | |
115 url = "http://%s%s" % (config['BAND_DOMAIN'], | |
116 reverse('email_list-confirm', kwargs={'key': subscriber.key})) | |
117 | |
118 if subscriber.is_pending(): | |
119 email_template = 'email_list/email_subscribe.txt' | |
120 else: | |
121 email_template = 'email_list/email_unsubscribe.txt' | |
122 | |
123 msg = render_to_string(email_template, { | |
124 'band': band, | |
125 'url': url, | |
126 'band_domain': config['BAND_DOMAIN'], | |
127 }) | |
128 | |
129 subject = "[%s] Mailing List Confirmation" % band | |
130 | |
131 send_mail(subject, msg, from_email, [subscriber.email]) | |
132 | |
133 | |
134 def send_mail_to_subscribers(subject, message): | |
135 """ | |
136 Send an email to each mailing list subscriber with the given subject and | |
137 message. | |
138 Returns the number of messages sent. | |
139 | |
140 """ | |
141 config = settings.BAND_CONFIG | |
142 unsubscribe_url = "http://%s%s" % (config['BAND_DOMAIN'], | |
143 reverse('email_list-main')) | |
144 | |
145 msg = render_to_string('email_list/mailing_list.txt', { | |
146 'band': config['BAND_NAME'], | |
147 'unsubscribe_url': unsubscribe_url, | |
148 'message': message, | |
149 }) | |
150 | |
151 mail_data = [(subject, msg, config['BAND_EMAIL'], [subscriber]) for | |
152 subscriber in Subscriber.objects.values_list('email', flat=True)] | |
153 | |
154 send_mass_mail(mail_data) | |
155 return len(mail_data) |