From 5075ed3f271612185fa1147cb779c07117232382 Mon Sep 17 00:00:00 2001 From: Laetitia Getti Date: Wed, 10 May 2023 12:35:15 +0200 Subject: [PATCH] customUser with email as username --- la_chariotte/accounts/admin.py | 15 ++ la_chariotte/accounts/forms.py | 23 +++ .../accounts/migrations/0001_initial.py | 137 ++++++++++++++++++ ...tomuser_email_alter_customuser_username.py | 31 ++++ la_chariotte/accounts/models.py | 14 +- la_chariotte/accounts/views.py | 6 +- la_chariotte/order/forms.py | 6 +- .../0016_alter_groupedorder_description.py | 17 +++ la_chariotte/order/models.py | 5 +- la_chariotte/order/tests/test_views.py | 1 - la_chariotte/settings.py | 1 + 11 files changed, 247 insertions(+), 9 deletions(-) create mode 100644 la_chariotte/accounts/admin.py create mode 100644 la_chariotte/accounts/forms.py create mode 100644 la_chariotte/accounts/migrations/0001_initial.py create mode 100644 la_chariotte/accounts/migrations/0002_alter_customuser_email_alter_customuser_username.py create mode 100644 la_chariotte/order/migrations/0016_alter_groupedorder_description.py diff --git a/la_chariotte/accounts/admin.py b/la_chariotte/accounts/admin.py new file mode 100644 index 0000000..3cd90ef --- /dev/null +++ b/la_chariotte/accounts/admin.py @@ -0,0 +1,15 @@ +from django.contrib import admin +from django.contrib.auth.admin import UserAdmin + +from .forms import CustomUserChangeForm, CustomUserCreationForm +from .models import CustomUser + + +class CustomUserAdmin(UserAdmin): + add_form = CustomUserCreationForm + form = CustomUserChangeForm + model = CustomUser + list_display = ["username", "first_name", "last_name"] + + +admin.site.register(CustomUser, CustomUserAdmin) diff --git a/la_chariotte/accounts/forms.py b/la_chariotte/accounts/forms.py new file mode 100644 index 0000000..8a6d0ca --- /dev/null +++ b/la_chariotte/accounts/forms.py @@ -0,0 +1,23 @@ +from django import forms +from django.contrib.auth.forms import UserChangeForm, UserCreationForm + +from .models import CustomUser + + +class CustomUserCreationForm(UserCreationForm): + class Meta: + model = CustomUser + fields = ("username", "first_name", "last_name", "password1", "password2") + widgets = { + "username": forms.TextInput( + attrs={ + "placeholder": "exemple@mail.fr", + } + ), + } + + +class CustomUserChangeForm(UserChangeForm): # pas encore utilisé - pour la V1 + class Meta: + model = CustomUser + fields = ("email", "first_name", "last_name") diff --git a/la_chariotte/accounts/migrations/0001_initial.py b/la_chariotte/accounts/migrations/0001_initial.py new file mode 100644 index 0000000..f4ea14b --- /dev/null +++ b/la_chariotte/accounts/migrations/0001_initial.py @@ -0,0 +1,137 @@ +# Generated by Django 4.2 on 2023-05-09 15:04 + +import django.contrib.auth.models +import django.contrib.auth.validators +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + initial = True + + dependencies = [ + ("auth", "0012_alter_user_first_name_max_length"), + ] + + operations = [ + migrations.CreateModel( + name="CustomUser", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("password", models.CharField(max_length=128, verbose_name="password")), + ( + "last_login", + models.DateTimeField( + blank=True, null=True, verbose_name="last login" + ), + ), + ( + "is_superuser", + models.BooleanField( + default=False, + help_text="Designates that this user has all permissions without explicitly assigning them.", + verbose_name="superuser status", + ), + ), + ( + "username", + 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=[ + django.contrib.auth.validators.UnicodeUsernameValidator() + ], + verbose_name="username", + ), + ), + ( + "first_name", + models.CharField( + blank=True, max_length=150, verbose_name="first name" + ), + ), + ( + "last_name", + models.CharField( + blank=True, max_length=150, verbose_name="last name" + ), + ), + ( + "is_staff", + models.BooleanField( + default=False, + help_text="Designates whether the user can log into this admin site.", + verbose_name="staff status", + ), + ), + ( + "is_active", + models.BooleanField( + default=True, + help_text="Designates whether this user should be treated as active. Unselect this instead of deleting accounts.", + verbose_name="active", + ), + ), + ( + "date_joined", + models.DateTimeField( + default=django.utils.timezone.now, verbose_name="date joined" + ), + ), + ( + "email", + models.EmailField( + blank=True, + error_messages={ + "unique": "Un·e utilisateur·ice avec cette adresse mail existe déjà." + }, + max_length=254, + unique=True, + verbose_name="Adresse mail", + ), + ), + ( + "groups", + models.ManyToManyField( + blank=True, + help_text="The groups this user belongs to. A user will get all permissions granted to each of their groups.", + related_name="user_set", + related_query_name="user", + to="auth.group", + verbose_name="groups", + ), + ), + ( + "user_permissions", + models.ManyToManyField( + blank=True, + help_text="Specific permissions for this user.", + related_name="user_set", + related_query_name="user", + to="auth.permission", + verbose_name="user permissions", + ), + ), + ], + options={ + "verbose_name": "user", + "verbose_name_plural": "users", + "abstract": False, + }, + managers=[ + ("objects", django.contrib.auth.models.UserManager()), + ], + ), + ] diff --git a/la_chariotte/accounts/migrations/0002_alter_customuser_email_alter_customuser_username.py b/la_chariotte/accounts/migrations/0002_alter_customuser_email_alter_customuser_username.py new file mode 100644 index 0000000..af520ec --- /dev/null +++ b/la_chariotte/accounts/migrations/0002_alter_customuser_email_alter_customuser_username.py @@ -0,0 +1,31 @@ +# Generated by Django 4.2 on 2023-05-10 09:40 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("accounts", "0001_initial"), + ] + + operations = [ + migrations.AlterField( + model_name="customuser", + name="email", + field=models.EmailField( + blank=True, max_length=254, verbose_name="email address" + ), + ), + migrations.AlterField( + model_name="customuser", + name="username", + field=models.EmailField( + error_messages={ + "unique": "Un·e utilisateur·ice avec cette adresse mail existe déjà." + }, + max_length=254, + unique=True, + verbose_name="Email", + ), + ), + ] diff --git a/la_chariotte/accounts/models.py b/la_chariotte/accounts/models.py index 71a8362..fee40a8 100644 --- a/la_chariotte/accounts/models.py +++ b/la_chariotte/accounts/models.py @@ -1,3 +1,15 @@ +from django.contrib.auth.models import AbstractUser from django.db import models -# Create your models here. + +class CustomUser(AbstractUser): + username = models.EmailField( + "Email", + unique=True, + error_messages={ + "unique": "Un·e utilisateur·ice avec cette adresse mail existe déjà.", + }, + ) + + def __str__(self): + return self.get_full_name() diff --git a/la_chariotte/accounts/views.py b/la_chariotte/accounts/views.py index bf85312..b550963 100644 --- a/la_chariotte/accounts/views.py +++ b/la_chariotte/accounts/views.py @@ -1,9 +1,11 @@ -from django.contrib.auth.forms import UserCreationForm +from django.contrib.auth.views import LoginView from django.urls import reverse_lazy from django.views import generic +from .forms import CustomUserCreationForm + class SignUpView(generic.CreateView): - form_class = UserCreationForm + form_class = CustomUserCreationForm success_url = reverse_lazy("accounts:login") template_name = "registration/signup.html" diff --git a/la_chariotte/order/forms.py b/la_chariotte/order/forms.py index e2b3b70..fe50a71 100644 --- a/la_chariotte/order/forms.py +++ b/la_chariotte/order/forms.py @@ -1,4 +1,4 @@ -from django.contrib.auth.models import User +from django.contrib.auth import get_user_model from django.core.exceptions import NON_FIELD_ERRORS, ValidationError from django.db import IntegrityError @@ -35,7 +35,7 @@ class GroupedOrderForm(ModelForm): super().__init__(*args, **kwargs) def save(self, commit=True): - self.instance.orga = User.objects.get(id=self.user.pk) + self.instance.orga = get_user_model().objects.get(id=self.user.pk) return super().save(commit=commit) def clean(self): @@ -45,7 +45,7 @@ class GroupedOrderForm(ModelForm): GroupedOrder.objects.get( name=cleaned_data["name"], delivery_date=cleaned_data["delivery_date"], - orga=User.objects.get(id=self.user.pk), + orga=get_user_model().objects.get(id=self.user.pk), ) except GroupedOrder.DoesNotExist: pass diff --git a/la_chariotte/order/migrations/0016_alter_groupedorder_description.py b/la_chariotte/order/migrations/0016_alter_groupedorder_description.py new file mode 100644 index 0000000..9ac6785 --- /dev/null +++ b/la_chariotte/order/migrations/0016_alter_groupedorder_description.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2 on 2023-05-09 15:04 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("order", "0015_groupedorder_description_groupedorder_place"), + ] + + operations = [ + migrations.AlterField( + model_name="groupedorder", + name="description", + field=models.TextField(blank=True, null=True, verbose_name="Description"), + ), + ] diff --git a/la_chariotte/order/models.py b/la_chariotte/order/models.py index 5b2696d..80e2c87 100644 --- a/la_chariotte/order/models.py +++ b/la_chariotte/order/models.py @@ -1,16 +1,17 @@ -from django.contrib.auth.models import User from django.core.exceptions import ValidationError from django.db import models from django.urls import reverse from django.utils import timezone +from la_chariotte.settings import AUTH_USER_MODEL + class GroupedOrder(models.Model): name = models.CharField( max_length=100, null=True, verbose_name="Titre de la commande" ) # optionnal orga = models.ForeignKey( - User, on_delete=models.CASCADE, verbose_name="Organisateur·ice" + AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="Organisateur·ice" ) delivery_date = models.DateField("Date de livraison") deadline = models.DateTimeField("Date limite de commande") diff --git a/la_chariotte/order/tests/test_views.py b/la_chariotte/order/tests/test_views.py index 954194a..015ea05 100644 --- a/la_chariotte/order/tests/test_views.py +++ b/la_chariotte/order/tests/test_views.py @@ -2,7 +2,6 @@ import datetime import pytest from django.contrib import auth -from django.contrib.auth.models import User from django.urls import reverse from django.utils import timezone diff --git a/la_chariotte/settings.py b/la_chariotte/settings.py index 2fe2f07..b94ef28 100644 --- a/la_chariotte/settings.py +++ b/la_chariotte/settings.py @@ -116,6 +116,7 @@ AUTH_PASSWORD_VALIDATORS = [ }, ] +AUTH_USER_MODEL = "accounts.CustomUser" # Internationalization # https://docs.djangoproject.com/en/4.1/topics/i18n/