summaryrefslogtreecommitdiffstats
path: root/myuser
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-04-23 11:16:38 +0100
committerMatthew Lemon <y@yulqen.org>2024-04-23 11:16:38 +0100
commit0f951dcf029d4af284467543a3afdf5bf6581a20 (patch)
treea48384210cdc168e3bd3ccff6d6d516eeed9e748 /myuser
parent8b084e9fe7a5f3a04c32daf9a24f7f2cf67300f9 (diff)
switched to Django
Diffstat (limited to 'myuser')
-rw-r--r--myuser/__init__.py0
-rw-r--r--myuser/admin.py100
-rw-r--r--myuser/apps.py6
-rw-r--r--myuser/migrations/0001_initial.py52
-rw-r--r--myuser/migrations/__init__.py0
-rw-r--r--myuser/models.py75
-rw-r--r--myuser/tests.py3
-rw-r--r--myuser/views.py3
8 files changed, 239 insertions, 0 deletions
diff --git a/myuser/__init__.py b/myuser/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/myuser/__init__.py
diff --git a/myuser/admin.py b/myuser/admin.py
new file mode 100644
index 0000000..46dc494
--- /dev/null
+++ b/myuser/admin.py
@@ -0,0 +1,100 @@
+from django import forms
+from django.contrib import admin
+from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
+from django.contrib.auth.forms import ReadOnlyPasswordHashField
+from django.contrib.auth.models import Group
+
+from .models import Team, TeamUser
+
+
+class TeamUserCreationForm(forms.ModelForm):
+ password1 = forms.CharField(label="Password", widget=forms.PasswordInput)
+ password2 = forms.CharField(label="Password confirmation", widget=forms.PasswordInput)
+
+ class Meta:
+ model = TeamUser
+ fields = ("email",)
+
+ def clean_password2(self):
+ "Check that the two password entries match"
+ password1 = self.cleaned_data.get("password1")
+ password2 = self.cleaned_data.get("password2")
+ if password1 and password2 and password1 != password2:
+ raise ValueError("Passwords don't match")
+ return password2
+
+ def save(self, commit=True):
+ # Save the provided password in hashed format
+ user = super().save(commit=False)
+ user.set_password(self.cleaned_data["password1"])
+ if commit:
+ user.save()
+ return user
+
+
+class TeamUserChangeForm(forms.ModelForm):
+ """A form for updating users. Includes all the fields on
+ the user, but replaces the password field with admin's
+ disabled password hash display field.
+ """
+
+ password = ReadOnlyPasswordHashField()
+
+ class Meta:
+ model = TeamUser
+ fields = (
+ "email",
+ "password",
+ "first_name",
+ "last_name",
+ "is_active",
+ "is_admin",
+ )
+
+
+class TeamUserAdmin(BaseUserAdmin):
+ form = TeamUserChangeForm
+ add_form = TeamUserCreationForm
+
+ list_display = ("email", "first_name", "last_name", "designation", "is_admin")
+ list_filter = ("is_admin",)
+ fieldsets = (
+ (None, {"fields": ("email", "password")}),
+ (
+ "Personal info",
+ {
+ "fields": (
+ "first_name",
+ "last_name",
+ "designation",
+ "dapsy",
+ "team",
+ "lead_for",
+ )
+ },
+ ),
+ ("Permissions", {"fields": ("is_admin",)}),
+ )
+ add_fieldsets = (
+ (
+ None,
+ {
+ "classes": ("wide",),
+ "fields": (
+ "email",
+ "first_name",
+ "last_name",
+ "password1",
+ "password2",
+ ),
+ },
+ ),
+ )
+ search_fields = ("email",)
+ ordering = ("email",)
+ filer_horizontal = ()
+
+
+admin.site.register(TeamUser, TeamUserAdmin)
+admin.site.register(Team)
+admin.site.unregister(Group)
diff --git a/myuser/apps.py b/myuser/apps.py
new file mode 100644
index 0000000..73bb265
--- /dev/null
+++ b/myuser/apps.py
@@ -0,0 +1,6 @@
+from django.apps import AppConfig
+
+
+class AuthuserConfig(AppConfig):
+ default_auto_field = "django.db.models.BigAutoField"
+ name = "myuser"
diff --git a/myuser/migrations/0001_initial.py b/myuser/migrations/0001_initial.py
new file mode 100644
index 0000000..3442670
--- /dev/null
+++ b/myuser/migrations/0001_initial.py
@@ -0,0 +1,52 @@
+# Generated by Django 4.0.8 on 2022-11-02 09:00
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+ initial = True
+
+ dependencies = [
+ ('auth', '0012_alter_user_first_name_max_length'),
+ ('engagements', '0001_initial'),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='Team',
+ fields=[
+ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+ ('date_created', models.DateTimeField(auto_now_add=True)),
+ ('last_modified', models.DateTimeField(auto_now=True)),
+ ('name', models.CharField(max_length=256)),
+ ],
+ options={
+ 'abstract': False,
+ },
+ ),
+ migrations.CreateModel(
+ name='TeamUser',
+ 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')),
+ ('email', models.EmailField(max_length=255, unique=True, verbose_name='email address')),
+ ('first_name', models.CharField(blank=True, max_length=20, null=True)),
+ ('last_name', models.CharField(blank=True, max_length=20, null=True)),
+ ('dapsy', models.BooleanField(default=False)),
+ ('is_active', models.BooleanField(default=True)),
+ ('is_admin', models.BooleanField(default=False)),
+ ('designation', models.CharField(blank=True, max_length=3, null=True)),
+ ('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')),
+ ('lead_for', models.ManyToManyField(blank=True, related_name='lead_inspector', to='engagements.organisation')),
+ ('team', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='team_members', to='myuser.team')),
+ ('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={
+ 'abstract': False,
+ },
+ ),
+ ]
diff --git a/myuser/migrations/__init__.py b/myuser/migrations/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/myuser/migrations/__init__.py
diff --git a/myuser/models.py b/myuser/models.py
new file mode 100644
index 0000000..1236ba6
--- /dev/null
+++ b/myuser/models.py
@@ -0,0 +1,75 @@
+from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
+from django.contrib.auth.models import PermissionsMixin
+from django.db import models
+from django.db.models import BooleanField, CharField, EmailField
+
+
+class Common(models.Model):
+ date_created = models.DateTimeField(auto_now_add=True)
+ last_modified = models.DateTimeField(auto_now=True)
+
+ class Meta:
+ abstract = True
+
+
+class TeamMgr(BaseUserManager):
+ def create_user(self, email, password=None):
+ if not email:
+ raise ValueError("Users must have an email address")
+ user = self.model(
+ email=self.normalize_email(email),
+ )
+ user.set_password(password)
+ user.save(using=self._db)
+ return user
+
+ def create_superuser(self, email, password=None):
+ user = self.create_user(
+ email,
+ password=password,
+ )
+ user.is_admin = True
+ user.save(using=self._db)
+ return user
+
+
+class Team(Common):
+ name = models.CharField(max_length=256)
+
+ def __str__(self):
+ return self.name
+
+
+class TeamUser(AbstractBaseUser, PermissionsMixin):
+ email = EmailField(verbose_name="email address", max_length=255, unique=True)
+ first_name = CharField(max_length=20, null=True, blank=True)
+ last_name = CharField(max_length=20, null=True, blank=True)
+ dapsy = BooleanField(default=False)
+ is_active = BooleanField(default=True)
+ is_admin = BooleanField(default=False)
+ team = models.ForeignKey(Team, null=True, on_delete=models.CASCADE, related_name="team_members")
+ lead_for = models.ManyToManyField("engagements.Organisation", blank=True, related_name="lead_inspector")
+ designation = models.CharField(max_length=3, null=True, blank=True)
+
+ USERNAME_FIELD = "email"
+ # EMAIL_FIELD = "email"
+
+ objects = TeamMgr()
+
+ def __str__(self):
+ return self.email
+
+ def fullname(self):
+ return f"{self.first_name} {self.last_name}"
+
+ def has_perm(self, perm, obj=None):
+ "Does this user have a specific permission? Set True for now."
+ return True
+
+ def has_module_perms(self, app_label):
+ "Does user have permissions to view the app `app_label`? Yes for now."
+ return True
+
+ @property
+ def is_staff(self):
+ return self.is_admin
diff --git a/myuser/tests.py b/myuser/tests.py
new file mode 100644
index 0000000..a79ca8b
--- /dev/null
+++ b/myuser/tests.py
@@ -0,0 +1,3 @@
+# from django.test import TestCase
+
+# Create your tests here.
diff --git a/myuser/views.py b/myuser/views.py
new file mode 100644
index 0000000..fd0e044
--- /dev/null
+++ b/myuser/views.py
@@ -0,0 +1,3 @@
+# from django.shortcuts import render
+
+# Create your views here.