Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AccountBaseUserModel(AccountModel):
- """
- A base for user models which need a username, an email, a password and
- related password checking and setting functionality for login purposes.
- """
- # TODO: Decide whether to remove ``username`` from the equation, entirely
- username = models.CharField(
- _(u'username'), max_length=30, validators=[ensure_not_email])
- email = models.EmailField(_(u'email'))
- password = fields.PasswordField(_(u'password'))
- first_name = models.CharField(_(u'first name'), max_length=255, blank=True)
- last_name = models.CharField(_(u'last name'), max_length=255, blank=True)
- primary_phone = models.CharField(
- _(u'primary phone'), max_length=20, blank=True)
- is_active = models.BooleanField(_(u'is active'), default=True)
- objects = AccountBaseUserManager()
- class Meta(AccountModel.Meta):
- abstract = True
- unique_together = (('account', 'username'), ('account', 'email'))
- ordering = ('username',)
- def __unicode__(self):
- return u'{0} for {1}'.format(self.username, self.account)
- def check_password(self, raw_password):
- """
- Returns ``True``, if the the hashed value of ``raw_password`` matches
- the value of ``self.password``, else returns ``False``.
- """
- return get_hash(raw_password) == self.password
- def is_authenticated(self):
- """
- Returns True, if the user can authenticate.
- """
- return self.can_authenticate()
- def can_authenticate(self):
- """
- Returns ``True``, if the user is active, else returns ``False``.
- """
- return self.is_active
- def has_account_access(self, account):
- """
- Returns ``True``, if ``self.account`` matches the provided ``account``,
- else returns ``False``.
- """
- if self.can_authenticate():
- return self.account_id == account.pk
- return False
- def set_password(self, raw_password):
- """
- Sets ``self.password`` to the hashed value of ``raw_password``.
- """
- self.password = get_hash(raw_password)
- self.save()
- class AnonAccountUser(object):
- """
- Provides a similar API to ``AccountBaseUserModel`` unauthenticated users.
- """
- def __init__(self):
- self.username = u'Anonymous'
- def is_authenticated(self):
- """
- Always returns False.
- """
- return False
Add Comment
Please, Sign In to add comment