diff 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
line wrap: on
line diff
--- a/email_list/forms.py	Tue Oct 15 21:35:48 2013 -0500
+++ b/email_list/forms.py	Wed Oct 16 20:32:58 2013 -0500
@@ -18,10 +18,20 @@
 
 
 class SubscriberForm(forms.Form):
-    name = forms.CharField(max_length=64, required=False)
-    email = forms.EmailField()
-    location = forms.CharField(max_length=64, required=False)
-    option = forms.ChoiceField(choices=SUBSCRIBE_OPTS)
+    name = forms.CharField(required=False,
+            widget=forms.TextInput(attrs={
+                    'class': 'form-control',
+                    'placeholder': 'Your name (optional)'}))
+    email = forms.EmailField(widget=forms.TextInput(attrs={
+                    'class': 'form-control',
+                    'type': 'email',
+                    'placeholder': 'Your email address'}))
+    location = forms.CharField(required=False,
+            widget=forms.TextInput(attrs={
+                     'class': 'form-control',
+                     'placeholder': 'City, State (optional)'}))
+    option = forms.ChoiceField(choices=SUBSCRIBE_OPTS,
+            widget=forms.Select(attrs={'class': 'form-control'}))
 
     def clean(self):
         """
@@ -29,16 +39,21 @@
         a validation error if not.
 
         """
-        email = self.cleaned_data['email']
+        cleaned_data = super(SubscriberForm, self).clean()
+        email = cleaned_data.get('email')
+        option = cleaned_data.get('option')
 
-        if self.cleaned_data['option'] == 'sub':
+        if not email or not option:
+            return cleaned_data
+
+        if option == 'sub':
             # is the user already subscribed (active)?
             try:
                 subscriber = Subscriber.objects.get(email=email)
             except Subscriber.DoesNotExist:
                 subscriber = Subscriber(email=email,
-                        name=self.cleaned_data['name'],
-                        location=self.cleaned_data['location'])
+                        name=cleaned_data['name'],
+                        location=cleaned_data['location'])
             else:
                 if subscriber.is_active():
                     raise forms.ValidationError(ALREADY_SUBSCRIBED)
@@ -52,7 +67,7 @@
         # save the subscriber away for a future process() call
         self.subscriber = subscriber
 
-        return self.cleaned_data
+        return cleaned_data
 
     def is_subscribe(self):
         """
@@ -65,7 +80,7 @@
     def process(self):
         """
         Call this function if is_valid() returns True. It carries out the user's
-        subscription request. 
+        subscription request.
 
         """
         if self.is_subscribe():
@@ -121,8 +136,8 @@
         email_template = 'email_list/email_unsubscribe.txt'
 
     msg = render_to_string(email_template, {
-        'band': band, 
-        'url': url, 
+        'band': band,
+        'url': url,
         'band_domain': config['BAND_DOMAIN'],
         })