From d16080cd3ebe8a8a8f853c00e31ac69452a85f08 Mon Sep 17 00:00:00 2001 From: Krzysztof Madejski Date: Tue, 5 Nov 2019 14:01:28 +0100 Subject: [PATCH] Custom user WIP #130 --- moonsheep/users.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 moonsheep/users.py diff --git a/moonsheep/users.py b/moonsheep/users.py new file mode 100644 index 0000000..9c0e901 --- /dev/null +++ b/moonsheep/users.py @@ -0,0 +1,81 @@ +from django.contrib.auth.models import AbstractUser, BaseUserManager +from django.db import models +from django.utils.translation import ugettext_lazy as _ + + +class UserManager(BaseUserManager): + """ + Define a model manager for User model with no username field. + + Copied and modified from django.contrib.auth.models.UserManager + """ + + use_in_migrations = True + + def _create_user(self, username, email, password, **extra_fields): + """ + Create and save a user with the given username, email, and password. + """ + if not username: + raise ValueError('The given username must be set') + email = self.normalize_email(email) + username = self.model.normalize_username(username) + user = self.model(username=username, email=email, **extra_fields) + user.set_password(password) + user.save(using=self._db) + return user + + + def create_user(self, username, email=None, password=None, **extra_fields): + extra_fields.setdefault('is_staff', False) + extra_fields.setdefault('is_superuser', False) + return self._create_user(username, email, password, **extra_fields) + + + def create_superuser(self, username, email, password, **extra_fields): + 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(username, email, password, **extra_fields) + + + +class User(AbstractUser): + objects = UserManager() + + username = None + email = models.EmailField(_('email address'), unique=True) + + USERNAME_FIELD = 'email' + REQUIRED_FIELDS = [] + + +# from django.contrib import admin +# from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin +# +# +# @admin.register(User) +# class UserAdmin(DjangoUserAdmin): +# """Define admin model for custom User model with no username field.""" +# +# fieldsets = ( +# (None, {'fields': ('email', 'password')}), +# (_('Personal info'), {'fields': ('first_name', 'last_name')}), +# (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser', +# 'groups', 'user_permissions')}), +# (_('Important dates'), {'fields': ('last_login', 'date_joined')}), +# ) +# add_fieldsets = ( +# (None, { +# 'classes': ('wide',), +# 'fields': ('email', 'password1', 'password2'), +# }), +# ) +# list_display = ('email', 'first_name', 'last_name', 'is_staff') +# search_fields = ('email', 'first_name', 'last_name') +# ordering = ('email',)