Guest User

Untitled

a guest
Jul 23rd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. ###
  2. ### User Document
  3. ###
  4.  
  5. class User(Document):
  6. """Bare minimum to have the concept of a User.
  7. """
  8. _private_fields = [
  9. 'password', 'is_active',
  10. ]
  11.  
  12. username_regex = re.compile('[^a-zA-Z0-9._]')
  13. username_min_length = 2
  14.  
  15. username = StringField(max_length=30, required=True)
  16. email = EmailField(max_length=100)
  17. password = StringField(max_length=128)
  18. is_active = BooleanField(default=False)
  19. last_login = LongField(default=curtime)
  20. date_joined = LongField(default=curtime)
  21.  
  22. def __unicode__(self):
  23. return u'%s' % (self.username)
  24.  
  25. def set_password(self, raw_passwd):
  26. """Generates bcrypt hash and salt for storing a user's password. With
  27. bcrypt, the salt is kind of redundant, but this format stays friendly
  28. to other algorithms.
  29. """
  30. algorithm = auth.BCRYPT
  31. (salt, digest) = auth.gen_hexdigest(algorithm, raw_passwd)
  32. self.password = auth.build_passwd_line(algorithm, salt, digest)
  33.  
  34. def check_password(self, raw_password, passwd_line):
  35. """Compares raw_password to password stored for user. Updates
  36. self.last_login on success.
  37. """
  38. algorithm, salt, hash = auth.split_passwd_line(self.password)
  39. if hash == auth.gen_hexdigest(algorithm, salt, raw_password):
  40. self.last_login = curtime()
  41. return True
  42. else:
  43. return False
  44.  
  45. @classmethod
  46. def create_user(cls, username, password, email=str()):
  47. """Creates a user document with given username and password
  48. and saves it.
  49.  
  50. Validation occurs only for email argument. It makes no assumptions
  51. about password format.
  52. """
  53. now = curtime()
  54. username = username.lower()
  55.  
  56. # validate email argument
  57. try:
  58. cls.validate_class_partial(dict(email=email))
  59. except ValueError:
  60. pass
  61. else:
  62. email = email.strip()
  63. email = email.lower()
  64.  
  65. user = cls(username=username, email=email, date_joined=now)
  66. user.set_password(password)
  67. return user
  68.  
  69.  
  70. ###
  71. ### UserProfile
  72. ###
  73.  
  74. class UserProfile(Document):
  75. """The basic things a user profile tends to carry. Isolated in separate
  76. class to keep separate from private data.
  77. """
  78. owner = ObjectIdField(required=True)
  79. username = StringField(max_length=30, required=True)
  80. name = StringField(max_length=255)
  81. website = URLField(max_length=255)
  82. bio = StringField(max_length=100)
  83. location_text = StringField(max_length=100)
  84. avatar_url = URLField(max_length=255)
  85.  
  86. _private_fields = [
  87. 'owner',
  88. ]
  89.  
  90. def __init__(self, *args, **kwargs):
  91. super(UserProfile, self).__init__(*args, **kwargs)
  92.  
  93. def __unicode__(self):
  94. return u'%s' % (self.name)
Add Comment
Please, Sign In to add comment