Skip to content
Snippets Groups Projects
Commit 03250d4b authored by Dorian Lesbre's avatar Dorian Lesbre
Browse files

Switched to email username + required first and last name for accounts

parent d8dafc1b
No related branches found
No related tags found
No related merge requests found
from django.contrib import admin
from django.contrib.auth.models import Group
# Register your models here.
from accounts.models import EmailUser
# no need for groups - we only have regular users and superusers
admin.site.unregister(Group)
@admin.register(EmailUser)
class EmailUserAdmin(admin.ModelAdmin):
"""option d'affichage des activités dans la vue django admin"""
list_display = ("email", "last_name", "first_name", "is_superuser")
list_filter = ("is_superuser",)
list_editable = ("is_superuser",)
ordering = ("last_name", "first_name")
readonly_fields = ("date_joined", "last_login",)
exclude = ("groups","password",)
list_per_page = 200
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from home.models import InterludesParticipant
from accounts.models import EmailUser
class CreateAccountForm(UserCreationForm):
"""Form used to register a new user"""
class Meta:
model = User
fields = ('username', 'password1', 'password2',)
model = EmailUser
fields = ('email', 'first_name', 'last_name', 'password1', 'password2',)
from django.db import models
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.models import AbstractUser
# Create your models here.
class EmailUserManager(BaseUserManager):
"""User model manager that replaces username with email"""
def create_user(self, email, password, **extra_fields):
"""Create and save a User with the given email and password."""
if not email:
raise ValueError("Creating user with no email")
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password, **extra_fields):
"""Create and save a SuperUser with the given email and password."""
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self.create_user(email, password, **extra_fields)
class EmailUser(AbstractUser):
"""Utilisateur identifié par son email et non
un nom d'utilisateur"""
username = None
email = models.EmailField('adresse email', unique=True)
first_name = models.CharField('prénom', max_length=100)
last_name = models.CharField("nom", max_length=100)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ("last_name", "first_name",)
objects = EmailUserManager()
def __str__(self):
return self.email
class Meta:
verbose_name = "utilisateur"
......@@ -22,7 +22,7 @@ def create_account(request):
raw_password = form.cleaned_data.get('password1')
# login user after signing up
user = authenticate(username=user.username, password=raw_password)
user = authenticate(email=user.email, password=raw_password)
login(request, user)
# redirect user to home page
......
from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import gettext_lazy as _
from accounts.models import EmailUser
class InterludesActivity(models.Model):
"""une activité des interludes (i.e. JDR, murder)..."""
title = models.CharField("Titre", max_length=200)
......@@ -42,7 +43,7 @@ class InterludesParticipant(models.Model):
ENS_RENNES = "R", _("ENS Rennes")
ENS_CACHAN = "C", _("ENS Paris Saclay")
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="Utilisateur")
user = models.OneToOneField(EmailUser, on_delete=models.CASCADE, related_name="Utilisateur")
name = models.CharField("Nom complet", max_length=200)
email = models.EmailField("email")
school = models.CharField("ENS de rattachement", choices=ENS.choices, max_length=1)
......
......@@ -90,6 +90,8 @@ AUTHENTICATION_BACKENDS = (
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_USER_MODEL = 'accounts.EmailUser'
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment