diff bio/forms.py @ 612:5be850a66dfc

For BB issue 17, validate timezone values when saving user profiles.
author Brian Neal <bgneal@gmail.com>
date Sun, 12 Aug 2012 09:34:56 -0500
parents 678a1a2ef55a
children 4f265f61874b
line wrap: on
line diff
--- a/bio/forms.py	Sat Aug 04 14:01:33 2012 -0500
+++ b/bio/forms.py	Sun Aug 12 09:34:56 2012 -0500
@@ -11,6 +11,8 @@
 from django.core.files.base import ContentFile
 from django.contrib.auth.models import User
 
+import pytz
+
 from bio.models import UserProfile
 from core.widgets import AutoCompleteUserInput
 from core.image import parse_image, downscale_image_square
@@ -55,6 +57,21 @@
             settings.GPP_THIRD_PARTY_JS['jquery-ui'] +
             ['js/bio.js', 'js/timezone.js'])
 
+    def clean_time_zone(self):
+        """Ensure the timezone is valid and will work with pytz.
+        
+        A blank (empty) value is allowed.
+        """
+
+        tz = self.cleaned_data['time_zone'].strip()
+        if tz:
+            try:
+                pytz.timezone(tz)
+            except pytz.UnknownTimeZoneError:
+                raise forms.ValidationError('Invalid timezone')
+
+        return tz
+
 
 class UploadAvatarForm(forms.Form):
     """Form used to change a user's avatar"""