Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.db import models
- from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
- from django.conf import settings
- class MyUserManager(BaseUserManager):
- def create_user(self, email, password=None):
- """
- Creates and saves a User with the given email and password
- """
- if not email:
- raise ValueError('Users must have an email address')
- user = self.model(email=self.normalize_email(email))
- user.set_password(password)
- user.save(using=self._db)
- return user
- def create_superuser(self, email, password):
- """
- Creates and saves a superuser with the given email and password.
- """
- user = self.create_user(email, password=password)
- user.is_admin = True
- user.save(using=self._db)
- return user
- class MyUser(AbstractBaseUser):
- """Custom User Model so that a customer's username is their email address.
- This makes the account creation process easier as they don't have to
- think of and remember a username.
- """
- email = models.EmailField(unique=True)
- full_name = models.CharField(max_length=255, blank=True)
- first_name = models.CharField(max_length=255, blank=True)
- is_active = models.BooleanField(default=True)
- is_admin = models.BooleanField(default=False)
- objects = MyUserManager()
- USERNAME_FIELD = 'email'
- def get_full_name(self):
- # Identify User by first and last name if available. If not, return self.email
- try:
- return self.first_name + ' ' + self.last_name
- except:
- return self.email
- def get_short_name(self):
- # Identify User by first name if available. If not, return self.email
- try:
- return self.first_name
- except:
- return self.email
- def __str__(self):
- return self.email
- def has_perm(self, perm, obj=None):
- "Does the user have a specific permission?"
- return True
- def has_module_perms(self, core):
- "Does the user have permissions to view the core application?"
- return True
- @property
- def is_staff(self):
- "Is the user a member of staff?"
- return self.is_admin
- class Company(models.Model):
- """Model to provide the association between a company, and what could
- be multiple users (Contacts) at that company. See Contact Model.
- """
- company = models.CharField(max_length=50, blank=True)
- website = models.CharField(max_length=200, blank=True)
- address_one = models.CharField(max_length=50, blank=True)
- address_two = models.CharField(max_length=50, blank=True)
- city = models.CharField(max_length=30, blank=True)
- state = models.CharField(max_length=30, blank=True)
- zipcode = models.CharField(max_length=10, blank=True)
- phone = models.CharField(max_length=25, blank=True)
- notes = models.TextField(blank=True)
- def __str__(self):
- return str(self.company)
- class Contact(models.Model):
- """Model to create an association with a Company for a Contact (if needed)."""
- user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
- account_rep = models.ForeignKey('Employee', blank=True, null=True)
- company = models.ForeignKey('Company', on_delete=models.CASCADE, blank=True)
- address_one = models.CharField(max_length=50, blank=True)
- address_two = models.CharField(max_length=50, blank=True)
- city = models.CharField(max_length=30, blank=True)
- state = models.CharField(max_length=30, blank=True)
- zipcode = models.CharField(max_length=10, blank=True)
- phone = models.CharField(max_length=25, blank=True)
- notes = models.TextField(blank=True)
- def __str__(self):
- if self.first_name and self.last_name:
- return str(self.first_name + ' ' + self.last_name)
- class Employee(models.Model):
- """Model to identify Employee User accounts with their associated
- departments at Southeast.
- """
- SALESREP = 'SR'
- CUSTOMERSERVICEREP = 'CSR'
- ART = 'ART'
- PRODUCTION = 'PROD'
- DEPARTMENTS = (
- (SALESREP, 'Sales'),
- (CUSTOMERSERVICEREP, 'Customer Service'),
- (ART, 'Art'),
- (PRODUCTION, 'Production'),
- )
- user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
- department = models.CharField(max_length=4, choices=DEPARTMENTS, default=ART)
- address_one = models.CharField(max_length=50, blank=True)
- address_two = models.CharField(max_length=50, blank=True)
- city = models.CharField(max_length=30, blank=True)
- state = models.CharField(max_length=30, blank=True)
- zipcode = models.CharField(max_length=10, blank=True)
- phone = models.CharField(max_length=25, blank=True)
- def __str__(self):
- if self.first_name and self.last_name:
- return str(self.first_name + ' ' + self.last_name)
- else:
- return str(self.user)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement