aboutsummaryrefslogtreecommitdiff
path: root/app/dispatchAuth/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/dispatchAuth/models.py')
-rw-r--r--app/dispatchAuth/models.py69
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