From e16fa08b915562c6ab77ce7bb79a9d766b5a4036 Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Thu, 2 Nov 2017 16:03:14 -0400 Subject: Initial setup to use a custom User model, I still need to figure out how to use the built in Django permissions though --- app/dispatchAuth/models.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 app/dispatchAuth/models.py (limited to 'app/dispatchAuth/models.py') diff --git a/app/dispatchAuth/models.py b/app/dispatchAuth/models.py new file mode 100644 index 0000000..6c90dd0 --- /dev/null +++ b/app/dispatchAuth/models.py @@ -0,0 +1,69 @@ +from django.contrib.auth.models import AbstractUser, AbstractBaseUser, PermissionsMixin, BaseUserManager +from django.db import models + +# Create your models here. + + +class UserManager(BaseUserManager): + def create_user(self, email, first_name, last_name, password=None): + """ + Creates and saves a User with the given email, first name, + last name and password. + """ + if not email: + raise ValueError('Users must have an email address') + + user = self.model( + email=self.normalize_email(email), + first_name=first_name, + last_name=last_name, + ) + + user.set_password(password) + user.save(using=self._db) + return user + + def create_superuser(self, email, first_name, last_name, password): + """ + Creates and saves a superuser with the given email, first name, + last name and password. + """ + user = self.create_user( + email=email, + first_name=first_name, + last_name=last_name, + password=password, + ) + user.is_superuser = True + user.save(using=self._db) + return user + + +class User(PermissionsMixin, AbstractBaseUser): + email = models.EmailField(max_length=256, unique=True) + first_name = models.CharField(max_length=256) + last_name = models.CharField(max_length=256) + is_active = models.BooleanField(default=True) + is_superuser = models.BooleanField(default=False) + + USERNAME_FIELD = 'email' + EMAIL_FIELD = 'email' + REQUIRED_FIELDS = ['first_name', 'last_name'] + + objects = UserManager() + + def get_full_name(self): + return "{} {}".format(self.first_name, self.last_name) + + def get_short_name(self): + return self.first_name + + def get_absolute_url(self): + return reverse('driver_details', kwargs={'pk': self.pk}) + + def __str__(self): + return self.email + + @property + def is_staff(self): + return self.is_superuser -- cgit v1.2.3