Skip to content
This repository has been archived by the owner on Jan 17, 2020. It is now read-only.

Commit

Permalink
Merge pull request #27 from bmispelon/registration-twitter
Browse files Browse the repository at this point in the history
Automatically strip leading @ from twitter handle during registration.
  • Loading branch information
MarkusH committed Jan 6, 2014
2 parents 741efe4 + 41cbd9b commit 5781480
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
12 changes: 10 additions & 2 deletions pyconde/accounts/forms.py
Expand Up @@ -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'),
Expand Down Expand Up @@ -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):
"""
Expand Down
41 changes: 40 additions & 1 deletion pyconde/accounts/tests.py
Expand Up @@ -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))

Expand Down Expand Up @@ -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())

0 comments on commit 5781480

Please sign in to comment.