Skip to content

Commit

Permalink
Custom user WIP #130
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofMadejski committed Nov 5, 2019
1 parent 50517f1 commit d16080c
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions moonsheep/users.py
Original file line number Diff line number Diff line change
@@ -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',)

0 comments on commit d16080c

Please sign in to comment.