76 lines
2.0 KiB
Python
76 lines
2.0 KiB
Python
# The custom User uses email as the unique identifier, and requires
|
|
# that every user provide a date of birth. This lets us test
|
|
# changes in username datatype, and non-text required fields.
|
|
|
|
from django.db import models
|
|
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
|
|
|
|
|
|
class CustomUserManager(BaseUserManager):
|
|
def create_user(self, email, date_of_birth, password=None):
|
|
"""
|
|
Creates and saves a User with the given email and password.
|
|
"""
|
|
if not email:
|
|
raise ValueError('Users must have an email address')
|
|
|
|
user = self.model(
|
|
email=CustomUserManager.normalize_email(email),
|
|
date_of_birth=date_of_birth,
|
|
)
|
|
|
|
user.set_password(password)
|
|
user.save(using=self._db)
|
|
return user
|
|
|
|
def create_superuser(self, username, password, date_of_birth):
|
|
u = self.create_user(username, password=password, date_of_birth=date_of_birth)
|
|
u.is_admin = True
|
|
u.save(using=self._db)
|
|
return u
|
|
|
|
|
|
class CustomUser(AbstractBaseUser):
|
|
email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
|
|
is_active = models.BooleanField(default=True)
|
|
is_admin = models.BooleanField(default=False)
|
|
date_of_birth = models.DateField()
|
|
|
|
objects = CustomUserManager()
|
|
|
|
USERNAME_FIELD = 'email'
|
|
REQUIRED_FIELDS = ['date_of_birth']
|
|
|
|
class Meta:
|
|
app_label = 'auth'
|
|
|
|
def get_full_name(self):
|
|
return self.email
|
|
|
|
def get_short_name(self):
|
|
return self.email
|
|
|
|
def __unicode__(self):
|
|
return self.email
|
|
|
|
# Maybe required?
|
|
def get_group_permissions(self, obj=None):
|
|
return set()
|
|
|
|
def get_all_permissions(self, obj=None):
|
|
return set()
|
|
|
|
def has_perm(self, perm, obj=None):
|
|
return True
|
|
|
|
def has_perms(self, perm_list, obj=None):
|
|
return True
|
|
|
|
def has_module_perms(self, app_label):
|
|
return True
|
|
|
|
# Admin required fields
|
|
@property
|
|
def is_staff(self):
|
|
return self.is_admin
|