spradio-server-django/savepointradio/core/managers.py
2020-01-21 13:20:07 -05:00

43 lines
1.5 KiB
Python

from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import gettext_lazy as _
class RadioUserManager(BaseUserManager):
'''
Custom user model manager where email is the unique identifier for
authentication instead of the username.
Credit for assistance goes to Michael Herman:
https://testdriven.io/blog/django-custom-user-model/
'''
def create_user(self, email, password=None, **extra_fields):
'''
Create and save a user.
'''
if not email:
raise ValueError(_('You must provide an email.'))
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_superuser(self, email, password=None, **extra_fields):
'''
Create and save a superuser.
'''
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)
extra_fields.setdefault('is_dj', False)
if extra_fields.get('is_staff') is not True:
raise ValueError(
_('A superuser must have "is_staff" set to True.')
)
if extra_fields.get('is_superuser') is not True:
raise ValueError(
_('A superuser must have "is_supervisor" set to True.')
)
return self.create_user(email, password, **extra_fields)