diff --git a/pyconde/accounts/forms.py b/pyconde/accounts/forms.py index d1aa805..2620f74 100644 --- a/pyconde/accounts/forms.py +++ b/pyconde/accounts/forms.py @@ -49,8 +49,7 @@ class ProfileRegistrationForm(RegistrationForm): help_text=Profile()._meta.get_field_by_name('avatar')[0].help_text) short_info = forms.CharField(label=_("short info"), widget=forms.Textarea, required=False) organisation = forms.CharField(label=_("Organisation"), required=False) - twitter = forms.CharField(label=_("Twitter"), required=False, - validators=[validators.twitter_username]) + twitter = forms.CharField(label=_("Twitter"), required=False) website = forms.URLField(label=_("Website"), required=False) num_accompanying_children = forms.IntegerField(required=False, label=_('Number of accompanying children'), @@ -119,6 +118,15 @@ def save_profile(self, new_user, *args, **kwargs): accept_ep_conferences=self.cleaned_data['accept_ep_conferences'] ) + def clean_twitter(self): + """ + Allow the user to enter either "@foo" or "foo" as their twitter handle. + """ + value = self.cleaned_data.get('twitter', '') + value = value.lstrip('@') + validators.twitter_username(value) # validates the max_length + return value + class AuthenticationForm(auth_forms.AuthenticationForm): """ diff --git a/pyconde/accounts/tests.py b/pyconde/accounts/tests.py index f9bd941..169b575 100644 --- a/pyconde/accounts/tests.py +++ b/pyconde/accounts/tests.py @@ -37,7 +37,7 @@ def test_no_display_name_given(self): self.assertEquals("username", account_tags.display_name(user)) -class AddressedAsFilterTest(TransactionTestCase): +class AddressedAsFilterTests(TransactionTestCase): def test_empty_parameter(self): self.assertIsNone(account_tags.addressed_as(None)) @@ -178,3 +178,42 @@ def test_start_with_at(self): def test_too_long(self): with self.assertRaises(ValidationError): validators.twitter_username("test test test t") + + +class ProfileRegistrationFormTests(TestCase): + def _required_data(self, **kwargs): + """ + Build a dict of required data for the ProfileRegistrationForm and + update it with the given kwargs. + """ + required = { + 'username': 'foo', + 'password': 'foo', + 'password_repeat': 'foo', + 'display_name': 'foo', + 'email': 'foo@example.com', + 'accept_privacy_policy': '1', + } + required.update(kwargs) + return required + + def test_twitter_handle_leading_at(self): + data = self._required_data(twitter='@foo') + f = forms.ProfileRegistrationForm(data) + self.assertTrue(f.is_valid()) + self.assertEqual(f.cleaned_data['twitter'], 'foo') + + def test_twitter_handle_too_long(self): + handle = 16 * 'a' + data = self._required_data(twitter=handle) + f = forms.ProfileRegistrationForm(data) + self.assertFalse(f.is_valid()) + errors = f['twitter'].errors + self.assertEqual(len(errors), 1) + self.assertEqual(errors[0], 'Twitter usernames have only 15 characters or less') + + def test_twitter_handle_15_chars_with_leading_at(self): + handle = '@' + 15 * 'a' + data = self._required_data(twitter=handle) + f = forms.ProfileRegistrationForm(data) + self.assertTrue(f.is_valid())