From 0de8ec8ae6542796994581a224b449451a195472 Mon Sep 17 00:00:00 2001 From: Alexis Michaltsis Date: Tue, 8 Nov 2022 12:22:12 +0100 Subject: [PATCH 1/2] Only allow unique emails for user accounts --- .../migrations/0002_alter_customuser_email.py | 18 ++++++++++++++++++ app/users/models.py | 13 ++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 app/users/migrations/0002_alter_customuser_email.py diff --git a/app/users/migrations/0002_alter_customuser_email.py b/app/users/migrations/0002_alter_customuser_email.py new file mode 100644 index 00000000..5db61546 --- /dev/null +++ b/app/users/migrations/0002_alter_customuser_email.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.15 on 2022-11-08 11:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='customuser', + name='email', + field=models.EmailField(error_messages={'unique': 'A user with that email already exists.'}, max_length=254, unique=True, verbose_name='email address'), + ), + ] diff --git a/app/users/models.py b/app/users/models.py index 659ac76b..982af6dc 100644 --- a/app/users/models.py +++ b/app/users/models.py @@ -1,5 +1,16 @@ from django.contrib.auth.models import AbstractUser +from django.utils.translation import gettext_lazy as _ +from django.db.models import EmailField class CustomUser(AbstractUser): - pass + email = EmailField( + _('email address'), + blank=False, + error_messages={ + 'unique': _( + "A user with that email already exists."), + }, + null=False, + unique=True, + ) From 090c9f6f331a2ac8b3c4621f9c4a15c6749145b4 Mon Sep 17 00:00:00 2001 From: Alexis Michaltsis Date: Tue, 8 Nov 2022 15:30:19 +0100 Subject: [PATCH 2/2] Make linter happy --- .../migrations/0002_alter_customuser_email.py | 17 ++++++++++------- app/users/models.py | 7 ++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/app/users/migrations/0002_alter_customuser_email.py b/app/users/migrations/0002_alter_customuser_email.py index 5db61546..7ed93a3a 100644 --- a/app/users/migrations/0002_alter_customuser_email.py +++ b/app/users/migrations/0002_alter_customuser_email.py @@ -5,14 +5,17 @@ class Migration(migrations.Migration): - dependencies = [ - ('users', '0001_initial'), - ] + dependencies = [("users", "0001_initial")] operations = [ migrations.AlterField( - model_name='customuser', - name='email', - field=models.EmailField(error_messages={'unique': 'A user with that email already exists.'}, max_length=254, unique=True, verbose_name='email address'), - ), + model_name="customuser", + name="email", + field=models.EmailField( + error_messages={"unique": "A user with that email already exists."}, + max_length=254, + unique=True, + verbose_name="email address", + ), + ) ] diff --git a/app/users/models.py b/app/users/models.py index 982af6dc..2bd39b90 100644 --- a/app/users/models.py +++ b/app/users/models.py @@ -5,12 +5,9 @@ class CustomUser(AbstractUser): email = EmailField( - _('email address'), + _("email address"), blank=False, - error_messages={ - 'unique': _( - "A user with that email already exists."), - }, + error_messages={"unique": _("A user with that email already exists.")}, null=False, unique=True, )