init
This commit is contained in:
0
user/__init__.py
Normal file
0
user/__init__.py
Normal file
27
user/admin.py
Normal file
27
user/admin.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from django.contrib import admin
|
||||
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
|
||||
|
||||
from .models import User
|
||||
from .forms import UserCreationForm, UserChangeForm
|
||||
|
||||
|
||||
@admin.register(User)
|
||||
class UserAdmin(BaseUserAdmin):
|
||||
add_form = UserCreationForm
|
||||
form = UserChangeForm
|
||||
model = User
|
||||
list_display = ["email", "is_staff", "is_superuser", "is_active"]
|
||||
list_filter = ["email", "is_staff", "is_superuser", "is_active"]
|
||||
fieldsets = [
|
||||
[None, {"fields": ["email", "password"]}],
|
||||
("permissions", {"fields": ["is_staff", "is_active", "is_superuser"]})
|
||||
]
|
||||
add_fieldsets = [
|
||||
[None, {
|
||||
"classes": ["wide"],
|
||||
"fields": ["email", "password1", "password2", "is_staff", "is_active", "is_superuser"]
|
||||
}]
|
||||
]
|
||||
search_fields = ["email"]
|
||||
ordering = ["email"]
|
||||
|
||||
6
user/apps.py
Normal file
6
user/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class UserConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'user'
|
||||
11
user/forms.py
Normal file
11
user/forms.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from django.contrib.auth import forms as BaseAuthForms
|
||||
|
||||
from .models import User
|
||||
|
||||
|
||||
class UserCreationForm(BaseAuthForms.UserCreationForm):
|
||||
pass
|
||||
|
||||
|
||||
class UserChangeForm(BaseAuthForms.UserChangeForm):
|
||||
pass
|
||||
44
user/migrations/0001_initial.py
Normal file
44
user/migrations/0001_initial.py
Normal file
@@ -0,0 +1,44 @@
|
||||
# Generated by Django 5.0.6 on 2024-05-15 14:10
|
||||
|
||||
import django.contrib.auth.validators
|
||||
import django.utils.timezone
|
||||
import user.models
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('auth', '0012_alter_user_first_name_max_length'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='User',
|
||||
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(max_length=254, unique=True)),
|
||||
('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', user.models.EmailUserManager()),
|
||||
],
|
||||
),
|
||||
]
|
||||
0
user/migrations/__init__.py
Normal file
0
user/migrations/__init__.py
Normal file
72
user/models.py
Normal file
72
user/models.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import AbstractUser, BaseUserManager
|
||||
|
||||
|
||||
class EmailUserManager(BaseUserManager):
|
||||
use_in_migrations = True
|
||||
|
||||
def _create_user(self, email, password, **extra_fields):
|
||||
if not email:
|
||||
raise ValueError("Un email doit être défini")
|
||||
|
||||
email = self.normalize_email(email)
|
||||
user = self.model(email=email, **extra_fields)
|
||||
user.set_password(password)
|
||||
user.save(using=self.db)
|
||||
return user
|
||||
|
||||
def create_user(self, username, email, password=None, **extra_fields):
|
||||
extra_fields.setdefault("is_staff", False)
|
||||
extra_fields.setdefault("is_superuser", False)
|
||||
return self._create_user(username, email, password, **extra_fields)
|
||||
|
||||
def create_superuser(self, email, password, **extra_fields):
|
||||
extra_fields.setdefault("is_staff", True)
|
||||
extra_fields.setdefault("is_superuser", True)
|
||||
|
||||
if extra_fields.get("is_staff") is not True:
|
||||
raise ValueError("Superuser doit être staff à True")
|
||||
if extra_fields.get("is_superuser") is not True:
|
||||
raise ValueError("SuperUser doit être is_superuser à True")
|
||||
|
||||
return self._create_user(email, password, **extra_fields)
|
||||
|
||||
|
||||
# class UsernameUserManager(BaseUserManager):
|
||||
# use_in_migrations = True
|
||||
#
|
||||
# def _create_user(self, username, email, password, **extra_fields):
|
||||
# if not username:
|
||||
# raise ValueError("Un username doit être défini")
|
||||
# if not email:
|
||||
# raise ValueError("Un email doit être défini")
|
||||
#
|
||||
# email = self.normalize_email(email)
|
||||
# user = self.model(username=username, email=email, **extra_fields)
|
||||
# user.set_password(password)
|
||||
# user.save(using=self.db)
|
||||
# return user
|
||||
#
|
||||
# def create_user(self, username, email, password=None, **extra_fields):
|
||||
# extra_fields.setdefault("is_staff", False)
|
||||
# extra_fields.setdefault("is_superuser", False)
|
||||
# return self._create_user(username, email, password, **extra_fields)
|
||||
#
|
||||
# def create_superuser(self, username, email, password, **extra_fields):
|
||||
# extra_fields.setdefault("is_staff", True)
|
||||
# extra_fields.setdefault("is_superuser", True)
|
||||
#
|
||||
# if extra_fields.get("is_staff") is not True:
|
||||
# raise ValueError("Superuser doit être staff à True")
|
||||
# if extra_fields.get("is_superuser") is not True:
|
||||
# raise ValueError("SuperUser doit être is_superuser à True")
|
||||
#
|
||||
# return self._create_user(username, email, password, **extra_fields)
|
||||
|
||||
|
||||
class User(AbstractUser):
|
||||
email = models.EmailField(unique=True)
|
||||
USERNAME_FIELD = 'email'
|
||||
|
||||
REQUIRED_FIELDS = []
|
||||
objects = EmailUserManager()
|
||||
3
user/tests.py
Normal file
3
user/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
3
user/views.py
Normal file
3
user/views.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
Reference in New Issue
Block a user