Created
March 21, 2016 17:35
-
-
Save eliagbenu/651ad364c4942efea37c 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
## in models.py | |
class CustomerUserManager(BaseUserManager): | |
def _create_user(self, national_id, password, | |
is_staff, is_superuser, **extra_fields): | |
now = timezone.now() | |
if not national_id: | |
raise ValueError('Please provide a national id') | |
national_id = self.normalize_national_id(national_id) | |
user = self.model(national_id=national_id, | |
is_staff=is_staff, is_active=True, | |
is_superuser=is_superuser, last_login=now, | |
created_at=now, **extra_fields) | |
user.set_password(password) | |
user.save(using=self._db) | |
return user | |
def create_user(self, national_id=None, password=None, **extra_fields): | |
return self._create_user( national_id, password, False, False,**extra_fields) | |
def create_superuser(self, national_id, password, **extra_fields): | |
return self._create_user( national_id, password, True, True, | |
**extra_fields) | |
def normalize_national_id(cls, national_id): | |
return national_id | |
class CustomUser(AbstractBaseUser,PermissionsMixin): | |
national_id = models.CharField(max_length=12,unique=True) | |
is_staff = models.BooleanField(_('staff status'), default=False, | |
help_text=_('Designates whether the user can log into this admin site.')) | |
is_active = models.BooleanField(_('active'), default=True, | |
help_text=_('Designates whether this user should be treated as active. Unselect this instead of deleting accounts.')) | |
created_at = models.DateTimeField(_('date created'), default=timezone.now) | |
updated_at = models.DateTimeField(_('last modified'), default=timezone.now) | |
USERNAME_FIELD = 'national_id' | |
REQUIRED_FIELDS = [] | |
objects = CustomUserManager() | |
def __unicode__(self): | |
return self.national_id | |
# in settings.py | |
# assume the app name is app_1 | |
AUTH_USER_MODEL = 'app_1.CustomUser' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment