Mercurial > public > madeira
comparison email_list/forms.py @ 101:0a8942306b04
Bootstrap work for mailing list page.
Also corrected form cleaning logic if fields aren't provided.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 16 Oct 2013 20:32:58 -0500 |
parents | e2868ad47a1e |
children |
comparison
equal
deleted
inserted
replaced
100:e221c38edf40 | 101:0a8942306b04 |
---|---|
16 ALREADY_SUBSCRIBED = "This email address is already subscribed." | 16 ALREADY_SUBSCRIBED = "This email address is already subscribed." |
17 NOT_SUBSCRIBED = "This email address is not on our list." | 17 NOT_SUBSCRIBED = "This email address is not on our list." |
18 | 18 |
19 | 19 |
20 class SubscriberForm(forms.Form): | 20 class SubscriberForm(forms.Form): |
21 name = forms.CharField(max_length=64, required=False) | 21 name = forms.CharField(required=False, |
22 email = forms.EmailField() | 22 widget=forms.TextInput(attrs={ |
23 location = forms.CharField(max_length=64, required=False) | 23 'class': 'form-control', |
24 option = forms.ChoiceField(choices=SUBSCRIBE_OPTS) | 24 'placeholder': 'Your name (optional)'})) |
25 email = forms.EmailField(widget=forms.TextInput(attrs={ | |
26 'class': 'form-control', | |
27 'type': 'email', | |
28 'placeholder': 'Your email address'})) | |
29 location = forms.CharField(required=False, | |
30 widget=forms.TextInput(attrs={ | |
31 'class': 'form-control', | |
32 'placeholder': 'City, State (optional)'})) | |
33 option = forms.ChoiceField(choices=SUBSCRIBE_OPTS, | |
34 widget=forms.Select(attrs={'class': 'form-control'})) | |
25 | 35 |
26 def clean(self): | 36 def clean(self): |
27 """ | 37 """ |
28 This method ensures the appropriate action can be carried out and raises | 38 This method ensures the appropriate action can be carried out and raises |
29 a validation error if not. | 39 a validation error if not. |
30 | 40 |
31 """ | 41 """ |
32 email = self.cleaned_data['email'] | 42 cleaned_data = super(SubscriberForm, self).clean() |
43 email = cleaned_data.get('email') | |
44 option = cleaned_data.get('option') | |
33 | 45 |
34 if self.cleaned_data['option'] == 'sub': | 46 if not email or not option: |
47 return cleaned_data | |
48 | |
49 if option == 'sub': | |
35 # is the user already subscribed (active)? | 50 # is the user already subscribed (active)? |
36 try: | 51 try: |
37 subscriber = Subscriber.objects.get(email=email) | 52 subscriber = Subscriber.objects.get(email=email) |
38 except Subscriber.DoesNotExist: | 53 except Subscriber.DoesNotExist: |
39 subscriber = Subscriber(email=email, | 54 subscriber = Subscriber(email=email, |
40 name=self.cleaned_data['name'], | 55 name=cleaned_data['name'], |
41 location=self.cleaned_data['location']) | 56 location=cleaned_data['location']) |
42 else: | 57 else: |
43 if subscriber.is_active(): | 58 if subscriber.is_active(): |
44 raise forms.ValidationError(ALREADY_SUBSCRIBED) | 59 raise forms.ValidationError(ALREADY_SUBSCRIBED) |
45 else: | 60 else: |
46 # is the user already unsubscribed or not subscribed? | 61 # is the user already unsubscribed or not subscribed? |
50 raise forms.ValidationError(NOT_SUBSCRIBED) | 65 raise forms.ValidationError(NOT_SUBSCRIBED) |
51 | 66 |
52 # save the subscriber away for a future process() call | 67 # save the subscriber away for a future process() call |
53 self.subscriber = subscriber | 68 self.subscriber = subscriber |
54 | 69 |
55 return self.cleaned_data | 70 return cleaned_data |
56 | 71 |
57 def is_subscribe(self): | 72 def is_subscribe(self): |
58 """ | 73 """ |
59 This function can be called after an is_valid() call to determine if the | 74 This function can be called after an is_valid() call to determine if the |
60 request was for a subscribe or unsubscribe. | 75 request was for a subscribe or unsubscribe. |
63 return self.cleaned_data['option'] == 'sub' | 78 return self.cleaned_data['option'] == 'sub' |
64 | 79 |
65 def process(self): | 80 def process(self): |
66 """ | 81 """ |
67 Call this function if is_valid() returns True. It carries out the user's | 82 Call this function if is_valid() returns True. It carries out the user's |
68 subscription request. | 83 subscription request. |
69 | 84 |
70 """ | 85 """ |
71 if self.is_subscribe(): | 86 if self.is_subscribe(): |
72 self.subscriber.set_pending() | 87 self.subscriber.set_pending() |
73 else: | 88 else: |
119 email_template = 'email_list/email_subscribe.txt' | 134 email_template = 'email_list/email_subscribe.txt' |
120 else: | 135 else: |
121 email_template = 'email_list/email_unsubscribe.txt' | 136 email_template = 'email_list/email_unsubscribe.txt' |
122 | 137 |
123 msg = render_to_string(email_template, { | 138 msg = render_to_string(email_template, { |
124 'band': band, | 139 'band': band, |
125 'url': url, | 140 'url': url, |
126 'band_domain': config['BAND_DOMAIN'], | 141 'band_domain': config['BAND_DOMAIN'], |
127 }) | 142 }) |
128 | 143 |
129 subject = "[%s] Mailing List Confirmation" % band | 144 subject = "[%s] Mailing List Confirmation" % band |
130 | 145 |