Created
March 21, 2016 10:32
-
-
Save m3ck0/4c3dd4b2b3aae9a5df51 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
from django.contrib.auth.models import AbstractBaseUser | |
from django.contrib.auth.models import BaseUserManager | |
from django.utils.translation import ugettext_lazy as _ | |
from django.db import models | |
# Create your models here. | |
class AccountManager(BaseUserManager): | |
def create_user(self, email, password=None, **kwargs): | |
if not email: | |
raise ValueError(_("User must have email")) | |
if not kwargs.get('firstname') or not kwargs.get('lastname'): | |
raise ValueError(_("User must have firstname and lastname")) | |
account = self.model( | |
email=self.normalize_email(email), | |
firstname=kwargs.get('firstname'), | |
lastname=kwargs.get('lastname')) | |
account.set_password(password) | |
account.save() | |
return account | |
def create_superuser(self, email, password, **kwargs): | |
account = self.create_user(email, password, **kwargs) | |
account.is_admin = True | |
account.is_staff = True | |
account.save() | |
return account | |
class Account(AbstractBaseUser, ): | |
SEX_CHOICE = ( | |
("M", _("Male")), | |
("F", _("Female")), | |
("U", _("Unspecified")), | |
) | |
email = models.EmailField(unique=True, db_index=True) | |
firstname = models.CharField(_("First name"), max_length=40) | |
lastname = models.CharField(_("Last name"), max_length=40) | |
sex = models.CharField(_("Sex"), max_length=1, choices=SEX_CHOICE, default="U") | |
age = models.IntegerField(blank=True, null=True) | |
status = models.CharField(_("Status"), max_length=10, default="inactive") | |
is_active = models.BooleanField(default=True) | |
is_admin = models.BooleanField(default=False) | |
is_staff = models.BooleanField(default=False) | |
created_at = models.DateTimeField(auto_now_add=True) | |
updated_at = models.DateTimeField(auto_now=True) | |
objects = AccountManager() | |
USERNAME_FIELD = 'email' | |
REQUIRED_FIELDS = ['firstname', 'lastname'] | |
def get_full_name(self): | |
return "{0} {1}".format(self.firstname, self.lastname) | |
def get_short_name(self): | |
return self.lastname | |
@property | |
def is_super_user(self): | |
return self.is_admin | |
def has_perm(self, perm, obj=None): | |
return self.is_admin | |
def has_module_perms(self, app_label): | |
return self.is_admin | |
def __str__(self): | |
return self.email |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment