29 lines
1.1 KiB
Python
29 lines
1.1 KiB
Python
from django.contrib.auth.models import BaseUserManager
|
|
from django.utils.translation import gettext as _
|
|
|
|
class UserProfileManager(BaseUserManager):
|
|
def create_user(self,username:str, email:str,password=None,**extra_fields):
|
|
if not email:
|
|
raise ValueError(_("The email must be set!"))
|
|
if not username:
|
|
raise ValueError(_("Username must be set!"))
|
|
|
|
email = self.normalize_email(email)
|
|
user = self.model(username=username,email=email,**extra_fields)
|
|
user.set_password(password)
|
|
user.save(using=self._db)
|
|
|
|
return user
|
|
|
|
def create_superuser(self, username:str, email:str, password=None,**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)
|
|
|