Advertisement
Guest User

Core Models

a guest
Dec 12th, 2016
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.98 KB | None | 0 0
  1. from django.db import models
  2. from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
  3. from django.conf import settings
  4.  
  5.  
  6. class MyUserManager(BaseUserManager):
  7.     def create_user(self, email, password=None):
  8.         """
  9.        Creates and saves a User with the given email and password
  10.        """
  11.         if not email:
  12.             raise ValueError('Users must have an email address')
  13.  
  14.         user = self.model(email=self.normalize_email(email))
  15.  
  16.         user.set_password(password)
  17.         user.save(using=self._db)
  18.         return user
  19.  
  20.     def create_superuser(self, email, password):
  21.         """
  22.        Creates and saves a superuser with the given email and password.
  23.        """
  24.         user = self.create_user(email, password=password)
  25.         user.is_admin = True
  26.         user.save(using=self._db)
  27.         return user
  28.  
  29.  
  30. class MyUser(AbstractBaseUser):
  31.     """Custom User Model so that a customer's username is their email address.
  32.    This makes the account creation process easier as they don't have to
  33.    think of and remember a username.
  34.    """
  35.     email = models.EmailField(unique=True)
  36.     full_name = models.CharField(max_length=255, blank=True)
  37.     first_name = models.CharField(max_length=255, blank=True)
  38.     is_active = models.BooleanField(default=True)
  39.     is_admin = models.BooleanField(default=False)
  40.  
  41.     objects = MyUserManager()
  42.  
  43.     USERNAME_FIELD = 'email'
  44.  
  45.     def get_full_name(self):
  46.         # Identify User by first and last name if available.  If not, return self.email
  47.         try:
  48.             return self.first_name + ' ' + self.last_name
  49.         except:
  50.             return self.email
  51.  
  52.     def get_short_name(self):
  53.         # Identify User by first name if available.  If not, return self.email
  54.         try:
  55.             return self.first_name
  56.         except:
  57.             return self.email
  58.  
  59.     def __str__(self):
  60.         return self.email
  61.  
  62.     def has_perm(self, perm, obj=None):
  63.         "Does the user have a specific permission?"
  64.         return True
  65.  
  66.     def has_module_perms(self, core):
  67.         "Does the user have permissions to view the core application?"
  68.         return True
  69.  
  70.     @property
  71.     def is_staff(self):
  72.         "Is the user a member of staff?"
  73.         return self.is_admin
  74.  
  75.  
  76. class Company(models.Model):
  77.     """Model to provide the association between a company, and what could
  78.    be multiple users (Contacts) at that company.  See Contact Model.
  79.    """
  80.     company = models.CharField(max_length=50, blank=True)
  81.     website = models.CharField(max_length=200, blank=True)
  82.     address_one = models.CharField(max_length=50, blank=True)
  83.     address_two = models.CharField(max_length=50, blank=True)
  84.     city = models.CharField(max_length=30, blank=True)
  85.     state = models.CharField(max_length=30, blank=True)
  86.     zipcode = models.CharField(max_length=10, blank=True)
  87.     phone = models.CharField(max_length=25, blank=True)
  88.     notes = models.TextField(blank=True)
  89.  
  90.     def __str__(self):
  91.         return str(self.company)
  92.  
  93.  
  94. class Contact(models.Model):
  95.     """Model to create an association with a Company for a Contact (if needed)."""
  96.     user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
  97.     account_rep = models.ForeignKey('Employee', blank=True, null=True)
  98.     company = models.ForeignKey('Company', on_delete=models.CASCADE, blank=True)
  99.     address_one = models.CharField(max_length=50, blank=True)
  100.     address_two = models.CharField(max_length=50, blank=True)
  101.     city = models.CharField(max_length=30, blank=True)
  102.     state = models.CharField(max_length=30, blank=True)
  103.     zipcode = models.CharField(max_length=10, blank=True)
  104.     phone = models.CharField(max_length=25, blank=True)
  105.     notes = models.TextField(blank=True)
  106.  
  107.     def __str__(self):
  108.         if self.first_name and self.last_name:
  109.             return str(self.first_name + ' ' + self.last_name)
  110.  
  111.  
  112. class Employee(models.Model):
  113.     """Model to identify Employee User accounts with their associated
  114.     departments at Southeast.
  115.    """
  116.     SALESREP = 'SR'
  117.     CUSTOMERSERVICEREP = 'CSR'
  118.     ART = 'ART'
  119.     PRODUCTION = 'PROD'
  120.     DEPARTMENTS = (
  121.         (SALESREP, 'Sales'),
  122.         (CUSTOMERSERVICEREP, 'Customer Service'),
  123.         (ART, 'Art'),
  124.         (PRODUCTION, 'Production'),
  125.     )
  126.     user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
  127.     department = models.CharField(max_length=4, choices=DEPARTMENTS, default=ART)
  128.     address_one = models.CharField(max_length=50, blank=True)
  129.     address_two = models.CharField(max_length=50, blank=True)
  130.     city = models.CharField(max_length=30, blank=True)
  131.     state = models.CharField(max_length=30, blank=True)
  132.     zipcode = models.CharField(max_length=10, blank=True)
  133.     phone = models.CharField(max_length=25, blank=True)
  134.  
  135.     def __str__(self):
  136.         if self.first_name and self.last_name:
  137.             return str(self.first_name + ' ' + self.last_name)
  138.         else:
  139.             return str(self.user)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement