From dbad9ac9150ed2b2c94f541d593c73873a500068 Mon Sep 17 00:00:00 2001 From: Nathan Levesque Date: Wed, 5 Feb 2025 16:10:46 -0500 Subject: [PATCH 1/2] Adde custom user model --- main/settings.py | 2 +- users/migrations/0001_initial.py | 142 +++++++++++++++++- .../0002_update_user_contenttype.py | 29 ++++ users/models.py | 10 ++ 4 files changed, 178 insertions(+), 5 deletions(-) create mode 100644 users/migrations/0002_update_user_contenttype.py create mode 100644 users/models.py diff --git a/main/settings.py b/main/settings.py index 8391121f5f..4e5e96190f 100644 --- a/main/settings.py +++ b/main/settings.py @@ -69,7 +69,7 @@ ALLOWED_HOSTS = ["*"] -AUTH_USER_MODEL = "auth.User" +AUTH_USER_MODEL = "users.User" SECURE_SSL_REDIRECT = get_bool("MITOL_SECURE_SSL_REDIRECT", True) # noqa: FBT003 diff --git a/users/migrations/0001_initial.py b/users/migrations/0001_initial.py index b90bc3412a..f4ed89b4d2 100644 --- a/users/migrations/0001_initial.py +++ b/users/migrations/0001_initial.py @@ -1,9 +1,143 @@ -# Generated by Django 4.2.18 on 2025-02-05 19:41 +# Generated by Django 4.2.18 on 2025-02-05 20:42 -from django.db import migrations +import django.contrib.auth.models +import django.contrib.auth.validators +import django.utils.timezone +from django.db import migrations, models class Migration(migrations.Migration): - dependencies = [] + initial = True - operations = [] + 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" + ), + ), + ( + "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." + ), + 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" + ), + ), + ( + "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={ + "db_table": "auth_user", + }, + managers=[ + ("objects", django.contrib.auth.models.UserManager()), + ], + ), + ] diff --git a/users/migrations/0002_update_user_contenttype.py b/users/migrations/0002_update_user_contenttype.py new file mode 100644 index 0000000000..10971877e7 --- /dev/null +++ b/users/migrations/0002_update_user_contenttype.py @@ -0,0 +1,29 @@ +# Generated by Django 4.2.18 on 2025-02-05 22:13 + +from django.db import migrations + + +def change_user_type(apps, schema_editor): + """Update the ContentType for User to point to the custom one""" + ContentType = apps.get_model("contenttypes", "ContentType") + ct = ContentType.objects.filter(app_label="auth", model="user").first() + if ct: + ct.app_label = "users" + ct.save() + + +def revert_user_type(apps, schema_editor): + """Update the ContentType for User to point to the default django one""" + ContentType = apps.get_model("contenttypes", "ContentType") + ct = ContentType.objects.filter(app_label="users", model="user").first() + if ct: + ct.app_label = "auth" + ct.save() + + +class Migration(migrations.Migration): + dependencies = [ + ("users", "0001_initial"), + ] + + operations = [migrations.RunPython(change_user_type, revert_user_type)] diff --git a/users/models.py b/users/models.py new file mode 100644 index 0000000000..4733922bf5 --- /dev/null +++ b/users/models.py @@ -0,0 +1,10 @@ +"""Users models""" + +from django.contrib.auth.models import AbstractUser + + +class User(AbstractUser): + """Custom model for users""" + + class Meta: + db_table = "auth_user" From 2d912b41173f4249e60a753b8b1da47fe7be1b0c Mon Sep 17 00:00:00 2001 From: Nathan Levesque Date: Mon, 10 Feb 2025 13:46:13 -0500 Subject: [PATCH 2/2] Use users.apps.UsersConfig --- main/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/settings.py b/main/settings.py index 4e5e96190f..ecb551307a 100644 --- a/main/settings.py +++ b/main/settings.py @@ -107,7 +107,7 @@ "drf_spectacular", # Put our apps after this point "main", - "users", + "users.apps.UsersConfig", "authentication", "channels", "profiles",