Created
May 27, 2013 12:18
-
-
Save mxub/5656782 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#employees/models.py | |
from django.db import models | |
from django.contrib.auth.models import ( | |
BaseUserManager, AbstractBaseUser, PermissionsMixin | |
) | |
class MinimalUserManager(BaseUserManager): | |
def create_user(self, email, password): | |
""" | |
Creates an minimal User just with email and password | |
""" | |
if not email: | |
msg = "Users must have an email address" | |
raise ValueError(msg) | |
user = self.model( | |
email=MinimalUserManager.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.is_staff = True | |
user.is_superuser = True | |
user.save(using=self._db) | |
return user | |
class MinimalUser(AbstractBaseUser, PermissionsMixin): | |
""" | |
Inherits from both the AbstractBaseUser and PermissionMixin. | |
""" | |
email = models.EmailField( | |
verbose_name="email address", | |
max_length=255, | |
unique=True, | |
db_index=True, | |
) | |
USERNAME_FIELD = "email" | |
 | |
is_active = models.BooleanField(default=True) | |
is_admin = models.BooleanField(default=False) | |
is_staff = models.BooleanField(default=False) | |
def __unicode__(self): | |
return self.email | |
class Employee(models.model): | |
MALE = 'M' | |
FEMALE = 'F' | |
GENDER_CHOICES = ( | |
(MALE, 'Male'), | |
(FEMALE, 'Female'), | |
) | |
first_name = models.CharField(max_length=50) | |
last_name = models.CharField(max_length=50) | |
gender = models.CharField( max_length=1, | |
choices=GENDER_CHOICES, | |
default=FEMALE) | |
birthday = models.DateField() | |
login = models.OneToOneField(settings.AUTH_USER_MODEL, null=True, blank=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment