Guest User

Untitled

a guest
Oct 8th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. class AccountBaseUserModel(AccountModel):
  2. """
  3. A base for user models which need a username, an email, a password and
  4. related password checking and setting functionality for login purposes.
  5. """
  6. # TODO: Decide whether to remove ``username`` from the equation, entirely
  7. username = models.CharField(
  8. _(u'username'), max_length=30, validators=[ensure_not_email])
  9. email = models.EmailField(_(u'email'))
  10. password = fields.PasswordField(_(u'password'))
  11. first_name = models.CharField(_(u'first name'), max_length=255, blank=True)
  12. last_name = models.CharField(_(u'last name'), max_length=255, blank=True)
  13. primary_phone = models.CharField(
  14. _(u'primary phone'), max_length=20, blank=True)
  15. is_active = models.BooleanField(_(u'is active'), default=True)
  16.  
  17. objects = AccountBaseUserManager()
  18.  
  19. class Meta(AccountModel.Meta):
  20. abstract = True
  21. unique_together = (('account', 'username'), ('account', 'email'))
  22. ordering = ('username',)
  23.  
  24.  
  25. def __unicode__(self):
  26. return u'{0} for {1}'.format(self.username, self.account)
  27.  
  28. def check_password(self, raw_password):
  29. """
  30. Returns ``True``, if the the hashed value of ``raw_password`` matches
  31. the value of ``self.password``, else returns ``False``.
  32. """
  33. return get_hash(raw_password) == self.password
  34.  
  35. def is_authenticated(self):
  36. """
  37. Returns True, if the user can authenticate.
  38. """
  39. return self.can_authenticate()
  40.  
  41. def can_authenticate(self):
  42. """
  43. Returns ``True``, if the user is active, else returns ``False``.
  44. """
  45. return self.is_active
  46.  
  47. def has_account_access(self, account):
  48. """
  49. Returns ``True``, if ``self.account`` matches the provided ``account``,
  50. else returns ``False``.
  51. """
  52. if self.can_authenticate():
  53. return self.account_id == account.pk
  54. return False
  55.  
  56. def set_password(self, raw_password):
  57. """
  58. Sets ``self.password`` to the hashed value of ``raw_password``.
  59. """
  60. self.password = get_hash(raw_password)
  61. self.save()
  62.  
  63.  
  64. class AnonAccountUser(object):
  65. """
  66. Provides a similar API to ``AccountBaseUserModel`` unauthenticated users.
  67. """
  68. def __init__(self):
  69. self.username = u'Anonymous'
  70.  
  71. def is_authenticated(self):
  72. """
  73. Always returns False.
  74. """
  75. return False
Add Comment
Please, Sign In to add comment