29 lines
1.2 KiB
Python
29 lines
1.2 KiB
Python
from django.contrib.auth.models import BaseUserManager
|
|
|
|
|
|
class UserManager(BaseUserManager):
|
|
def create_user(self, email, password=None, **extra_fields):
|
|
if not email:
|
|
raise ValueError("The Email field must be set")
|
|
email = self.normalize_email(email)
|
|
# Mirror the User.save() invariant so callers reading the manager
|
|
# in isolation see the contract: name defaults to email-local-part
|
|
# when not provided.
|
|
if not extra_fields.get("name"):
|
|
extra_fields["name"] = email.split("@", 1)[0][:255]
|
|
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):
|
|
extra_fields.setdefault("is_superuser", True)
|
|
extra_fields.setdefault("is_staff", 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(email, password, **extra_fields)
|