Regenerate username if it's empty after slugification

This commit is contained in:
Kumar McMillan 2011-12-14 15:23:45 -06:00
Родитель 4de033a176
Коммит cf60837045
2 изменённых файлов: 6 добавлений и 2 удалений

Просмотреть файл

@ -31,6 +31,10 @@ class TestAutoCreateUsername(amo.tests.TestCase):
eq_(autocreate_username('testaccount+slug'),
'testaccountslug')
def test_empty_username_is_a_random_hash(self):
un = autocreate_username('.+') # this shouldn't happen but it could!
assert len(un) and not un.startswith('.+'), 'Unexpected: %s' % un
def test_blacklisted(self):
BlacklistedUsername.objects.create(username='firefox')
un = autocreate_username('firefox')

Просмотреть файл

@ -113,9 +113,9 @@ def autocreate_username(candidate, tries=1):
adjusted_u = make_u(candidate)
if tries > 1:
adjusted_u = '%s%s' % (adjusted_u, tries)
if (BlacklistedUsername.blocked(adjusted_u)
if (BlacklistedUsername.blocked(adjusted_u) or adjusted_u == ''
or tries > max_tries or len(adjusted_u) > 255):
log.info('username blocked, max tries reached, or too long;'
log.info('username blocked, empty, max tries reached, or too long;'
' username=%s; max=%s' % (adjusted_u, max_tries))
return autocreate_username(uuid.uuid4().hex[0:15])
if UserProfile.objects.filter(username=adjusted_u).count():