From 7cf26b1d92f5daf43d67a0a048f9c64e5438cf0c Mon Sep 17 00:00:00 2001 From: Roman Hajnala Date: Fri, 14 Mar 2025 11:35:03 +0100 Subject: [PATCH] :bug: fixnuty bug s pridavanim uzivatela --- src/users/admin.py | 9 ++------- src/users/models.py | 41 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/users/admin.py b/src/users/admin.py index e57e20b..837b752 100644 --- a/src/users/admin.py +++ b/src/users/admin.py @@ -18,13 +18,8 @@ class MyUserCreationForm(UserCreationForm): @admin.register(models.User) class UserAdmin(AuthUserAdmin): form = MyUserChangeForm - add_form = MyUserCreationForm - list_display = ('username', 'first_name', 'last_name', - 'email', 'uid', ) - list_display_links = ('username',) - fieldsets = AuthUserAdmin.fieldsets + ( - (None, {'fields': ('uid', )}), - ) + list_display = ('username', 'first_name', 'last_name', 'email', 'uid', ) + @admin.register(models.ConfirmationCode) diff --git a/src/users/models.py b/src/users/models.py index ab3f23e..086eca5 100644 --- a/src/users/models.py +++ b/src/users/models.py @@ -1,16 +1,53 @@ import uuid -from django.contrib.auth.models import AbstractUser, UserManager +from django.contrib.auth.models import AbstractUser, BaseUserManager from django.db import models from django.utils.translation import gettext_lazy as _ from model_utils.models import TimeStampedModel, TimeFramedModel from core.utils import generate_random_string + +class MyUserManager(BaseUserManager): + def create_user(self, email, date_of_birth, password=None): + """ + Creates and saves a User with the given email, date of + birth and password. + """ + if not email: + raise ValueError('Users must have an email address') + + user = self.model( + email=self.normalize_email(email), + date_of_birth=date_of_birth, + ) + + user.set_password(password) + user.save(using=self._db) + return user + + def create_superuser(self, email, date_of_birth, password=None): + """ + Creates and saves a superuser with the given email, date of + birth and password. + """ + user = self.create_user( + email, + password=password, + date_of_birth=date_of_birth, + ) + user.is_admin = True + user.save(using=self._db) + return user + + + class User(AbstractUser): email = models.EmailField(_("email address"), blank=False, unique=True) uid = models.UUIDField(blank=False, null=False, verbose_name=_('UUID')) - objects = UserManager() + objects = MyUserManager() + + REQUIRED_FIELDS = ['email'] class Meta: verbose_name = _('User')