Skip to content

Commit

Permalink
Introduce custom User model
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen committed Apr 12, 2024
1 parent 121fd68 commit 6a79b74
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 0 deletions.
Empty file.
6 changes: 6 additions & 0 deletions template/src/project_name/accounts/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin

from .models import User

admin.site.register(User, UserAdmin)
6 changes: 6 additions & 0 deletions template/src/project_name/accounts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AccountsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "{{ project_name }}.accounts"
132 changes: 132 additions & 0 deletions template/src/project_name/accounts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Generated by Django 4.2.11 on 2024-04-10 07:32

import django.contrib.auth.models
import django.contrib.auth.validators
import django.utils.timezone
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.", # noqa: E501
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.", # noqa: E501
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"
),
),
(
"email",
models.EmailField(
blank=True, max_length=254, verbose_name="email address"
),
),
(
"is_staff",
models.BooleanField(
default=False,
help_text="Designates whether the user can log into this admin site.", # noqa: E501
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.", # noqa: E501
verbose_name="active",
),
),
(
"date_joined",
models.DateTimeField(
default=django.utils.timezone.now, verbose_name="date joined"
),
),
(
"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.", # noqa: E501
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()),
],
),
]
Empty file.
7 changes: 7 additions & 0 deletions template/src/project_name/accounts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib.auth.models import AbstractUser


class User(AbstractUser):

def __str__(self) -> str:
return self.username
1 change: 1 addition & 0 deletions template/src/project_name/accounts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your views here.
3 changes: 3 additions & 0 deletions template/src/project_name/main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"django.contrib.staticfiles",
# Third party
# Local
"{{ project_name }}.accounts",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -96,6 +97,8 @@
},
]

AUTH_USER_MODEL = "accounts.User"


# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/
Expand Down
Empty file.
32 changes: 32 additions & 0 deletions template/src/tests/system/accounts/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest
from django.contrib.auth import get_user_model

from {{ project_name }}.accounts import models


@pytest.mark.django_db
def test_create_user() -> None:
User = get_user_model()
user = User.objects.create_user(username="test", password="123") # noqa: S106

assert User is models.User
assert user.username == "test"
assert user.is_active
assert not user.is_staff
assert not user.is_superuser
assert user.email == ""


@pytest.mark.django_db
def test_create_superuser() -> None:
User = get_user_model()
admin_user = User.objects.create_superuser( # noqa: S106
username="superuser", password="123"
)

assert User is models.User
assert admin_user.username == "superuser"
assert admin_user.is_active
assert admin_user.is_staff
assert admin_user.is_superuser
assert admin_user.email == ""

0 comments on commit 6a79b74

Please sign in to comment.