diff options
| author | Mitch Riedstra <Mitch@riedstra.us> | 2017-11-02 16:03:14 -0400 |
|---|---|---|
| committer | Mitch Riedstra <Mitch@riedstra.us> | 2017-11-02 16:03:14 -0400 |
| commit | e16fa08b915562c6ab77ce7bb79a9d766b5a4036 (patch) | |
| tree | 2f7d518131600f2db7bc3ac0545f11f2571c2aee /app/dispatchAuth/models.py | |
| parent | e4d865b1a61f6a72551e70abad78c6c35b9345e7 (diff) | |
| download | dispatch-tracker-e16fa08b915562c6ab77ce7bb79a9d766b5a4036.tar.gz dispatch-tracker-e16fa08b915562c6ab77ce7bb79a9d766b5a4036.tar.xz | |
Initial setup to use a custom User model, I still need to figure out how to use the built in Django permissions though
Diffstat (limited to 'app/dispatchAuth/models.py')
| -rw-r--r-- | app/dispatchAuth/models.py | 69 |
1 files changed, 69 insertions, 0 deletions
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 |
