From 9935f97cd203bdcc722bc3d4e96858e221d96ff8 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 22 Apr 2016 21:17:42 +0200 Subject: Refs #21379 -- Normalized unicode username inputs --- django/contrib/auth/forms.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'django/contrib/auth/forms.py') diff --git a/django/contrib/auth/forms.py b/django/contrib/auth/forms.py index 692cdb5dab..f279f5e893 100644 --- a/django/contrib/auth/forms.py +++ b/django/contrib/auth/forms.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals +import unicodedata + from django import forms from django.contrib.auth import ( authenticate, get_user_model, password_validation, @@ -60,6 +62,11 @@ class ReadOnlyPasswordHashField(forms.Field): return False +class UsernameField(forms.CharField): + def to_python(self, value): + return unicodedata.normalize('NFKC', super(UsernameField, self).to_python(value)) + + class UserCreationForm(forms.ModelForm): """ A form that creates a user, with no privileges, from the given username and @@ -83,6 +90,7 @@ class UserCreationForm(forms.ModelForm): class Meta: model = User fields = ("username",) + field_classes = {'username': UsernameField} def __init__(self, *args, **kwargs): super(UserCreationForm, self).__init__(*args, **kwargs) @@ -121,6 +129,7 @@ class UserChangeForm(forms.ModelForm): class Meta: model = User fields = '__all__' + field_classes = {'username': UsernameField} def __init__(self, *args, **kwargs): super(UserChangeForm, self).__init__(*args, **kwargs) @@ -140,7 +149,7 @@ class AuthenticationForm(forms.Form): Base class for authenticating users. Extend this to get a form that accepts username/password logins. """ - username = forms.CharField( + username = UsernameField( max_length=254, widget=forms.TextInput(attrs={'autofocus': ''}), ) -- cgit v1.3