# HG changeset patch # User Brian Neal # Date 1344782096 18000 # Node ID 5be850a66dfcdc59c67075b37528a173a4dbbd3d # Parent 8b9fc74872224b182d9b37b57021d004541a5da1 For BB issue 17, validate timezone values when saving user profiles. diff -r 8b9fc7487222 -r 5be850a66dfc bio/forms.py --- 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""" diff -r 8b9fc7487222 -r 5be850a66dfc bio/tests/__init__.py --- 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 * diff -r 8b9fc7487222 -r 5be850a66dfc bio/tests/form_tests.py --- /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()) +