[5ec3a0b] | 1 | |
---|
| 2 | from uuid import uuid1 |
---|
| 3 | from operator import attrgetter |
---|
| 4 | |
---|
| 5 | import bcrypt |
---|
| 6 | from repoze.folder import Folder |
---|
| 7 | from pyramid.security import Allow |
---|
| 8 | |
---|
| 9 | from ow.catalog import get_catalog, reindex_object |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | class User(Folder): |
---|
| 13 | |
---|
| 14 | __parent__ = __name__ = None |
---|
| 15 | |
---|
| 16 | def __acl__(self): |
---|
| 17 | permissions = [ |
---|
| 18 | (Allow, str(self.uid), 'edit'), |
---|
| 19 | (Allow, str(self.uid), 'view'), |
---|
| 20 | ] |
---|
| 21 | return permissions |
---|
| 22 | |
---|
| 23 | def __init__(self, **kw): |
---|
| 24 | self.uid = kw.get('uid', uuid1()) |
---|
| 25 | self.firstname = kw.get('firstname', '') |
---|
| 26 | self.lastname = kw.get('lastname', '') |
---|
| 27 | self.email = kw.get('email', '') |
---|
| 28 | self.bio = kw.get('bio', '') |
---|
| 29 | self.birth_date = kw.get('birth_date', None) |
---|
| 30 | self.height = kw.get('height', None) |
---|
| 31 | self.weight = kw.get('weight', None) |
---|
| 32 | self.gender = kw.get('gender', 'female') |
---|
| 33 | self.picture = kw.get('picture', None) # blob |
---|
| 34 | self.__password = None |
---|
| 35 | self.last_workout_id = 0 |
---|
| 36 | super(User, self).__init__() |
---|
| 37 | |
---|
| 38 | def __str__(self): |
---|
| 39 | return u'User: %s (%s)' % (self.email, self.uid) |
---|
| 40 | |
---|
| 41 | @property |
---|
| 42 | def password(self): |
---|
| 43 | return self.__password |
---|
| 44 | |
---|
| 45 | @password.setter |
---|
| 46 | def password(self, password=None): |
---|
| 47 | """ |
---|
| 48 | Sets a password for the user, hashing with bcrypt. |
---|
| 49 | """ |
---|
| 50 | password = password.encode('utf-8') |
---|
| 51 | self.__password = bcrypt.hashpw(password, bcrypt.gensalt()) |
---|
| 52 | |
---|
| 53 | def check_password(self, password): |
---|
| 54 | """ |
---|
| 55 | Check a plain text password against a hashed one |
---|
| 56 | """ |
---|
| 57 | hashed = bcrypt.hashpw(password.encode('utf-8'), self.__password) |
---|
| 58 | return hashed == self.__password |
---|
| 59 | |
---|
| 60 | @property |
---|
| 61 | def fullname(self): |
---|
| 62 | """ |
---|
| 63 | Naive implementation of fullname: firstname + lastname |
---|
| 64 | """ |
---|
| 65 | return u'%s %s' % (self.firstname, self.lastname) |
---|
| 66 | |
---|
| 67 | def add_workout(self, workout): |
---|
| 68 | # This returns the main catalog at the root folder |
---|
| 69 | catalog = get_catalog(self) |
---|
| 70 | self.last_workout_id += 1 |
---|
| 71 | workout_id = str(self.last_workout_id) |
---|
| 72 | self[workout_id] = workout |
---|
| 73 | reindex_object(catalog, workout) |
---|
| 74 | |
---|
| 75 | def workouts(self): |
---|
| 76 | """ |
---|
| 77 | Return this user workouts, sorted by date, from newer to older |
---|
| 78 | """ |
---|
| 79 | workouts = sorted(self.values(), key=attrgetter('start')) |
---|
| 80 | workouts.reverse() |
---|
| 81 | return workouts |
---|
| 82 | |
---|
| 83 | def workout_ids(self): |
---|
| 84 | return self.keys() |
---|
| 85 | |
---|
| 86 | @property |
---|
| 87 | def num_workouts(self): |
---|
| 88 | return len(self.workout_ids()) |
---|