TurTaskWeb/backend/users/managers.py
sosokker 15b8f1446e Add Authenticate API endpoint / Add CustomUser models
React will use this authenticate api endpoint
2023-10-27 21:39:59 +07:00

32 lines
1.3 KiB
Python

from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import BaseUserManager
class CustomAccountManager(BaseUserManager):
def create_superuser(self, email, username, first_name, password, **other_fields):
"""
Create a superuser with the given email, username, first_name, and password.
"""
other_fields.setdefault('is_staff', True)
other_fields.setdefault('is_superuser', True)
other_fields.setdefault('is_active', True)
if other_fields.get('is_staff') is not True:
raise ValueError('Superuser must be assigned to is_staff=True.')
if other_fields.get('is_superuser') is not True:
raise ValueError('Superuser must be assigned to is_superuser=True.')
return self.create_user(email, username, first_name, password, **other_fields)
def create_user(self, email, username, first_name, password, **other_fields):
"""
Create a user with the given email, username, first_name, and password.
"""
if not email:
raise ValueError(_('You must provide an email address'))
email = self.normalize_email(email)
user = self.model(email=email, username=username, first_name=first_name, **other_fields)
user.set_password(password)
user.save()
return user