Make user-model swappable

I may just need this later, and doing it this late was already painful enough.
This commit is contained in:
Klaas van Schelven
2024-05-29 10:22:57 +02:00
parent 9b6409f0cd
commit cef1127e48
10 changed files with 80 additions and 2 deletions

View File

@@ -47,9 +47,10 @@ INSTALLED_APPS = [
'django.contrib.staticfiles',
'tailwind', # As currently set up, this is also needed in production (templatetags)
'theme',
'admin_auto_filters', # TODO: decide whether 'admin.py' is useful in production too.
'users',
'theme',
'snappea',
'compat',
'projects',
@@ -62,6 +63,8 @@ INSTALLED_APPS = [
'performance',
]
AUTH_USER_MODEL = "users.User"
TAILWIND_APP_NAME = 'theme'
MIDDLEWARE = [

View File

@@ -7,6 +7,7 @@ from functools import partial
from django.db import models, transaction
from django.db.models import F, Value
from django.template.defaultfilters import date as default_date_filter
from django.conf import settings
from bugsink.volume_based_condition import VolumeBasedCondition
from alerts.tasks import send_unmute_alert
@@ -458,7 +459,9 @@ class TurningPoint(models.Model):
issue = models.ForeignKey("Issue", blank=False, null=True, on_delete=models.SET_NULL) # SET_NULL: cleanup 'later'
triggering_event = models.ForeignKey("events.Event", blank=True, null=True, on_delete=models.SET_NULL)
user = models.ForeignKey("auth.User", blank=True, null=True, on_delete=models.SET_NULL) # null: the system-user
# null: the system-user
user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.SET_NULL)
timestamp = models.DateTimeField(blank=False, null=False) # this info is also in the event, but event is nullable
kind = models.IntegerField(blank=False, null=False, choices=TurningPointKind.choices)
metadata = models.TextField(blank=False, null=False, default="{}") # json string

View File

@@ -47,6 +47,7 @@ include = [
"static*",
"templates*",
"theme*",
"users*",
]
# exclude = ["my_package.tests*"] # exclude packages matching these glob patterns (empty by default)

0
users/__init__.py Normal file
View File

5
users/admin.py Normal file
View File

@@ -0,0 +1,5 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User
admin.site.register(User, UserAdmin)

6
users/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class UsersConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'users'

View File

@@ -0,0 +1,46 @@
# Generated by Django 4.2.13 on 2024-05-29 08:00
import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
# Since this was introduced later, it "shouldn't" work in theory, but it appears to work in practice, as long as you
# manually fake it using:
# INSERT INTO django_migrations (app, name, applied) VALUES ('users', '0001_initial', '2023-11-05 16:18:04.716257');
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.', 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()),
],
),
]

View File

11
users/models.py Normal file
View File

@@ -0,0 +1,11 @@
from django.contrib.auth.models import AbstractUser
# > If youre starting a new project, its highly recommended to set up a custom user model, even if the default User
# > model is sufficient for you. This model behaves identically to the default user model, but youll be able to
# > customize it in the future if the need arises
class User(AbstractUser):
class Meta:
db_table = 'auth_user'

3
users/tests.py Normal file
View File

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