Skip to content

Commit

Permalink
Username case insensitive and username to lowercase (#251)
Browse files Browse the repository at this point in the history
* [Add] check username case insensitive

* [Fix] Variable bad defined

* [Update] Change error handler

* [Update] Change check in save by clean_username

* [Fix] Return username after clean

* [Add] Clear code

* [Fix] Save username in lowercase

* [Update] transaltions files

* [Update] Username exits query

* [Add] username to lowercase

* [Add] check username case insensitive

* [Fix] Variable bad defined

* [Update] Change error handler

* [Update] Change check in save by clean_username

* [Fix] Return username after clean

* [Add] Clear code

* [Fix] Save username in lowercase

* [Update] transaltions files

* [Update] Username exits query

* [Add] username to lowercase

* [Fix] Indentation

* [Fix] locale checkout from master

* [Fix] Typo error in exception raise

* [Fix] Reorder lower()
[Fix] alphabetize imports

* [Update] change to new registration form with username lowercase
[Add] Test for RegistrationFormUsernameLowercase

* [Add] new form username to lower case to docs

* [Fix] RegistrationFormUniqueEmail in docs

* [Add] documentation in right place

* [Remove] removed docs from docs directory

* [Fix] Indentantion and white spaces

* [Fix] failed attribute in check if username exits

* [Fix] Filter with username customizable field

* [Fix] Removed white space after `{`
  • Loading branch information
pando85 authored and joshblum committed Apr 25, 2017
1 parent 99b31c4 commit e689b22
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
16 changes: 15 additions & 1 deletion registration/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@


from django import forms
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.forms import UserCreationForm
from django.utils.translation import ugettext_lazy as _

from .users import UserModel, UsernameField

Expand Down Expand Up @@ -40,6 +40,20 @@ class Meta:
fields = (UsernameField(), "email")


class RegistrationFormUsernameLowercase(RegistrationForm):
"""
A subclass of :class:`RegistrationForm` which enforces unique case insensitive
usernames, make all usernames to lower case.
"""
def clean_username(self):
username = self.cleaned_data.get('username', '').lower()
if User.objects.filter(**{UsernameField(): username}).exists():
raise forms.ValidationError(_('A user with that username already exists.'))

return username


class RegistrationFormTermsOfService(RegistrationForm):
"""
Subclass of ``RegistrationForm`` which adds a required checkbox
Expand Down
24 changes: 24 additions & 0 deletions registration/tests/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,30 @@ def test_registration_form(self):
'password2': 'foo'})
self.failUnless(form.is_valid())

def test_registration_form_username_lowercase(self):
"""
Test that ``RegistrationFormUniqueEmail`` validates uniqueness
of email addresses.
"""
# Create a user so we can verify that duplicate addresses
# aren't permitted.
UserModel().objects.create_user('alice', '[email protected]', 'secret')

form = forms.RegistrationFormUsernameLowercase(data={'username': 'Alice',
'email': '[email protected]',
'password1': 'foo',
'password2': 'foo'})
self.failIf(form.is_valid())
self.assertEqual(form.errors['username'],
["A user with that username already exists."])

form = forms.RegistrationFormUsernameLowercase(data={'username': 'foo',
'email': '[email protected]',
'password1': 'foo',
'password2': 'foo'})
self.failUnless(form.is_valid())

def test_registration_form_tos(self):
"""
Test that ``RegistrationFormTermsOfService`` requires
Expand Down

0 comments on commit e689b22

Please sign in to comment.