From d59e8cd6b09833a528af612cb30763714f17c240 Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Fri, 7 Jul 2023 13:22:54 -0400 Subject: [PATCH] Add user app and model --- printpub/settings.py | 77 ++++++++++++------------ printpub/user/__init__.py | 0 printpub/user/admin.py | 3 + printpub/user/apps.py | 6 ++ printpub/user/migrations/0001_initial.py | 43 +++++++++++++ printpub/user/migrations/__init__.py | 0 printpub/user/models.py | 22 +++++++ printpub/user/tests.py | 3 + printpub/user/views.py | 3 + 9 files changed, 120 insertions(+), 37 deletions(-) create mode 100644 printpub/user/__init__.py create mode 100644 printpub/user/admin.py create mode 100644 printpub/user/apps.py create mode 100644 printpub/user/migrations/0001_initial.py create mode 100644 printpub/user/migrations/__init__.py create mode 100644 printpub/user/models.py create mode 100644 printpub/user/tests.py create mode 100644 printpub/user/views.py diff --git a/printpub/settings.py b/printpub/settings.py index 4649b69..a9cf527 100644 --- a/printpub/settings.py +++ b/printpub/settings.py @@ -20,7 +20,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-ko*epoxb3ubgripapva6rb76y87g#0s8-kieljx$341ct_wr^9' +SECRET_KEY = "django-insecure-ko*epoxb3ubgripapva6rb76y87g#0s8-kieljx$341ct_wr^9" # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True @@ -31,53 +31,56 @@ ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', - 'rest_framework' + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", + "rest_framework", + "printpub.user.apps.UserConfig", ] +AUTH_USER_MODEL = "user.LocalUser" + MIDDLEWARE = [ - 'django.middleware.security.SecurityMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', + "django.middleware.security.SecurityMiddleware", + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", ] -ROOT_URLCONF = 'printpub.urls' +ROOT_URLCONF = "printpub.urls" TEMPLATES = [ { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", ], }, }, ] -WSGI_APPLICATION = 'printpub.wsgi.application' +WSGI_APPLICATION = "printpub.wsgi.application" # Database # https://docs.djangoproject.com/en/4.2/ref/settings/#databases DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': BASE_DIR / 'db.sqlite3', + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": BASE_DIR / "db.sqlite3", } } @@ -87,16 +90,16 @@ DATABASES = { AUTH_PASSWORD_VALIDATORS = [ { - 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", }, { - 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", }, { - 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", }, { - 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", }, ] @@ -104,9 +107,9 @@ AUTH_PASSWORD_VALIDATORS = [ # Internationalization # https://docs.djangoproject.com/en/4.2/topics/i18n/ -LANGUAGE_CODE = 'en-us' +LANGUAGE_CODE = "en-us" -TIME_ZONE = 'UTC' +TIME_ZONE = "UTC" USE_I18N = True @@ -116,9 +119,9 @@ USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/4.2/howto/static-files/ -STATIC_URL = 'static/' +STATIC_URL = "static/" # Default primary key field type # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field -DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" diff --git a/printpub/user/__init__.py b/printpub/user/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/printpub/user/admin.py b/printpub/user/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/printpub/user/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/printpub/user/apps.py b/printpub/user/apps.py new file mode 100644 index 0000000..6ccc98a --- /dev/null +++ b/printpub/user/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class UserConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "printpub.user" diff --git a/printpub/user/migrations/0001_initial.py b/printpub/user/migrations/0001_initial.py new file mode 100644 index 0000000..91a785a --- /dev/null +++ b/printpub/user/migrations/0001_initial.py @@ -0,0 +1,43 @@ +# Generated by Django 4.2.3 on 2023-07-07 17:27 + +import django.contrib.auth.models +import django.contrib.auth.validators +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('auth', '0012_alter_user_first_name_max_length'), + ] + + operations = [ + migrations.CreateModel( + name='LocalUser', + 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')), + ('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')), + ('display_name', models.CharField(max_length=128)), + ('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={ + 'verbose_name': 'user', + 'verbose_name_plural': 'users', + 'abstract': False, + }, + managers=[ + ('objects', django.contrib.auth.models.UserManager()), + ], + ), + ] diff --git a/printpub/user/migrations/__init__.py b/printpub/user/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/printpub/user/models.py b/printpub/user/models.py new file mode 100644 index 0000000..e628b76 --- /dev/null +++ b/printpub/user/models.py @@ -0,0 +1,22 @@ +from django.db import models +from django.contrib.auth import models as auth_models + + +class LocalUser(auth_models.AbstractUser): + REQUIRED_FIELDS = ["display_name"] + + first_name = None + last_name = None + display_name = models.CharField(max_length=128) + + def get_full_name(self) -> str: + """ + Override. We do not use first/last name so we must give something else + """ + return self.display_name + + def get_short_name(self) -> str: + """ + Override. We do not use first name so we must give something else + """ + return self.get_username() diff --git a/printpub/user/tests.py b/printpub/user/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/printpub/user/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/printpub/user/views.py b/printpub/user/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/printpub/user/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here.