Skip to content

Commit 2670eaf

Browse files
authored
Base custom User model (#972)
* Add a custom User model to replace the default one * SQL update the migrations table to fake-apply the custom User initial migration * Remove useless STATICFILES settings * Update settings with the Django Admin seed env vars
1 parent 2eb8d3f commit 2670eaf

File tree

10 files changed

+124
-6
lines changed

10 files changed

+124
-6
lines changed

backend/seismic_site/settings.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
HERE_MAPS_API_KEY=(str, ""),
3434
DATA_UPLOAD_MAX_NUMBER_FIELDS=(int, 1000),
3535
BACKGROUND_WORKERS_COUNT=(int, 1),
36+
DJANGO_ADMIN_EMAIL=(str, ""),
37+
DJANGO_ADMIN_PASSWORD=(str, ""),
3638
# email settings
3739
EMAIL_SEND_METHOD=(str, "async"),
3840
EMAIL_BACKEND=(str, "django.core.mail.backends.console.EmailBackend"),
@@ -149,6 +151,7 @@
149151
"utils",
150152
"buildings",
151153
"static_custom",
154+
"users",
152155
# api documentation
153156
"drf_spectacular",
154157
]
@@ -224,6 +227,11 @@
224227
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
225228

226229

230+
AUTH_USER_MODEL = "users.User"
231+
DJANGO_ADMIN_EMAIL = env.str("DJANGO_ADMIN_EMAIL")
232+
DJANGO_ADMIN_PASSWORD = env.str("DJANGO_ADMIN_PASSWORD")
233+
234+
227235
# Password validation
228236
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
229237

@@ -305,12 +313,7 @@
305313
STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, "static"))
306314
MEDIA_ROOT = os.path.abspath(os.path.join(BASE_DIR, "media"))
307315

308-
DEV_DEPENDECIES_LOCATION = "bower_components"
309-
310-
STATICFILES_DIRS = [
311-
os.path.abspath(os.path.join(DEV_DEPENDECIES_LOCATION)),
312-
os.path.abspath(os.path.join("static_extras")),
313-
]
316+
STATICFILES_DIRS = []
314317

315318
default_storage_options = {}
316319

backend/users/__init__.py

Whitespace-only changes.

backend/users/admin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Register your models here.

backend/users/apps.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class UsersConfig(AppConfig):
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "users"
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Generated by Django 4.2.11 on 2024-04-02 05:23
2+
3+
import django.contrib.auth.models
4+
import django.contrib.auth.validators
5+
from django.db import migrations, models
6+
import django.utils.timezone
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
initial = True
12+
13+
dependencies = [
14+
("auth", "0012_alter_user_first_name_max_length"),
15+
]
16+
17+
operations = [
18+
migrations.CreateModel(
19+
name="User",
20+
fields=[
21+
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
22+
("password", models.CharField(max_length=128, verbose_name="password")),
23+
("last_login", models.DateTimeField(blank=True, null=True, verbose_name="last login")),
24+
(
25+
"is_superuser",
26+
models.BooleanField(
27+
default=False,
28+
help_text="Designates that this user has all permissions without explicitly assigning them.",
29+
verbose_name="superuser status",
30+
),
31+
),
32+
(
33+
"username",
34+
models.CharField(
35+
error_messages={"unique": "A user with that username already exists."},
36+
help_text="Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.",
37+
max_length=150,
38+
unique=True,
39+
validators=[django.contrib.auth.validators.UnicodeUsernameValidator()],
40+
verbose_name="username",
41+
),
42+
),
43+
("first_name", models.CharField(blank=True, max_length=150, verbose_name="first name")),
44+
("last_name", models.CharField(blank=True, max_length=150, verbose_name="last name")),
45+
("email", models.EmailField(blank=True, max_length=254, verbose_name="email address")),
46+
(
47+
"is_staff",
48+
models.BooleanField(
49+
default=False,
50+
help_text="Designates whether the user can log into this admin site.",
51+
verbose_name="staff status",
52+
),
53+
),
54+
(
55+
"is_active",
56+
models.BooleanField(
57+
default=True,
58+
help_text="Designates whether this user should be treated as active. Unselect this instead of deleting accounts.",
59+
verbose_name="active",
60+
),
61+
),
62+
("date_joined", models.DateTimeField(default=django.utils.timezone.now, verbose_name="date joined")),
63+
(
64+
"groups",
65+
models.ManyToManyField(
66+
blank=True,
67+
help_text="The groups this user belongs to. A user will get all permissions granted to each of their groups.",
68+
related_name="user_set",
69+
related_query_name="user",
70+
to="auth.group",
71+
verbose_name="groups",
72+
),
73+
),
74+
(
75+
"user_permissions",
76+
models.ManyToManyField(
77+
blank=True,
78+
help_text="Specific permissions for this user.",
79+
related_name="user_set",
80+
related_query_name="user",
81+
to="auth.permission",
82+
verbose_name="user permissions",
83+
),
84+
),
85+
],
86+
options={
87+
"db_table": "auth_user",
88+
},
89+
managers=[
90+
("objects", django.contrib.auth.models.UserManager()),
91+
],
92+
),
93+
]

backend/users/migrations/__init__.py

Whitespace-only changes.

backend/users/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.contrib.auth.models import AbstractUser
2+
3+
4+
class User(AbstractUser):
5+
class Meta:
6+
db_table = "auth_user" # TODO: This will be changed back after the data migration

backend/users/tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Create your tests here.

backend/users/views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Create your views here.

docker/s6-rc.d/init/init.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ python3 manage.py check
2525

2626
# Run the database migrations
2727
if is_enabled "${RUN_MIGRATIONS:-False}"; then
28+
29+
# Fix the User model
30+
echo "Fixing the User model"
31+
echo "INSERT INTO django_migrations (app, name, applied) SELECT 'users', '0001_initial', CURRENT_TIMESTAMP WHERE NOT EXISTS (SELECT app FROM django_migrations WHERE app='users' AND name='0001_initial');" | python3 manage.py dbshell
32+
echo "UPDATE django_content_type SET app_label = 'users' WHERE app_label = 'auth' and model = 'user';" | python manage.py dbshell
33+
34+
# Run the actual migrations
2835
echo "Migrating database"
2936
python3 manage.py migrate --run-syncdb
3037
python3 manage.py createcachetable

0 commit comments

Comments
 (0)