Skip to content

Commit

Permalink
3 - Create the User Model Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
codingforentrepreneurs committed Oct 9, 2017
1 parent c698e75 commit 968ec05
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/accounts/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
from django.db import models
from django.contrib.auth.models import (
AbstractBaseUser
AbstractBaseUser, BaseUserManager
)


class UserManager(BaseUserManager):
def create_user(self, email, password=None, is_active=True, is_staff=False, is_admin=False):
if not email:
raise ValueError("Users must have an email address")
if not password:
raise ValueError("Users must have a password")

user_obj = self.model(
email = self.normalize_email(email)
)
user_obj.set_password(password) # change user password
user_obj.staff = is_staff
user_obj.admin = is_admin
user_obj.active = is_active
user_obj.save(using=self._db)
return user_obj

def create_staffuser(self, email, password=None):
user = self.create_user(
email,
password=password,
is_staff=True
)
return user

def create_superuser(self, email, password=None):
user = self.create_user(
email,
password=password,
is_staff=True,
is_admin=True
)
return user


class User(AbstractBaseUser):
email = models.EmailField(max_length=255, unique=True)
# full_name = models.CharField(max_length=255, blank=True, null=True)
Expand All @@ -17,6 +53,8 @@ class User(AbstractBaseUser):
# USERNAME_FIELD and password are required by default
REQUIRED_FIELDS = [] #['full_name'] #python manage.py createsuperuser

objects = UserManager()

def __str__(self):
return self.email

Expand Down

0 comments on commit 968ec05

Please sign in to comment.