From 4083be8e8cb1412f8fff33062b660a67e9187f28 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Fri, 15 May 2020 11:01:56 +0300 Subject: [PATCH] Username: disallow @ in usernames. --- myauth/migrations/0002_auto_20200515_0801.py | 19 ++++++++++++++ myauth/models.py | 27 +++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 myauth/migrations/0002_auto_20200515_0801.py diff --git a/myauth/migrations/0002_auto_20200515_0801.py b/myauth/migrations/0002_auto_20200515_0801.py new file mode 100644 index 0000000..3ce02b2 --- /dev/null +++ b/myauth/migrations/0002_auto_20200515_0801.py @@ -0,0 +1,19 @@ +# Generated by Django 3.0.3 on 2020-05-15 08:01 + +from django.db import migrations, models +import myauth.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('myauth', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='user', + name='username', + field=models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and ./+/-/_ only.', max_length=150, unique=True, validators=[myauth.models.UnicodeUsernameValidator()], verbose_name='username'), + ), + ] diff --git a/myauth/models.py b/myauth/models.py index 3d30525..4afc27c 100644 --- a/myauth/models.py +++ b/myauth/models.py @@ -1,5 +1,30 @@ from django.contrib.auth.models import AbstractUser +from django.core import validators +from django.db import models +from django.utils.deconstruct import deconstructible +from django.utils.translation import gettext_lazy as _ + + +@deconstructible +class UnicodeUsernameValidator(validators.RegexValidator): + regex = r'^[\w.+-]+\Z' + message = _( + 'Enter a valid username. This value may contain only letters, ' + 'numbers, and ./+/-/_ characters.' + ) + flags = 0 class User(AbstractUser): - pass + username_validator = UnicodeUsernameValidator() + + username = models.CharField( + _('username'), + max_length=150, + unique=True, + help_text=_('Required. 150 characters or fewer. Letters, digits and ./+/-/_ only.'), + validators=[username_validator], + error_messages={ + 'unique': _("A user with that username already exists."), + }, + )