Skip to content

Commit

Permalink
Merge pull request #703 from Amsterdam/fix/92779-user-creation
Browse files Browse the repository at this point in the history
Fix user creation
  • Loading branch information
NvdLaan authored Sep 2, 2024
2 parents 87152cf + 119bcba commit f9e96e9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 37 deletions.
20 changes: 20 additions & 0 deletions app/apps/users/migrations/0011_alter_user_managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.15 on 2024-09-02 11:41

import django.contrib.auth.models
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("users", "0010_user_team_settings"),
]

operations = [
migrations.AlterModelManagers(
name="user",
managers=[
("objects", django.contrib.auth.models.UserManager()),
],
),
]
5 changes: 1 addition & 4 deletions app/apps/users/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import datetime
import uuid

from apps.users.user_manager import UserManager
from apps.users.utils import generate_username
from django.contrib.auth.models import AbstractUser
from django.db import models
Expand All @@ -26,9 +25,7 @@ class Meta:
)

USERNAME_FIELD = "email"
REQUIRED_FIELDS = []

objects = UserManager()
REQUIRED_FIELDS = ["username"]

@property
def get_current_itinerary(self):
Expand Down
65 changes: 34 additions & 31 deletions app/apps/users/user_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,37 @@


class UserManager(BaseUserManager):
"""Define a model manager for User model with no username field."""

use_in_migrations = True

def _create_user(self, email, password, **extra_fields):
"""Create and save a User with the given email and password."""
if not email:
raise ValueError("The given email must be set")
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user

def create_user(self, email, password=None, **extra_fields):
"""Create and save a regular User with the given email and password."""
extra_fields.setdefault("is_staff", False)
extra_fields.setdefault("is_superuser", False)
return self._create_user(email, password, **extra_fields)

def create_superuser(self, email, password, **extra_fields):
"""Create and save a SuperUser with the given email and password."""
extra_fields.setdefault("is_staff", True)
extra_fields.setdefault("is_superuser", True)

if extra_fields.get("is_staff") is not True:
raise ValueError("Superuser must have is_staff=True.")
if extra_fields.get("is_superuser") is not True:
raise ValueError("Superuser must have is_superuser=True.")

return self._create_user(email, password, **extra_fields)
"""
This model isn't used anymore but can't be deleted because of the reference in 0001_initial.py
If the migrations are applied in production, this can be removed.
"""

# use_in_migrations = True

# def _create_user(self, email, password, **extra_fields):
# """Create and save a User with the given email and password."""
# if not email:
# raise ValueError("The given email must be set")
# email = self.normalize_email(email)
# user = self.model(email=email, **extra_fields)
# user.set_password(password)
# user.save(using=self._db)
# return user

# def create_user(self, email, password=None, **extra_fields):
# """Create and save a regular User with the given email and password."""
# extra_fields.setdefault("is_staff", False)
# extra_fields.setdefault("is_superuser", False)
# return self._create_user(email, password, **extra_fields)

# def create_superuser(self, email, password, **extra_fields):
# """Create and save a SuperUser with the given email and password."""
# extra_fields.setdefault("is_staff", True)
# extra_fields.setdefault("is_superuser", True)

# if extra_fields.get("is_staff") is not True:
# raise ValueError("Superuser must have is_staff=True.")
# if extra_fields.get("is_superuser") is not True:
# raise ValueError("Superuser must have is_superuser=True.")

# return self._create_user(email, password, **extra_fields)
3 changes: 1 addition & 2 deletions app/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ django-celery-results==2.5.1
django-timezone-field<7.0

# Authentication
datapunt-keycloak-oidc @ git+https://github.com/remyvdwereld/keycloak_oidc_top.git@v1.1
datapunt-keycloak-oidc @ git+https://github.com/remyvdwereld/keycloak_oidc_top.git@main
djangorestframework-simplejwt==5.3.1

# Algorithm toolbox
geopy==2.4.1
joblib==1.3.2
Expand Down

0 comments on commit f9e96e9

Please sign in to comment.