changeset 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 8b9fc7487222
children 0729c73d5761
files bio/forms.py bio/tests/__init__.py bio/tests/form_tests.py
diffstat 3 files changed, 46 insertions(+), 0 deletions(-) [+]
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"""
--- a/bio/tests/__init__.py	Sat Aug 04 14:01:33 2012 -0500
+++ b/bio/tests/__init__.py	Sun Aug 12 09:34:56 2012 -0500
@@ -1,1 +1,2 @@
 from view_tests import *
+from form_tests import *
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bio/tests/form_tests.py	Sun Aug 12 09:34:56 2012 -0500
@@ -0,0 +1,28 @@
+"""
+Form tests for the bio application.
+
+"""
+from django.test import TestCase
+
+from bio.forms import EditUserProfileForm
+
+
+class EditUserProfileFormTestCase(TestCase):
+
+    def test_valid_timezone(self):
+        
+        post_data = {'time_zone': 'US/Central'}
+        form = EditUserProfileForm(post_data)
+        self.assertTrue(form.is_valid())
+
+    def test_invalid_timezone(self):
+        
+        post_data = {'time_zone': u'Am\xe9rica/Argentina_/_Buenos_Aires'}
+        form = EditUserProfileForm(post_data)
+        self.assertFalse(form.is_valid())
+
+    def test_blank_timezone(self):
+        
+        form = EditUserProfileForm({})
+        self.assertTrue(form.is_valid())
+