Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added custom user model with customer user manager #6

Merged
merged 9 commits into from
May 13, 2024
3 changes: 3 additions & 0 deletions reddit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"users",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -129,3 +130,5 @@
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

AUTH_USER_MODEL = "users.User"
43 changes: 43 additions & 0 deletions users/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 5.0.4 on 2024-05-11 16:16

import django.utils.timezone
from django.db import migrations, models

import users.models


class Migration(migrations.Migration):

initial = True

dependencies = [ # noqa: RUF012
("auth", "0012_alter_user_first_name_max_length"),
]

operations = [ # noqa: RUF012
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")), # noqa: E501
("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")), # noqa: E501
("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")), # noqa: E501
("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")), # noqa: E501
("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")), # noqa: E501
],
options={
"verbose_name": "user",
"verbose_name_plural": "users",
"abstract": False,
},
managers=[
("objects", users.models.UserManager()),
],
),
]
42 changes: 40 additions & 2 deletions users/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
# from django.db import models #noqa: ERA001
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.models import AbstractUser
from django.db import models

# Create your models here.

class UserManager(BaseUserManager):
use_in_migrations = True

def _create_user(self, email, password, **extra_fields): # noqa: ANN202, ANN101, ANN001, ANN003
if not email:
message = "Users must have an email address"
raise ValueError(message)
email: str = 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): # noqa: ANN201, ANN101, ANN001, ANN003, D102
extra_fields.setdefault("is_staff", False)
extra_fields.setdefault("is_superuser", False)
return self._create_user(email, password, **extra_fields)

def create_superuser(self, email, password, **extra_fields): # noqa: ANN201, ANN001, ANN003, ANN101, D102
extra_fields.setdefault("is_staff", True)
extra_fields.setdefault("is_superuser", True)

if extra_fields.get("is_staff") is not True:
raise ValueError("Superuser must have is_staff=True.") # noqa: EM101, TRY003
if extra_fields.get("is_superuser") is not True:
raise ValueError("Superuser must have is_superuser=True.") # noqa: EM101, TRY003

return self._create_user(email, password, **extra_fields)


class User(AbstractUser):
USERNAME_FIELD = "email"
REQUIRED_FIELDS = [] # noqa: RUF012
objects = UserManager()
username = None
email = models.EmailField(unique=True)
Loading