Advertisement
Guest User

Untitled

a guest
Mar 24th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1.  
  2. ## in models.py
  3.  
  4. class CustomerUserManager(BaseUserManager):
  5.  
  6. def _create_user(self, national_id, password,
  7. is_staff, is_superuser, **extra_fields):
  8. now = timezone.now()
  9.  
  10. if not national_id:
  11. raise ValueError('Please provide a national id')
  12.  
  13. national_id = self.normalize_national_id(national_id)
  14. user = self.model(national_id=national_id,
  15. is_staff=is_staff, is_active=True,
  16. is_superuser=is_superuser, last_login=now,
  17. created_at=now, **extra_fields)
  18. user.set_password(password)
  19. user.save(using=self._db)
  20. return user
  21.  
  22.  
  23. def create_user(self, national_id=None, password=None, **extra_fields):
  24. return self._create_user( national_id, password, False, False,**extra_fields)
  25.  
  26. def create_superuser(self, national_id, password, **extra_fields):
  27. return self._create_user( national_id, password, True, True,
  28. **extra_fields)
  29.  
  30.  
  31. def normalize_national_id(cls, national_id):
  32. return national_id
  33.  
  34.  
  35.  
  36. class CustomUser(AbstractBaseUser,PermissionsMixin):
  37. national_id = models.CharField(max_length=12,unique=True)
  38. is_staff = models.BooleanField(_('staff status'), default=False,
  39. help_text=_('Designates whether the user can log into this admin site.'))
  40. is_active = models.BooleanField(_('active'), default=True,
  41. help_text=_('Designates whether this user should be treated as active. Unselect this instead of deleting accounts.'))
  42. created_at = models.DateTimeField(_('date created'), default=timezone.now)
  43. updated_at = models.DateTimeField(_('last modified'), default=timezone.now)
  44.  
  45. USERNAME_FIELD = 'national_id'
  46. REQUIRED_FIELDS = []
  47.  
  48. objects = CustomUserManager()
  49.  
  50. def __unicode__(self):
  51. return self.national_id
  52.  
  53.  
  54. # in settings.py
  55. # assume the app name is app_1
  56.  
  57. AUTH_USER_MODEL = 'app_1.CustomUser'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement