From a96dcc66b73d946d585ed803adca0e0998a46f57 Mon Sep 17 00:00:00 2001 From: dzhuang Date: Wed, 12 Apr 2017 17:13:06 +0800 Subject: [PATCH 1/2] Update username validator. --- .../0010_change_username_validator.py | 21 +++++++++++++++++++ accounts/models.py | 10 ++------- course/auth.py | 12 ++--------- 3 files changed, 25 insertions(+), 18 deletions(-) create mode 100644 accounts/migrations/0010_change_username_validator.py diff --git a/accounts/migrations/0010_change_username_validator.py b/accounts/migrations/0010_change_username_validator.py new file mode 100644 index 00000000..26f001c3 --- /dev/null +++ b/accounts/migrations/0010_change_username_validator.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.4 on 2017-04-12 08:55 +from __future__ import unicode_literals + +import django.contrib.auth.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('accounts', '0009_user_git_auth_token_hash'), + ] + + operations = [ + migrations.AlterField( + model_name='user', + name='username', + field=models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=30, unique=True, validators=[django.contrib.auth.validators.ASCIIUsernameValidator()], verbose_name='username'), + ), + ] diff --git a/accounts/models.py b/accounts/models.py index 915e0945..6924f01e 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -30,7 +30,7 @@ from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin from django.utils.translation import ugettext_lazy as _, pgettext_lazy from django.utils import timezone from django.contrib.auth.models import UserManager -from django.core import validators +from django.contrib.auth.validators import ASCIIUsernameValidator from course.constants import USER_STATUS_CHOICES @@ -44,13 +44,7 @@ class User(AbstractBaseUser, PermissionsMixin): unique=True, help_text=_( 'Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.'), - validators=[ - validators.RegexValidator( - r'^[\w.@+-]+$', - _('Enter a valid username. This value may contain only ' - 'letters, numbers ' 'and @/./+/-/_ characters.') - ), - ], + validators=[ASCIIUsernameValidator()], error_messages={ 'unique': _("A user with that username already exists."), }, diff --git a/course/auth.py b/course/auth.py index 348c2e38..84fe455b 100644 --- a/course/auth.py +++ b/course/auth.py @@ -42,7 +42,7 @@ from django.contrib.auth.forms import \ from django.contrib.sites.shortcuts import get_current_site from django.contrib.auth.decorators import user_passes_test from django.urls import reverse -from django.core import validators +from django.contrib.auth.validators import ASCIIUsernameValidator from django.utils.http import is_safe_url from django.http import HttpResponseRedirect from django.template.response import TemplateResponse @@ -386,15 +386,7 @@ def sign_in_by_user_pw(request, redirect_field_name=REDIRECT_FIELD_NAME): class SignUpForm(StyledModelForm): username = forms.CharField(required=True, max_length=30, label=_("Username"), - validators=[ - validators.RegexValidator('^[\\w.@+-]+$', - string_concat( - _('Enter a valid username.'), (' '), - _('This value may contain only letters, ' - 'numbers and @/./+/-/_ characters.') - ), - 'invalid') - ]) + validators=[ASCIIUsernameValidator()]) class Meta: model = get_user_model() -- GitLab From 992e317a71cfa5c9b4a46487fd657990f8da7b2d Mon Sep 17 00:00:00 2001 From: dzhuang Date: Wed, 12 Apr 2017 17:26:21 +0800 Subject: [PATCH 2/2] Remove meaningless scripts. --- accounts/models.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/accounts/models.py b/accounts/models.py index 6924f01e..51c97f38 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -153,8 +153,6 @@ class User(AbstractBaseUser, PermissionsMixin): Returns the first_name plus the last_name, with a space in between. """ - if force_verbose_blank: - first_name return '%s %s' % ( verbose_blank(first_name), verbose_blank(last_name)) -- GitLab