-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Put it in a separate app so it can be shared by the frontend and the admin interface, which will in turn be a separate app - No username — have email as the primary identifier for users - Most users won't need passwords and Django needs the column for the admin interface, so default to... a uuid4? - Punt the has_perm and has_module_perms questions for now and have both return True for everything
- Loading branch information
1 parent
09ade5e
commit 08b9d73
Showing
7 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class AuthConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "consultation_analyser.authentication" |
47 changes: 47 additions & 0 deletions
47
consultation_analyser/authentication/migrations/0001_initial.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Generated by Django 5.0.4 on 2024-04-11 17:09 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [] | ||
|
||
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" | ||
), | ||
), | ||
( | ||
"email", | ||
models.EmailField( | ||
max_length=255, unique=True, verbose_name="email address" | ||
), | ||
), | ||
("created_at", models.DateTimeField(auto_now_add=True)), | ||
("modified_at", models.DateTimeField(auto_now=True)), | ||
("is_staff", models.BooleanField(default=True)), | ||
("is_active", models.BooleanField(default=True)), | ||
], | ||
options={ | ||
"abstract": False, | ||
}, | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import uuid | ||
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager | ||
from django.core.validators import validate_email | ||
from django.db import models | ||
|
||
|
||
class UserManager(BaseUserManager): | ||
def create_user(self, email, password=None): | ||
# If there's no pw, assign a long random one that won't be used | ||
if password is None: | ||
password = uuid.uuid4() | ||
|
||
user = self.model( | ||
email=self.normalize_email(email), | ||
password=password, | ||
) | ||
|
||
user.full_clean() | ||
|
||
user.save(using=self._db) | ||
return user | ||
|
||
|
||
class User(AbstractBaseUser): | ||
email = models.EmailField( | ||
verbose_name="email address", | ||
max_length=255, | ||
unique=True, | ||
) | ||
|
||
created_at = models.DateTimeField(editable=False, auto_now_add=True) | ||
modified_at = models.DateTimeField(editable=False, auto_now=True) | ||
|
||
objects = UserManager() | ||
|
||
USERNAME_FIELD = "email" | ||
|
||
# boilerplate required by django admin | ||
is_staff = models.BooleanField(default=True) | ||
is_active = models.BooleanField(default=True) | ||
|
||
def has_perm(self, perm, obj=None): | ||
return True | ||
|
||
def has_module_perms(self, app_label): | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import pytest | ||
|
||
from consultation_analyser.authentication.models import User | ||
from django.core.exceptions import ValidationError | ||
|
||
@pytest.mark.django_db | ||
def test_create_valid_user(): | ||
user = User.objects.create_user(email="[email protected]") | ||
assert user.id | ||
|
||
@pytest.mark.django_db | ||
def test_create_user_with_invalid_email(): | ||
with pytest.raises(ValidationError): | ||
User.objects.create_user(email="sdfdsf") | ||
|
||
@pytest.mark.django_db | ||
def test_create_user_with_duplicate_email(): | ||
User.objects.create_user(email="[email protected]") | ||
|
||
with pytest.raises(ValidationError): | ||
User.objects.create_user(email="[email protected]") |