Adding a custom User model

This commit is contained in:
2020-11-04 21:53:20 -03:00
parent b1d7d1b636
commit c8aaf33170
10 changed files with 142 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ INSTALLED_APPS = [
"django.contrib.sessions", "django.contrib.sessions",
"django.contrib.messages", "django.contrib.messages",
"django.contrib.staticfiles", "django.contrib.staticfiles",
"core",
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@@ -54,6 +55,8 @@ TEMPLATES = [
WSGI_APPLICATION = "app.wsgi.application" WSGI_APPLICATION = "app.wsgi.application"
AUTH_USER_MODEL = "core.User"
# Database # Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases # https://docs.djangoproject.com/en/3.1/ref/settings/#databases

0
core/__init__.py Normal file
View File

16
core/admin.py Normal file
View File

@@ -0,0 +1,16 @@
from django.contrib import admin
from .models import User
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
search_fields = ("id", "email", "first_name", "last_name")
list_display = (
"id",
"first_name",
"last_name",
"is_active",
"email",
"date_joined",
)

5
core/apps.py Normal file
View File

@@ -0,0 +1,5 @@
from django.apps import AppConfig
class CoreConfig(AppConfig):
name = 'core'

29
core/managers.py Normal file
View File

@@ -0,0 +1,29 @@
from django.contrib.auth.base_user import BaseUserManager
class UserManager(BaseUserManager):
use_in_migrations = True
def _create_user(self, email, password, **extra_fields):
"""
Creates and saves a User with the given email and password.
"""
if not email:
raise ValueError("The given email must be set")
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, email, password=None, **extra_fields):
extra_fields.setdefault("is_superuser", False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault("is_superuser", 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)

View File

@@ -0,0 +1,42 @@
# Generated by Django 3.1.3 on 2020-11-05 00:51
import core.managers
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.AutoField(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')),
('email', models.EmailField(max_length=254, unique=True, verbose_name='E-mail')),
('first_name', models.CharField(blank=True, max_length=30, verbose_name='First name')),
('last_name', models.CharField(blank=True, max_length=30, verbose_name='Last name')),
('date_joined', models.DateTimeField(auto_now_add=True, verbose_name='date joined')),
('is_active', models.BooleanField(default=True, verbose_name='User active?')),
('is_staff', models.BooleanField(default=False, verbose_name='Staff?')),
('is_superuser', models.BooleanField(default=False, verbose_name='Superuser?')),
('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',
'db_table': 'users',
'ordering': ['first_name', 'last_name'],
},
managers=[
('objects', core.managers.UserManager()),
],
),
]

View File

41
core/models.py Normal file
View File

@@ -0,0 +1,41 @@
from django.db import models
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.base_user import AbstractBaseUser
from .managers import UserManager
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField("E-mail", unique=True)
first_name = models.CharField("First name", max_length=30, blank=True)
last_name = models.CharField("Last name", max_length=30, blank=True)
date_joined = models.DateTimeField("date joined", auto_now_add=True)
is_active = models.BooleanField("User active?", default=True)
is_staff = models.BooleanField("Staff?", default=False)
is_superuser = models.BooleanField("Superuser?", default=False)
objects = UserManager()
USERNAME_FIELD = "email"
class Meta:
ordering = ["first_name", "last_name"]
verbose_name = "User"
verbose_name_plural = "Users"
db_table = "users"
def get_full_name(self):
"""
Returns the first_name plus the last_name, with a space in between.
"""
full_name = f"{self.first_name} {self.last_name}"
return full_name.strip()
def get_short_name(self):
"""
Returns the short name for the user.
"""
return self.first_name or "Unamed"

3
core/tests.py Normal file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

3
core/views.py Normal file
View File

@@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.