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