1 | import json |
---|
2 | |
---|
3 | from repoze.folder import Folder |
---|
4 | from repoze.catalog.indexes.field import CatalogFieldIndex |
---|
5 | |
---|
6 | from pyramid.security import Allow, Everyone |
---|
7 | |
---|
8 | from ow.models.user import User |
---|
9 | from ow.catalog import get_catalog |
---|
10 | |
---|
11 | |
---|
12 | class OpenWorkouts(Folder): |
---|
13 | """ |
---|
14 | Root object, contains basically all the users, and in turn |
---|
15 | the users contain their workouts. |
---|
16 | """ |
---|
17 | |
---|
18 | __parent__ = __name__ = None |
---|
19 | |
---|
20 | __acl__ = [ |
---|
21 | (Allow, Everyone, 'view'), |
---|
22 | (Allow, 'group:admins', 'edit') |
---|
23 | ] |
---|
24 | |
---|
25 | def _get_catalog_indexes(self): |
---|
26 | indexes = { |
---|
27 | 'sport': CatalogFieldIndex('sport'), |
---|
28 | } |
---|
29 | return indexes |
---|
30 | |
---|
31 | def all_usernames(self): |
---|
32 | """ |
---|
33 | Return all available usernames |
---|
34 | """ |
---|
35 | return self.keys() |
---|
36 | |
---|
37 | def lowercase_usernames(self): |
---|
38 | """ |
---|
39 | Return all available usernames in lower case. Useful for |
---|
40 | username uniqueness validation on signup |
---|
41 | """ |
---|
42 | return [name.lower() for name in self.keys()] |
---|
43 | |
---|
44 | def emails(self): |
---|
45 | """ |
---|
46 | Return all emails currently in use by users |
---|
47 | """ |
---|
48 | return [u.email for u in self.users()] |
---|
49 | |
---|
50 | def lowercase_emails(self): |
---|
51 | """ |
---|
52 | Returns all emails currently in use by users, transformed to |
---|
53 | lower case. This is useful to validate a new user email address |
---|
54 | against the currently used addresses |
---|
55 | """ |
---|
56 | return [u.email.lower() for u in self.users()] |
---|
57 | |
---|
58 | def users(self): |
---|
59 | """ |
---|
60 | Return all user objects |
---|
61 | """ |
---|
62 | return [u for u in self.values() if isinstance(u, User)] |
---|
63 | |
---|
64 | def get_user(self, user_id): |
---|
65 | return self.get(user_id, None) |
---|
66 | |
---|
67 | def add_user(self, user_id, **kw): |
---|
68 | u = User(**kw) |
---|
69 | self[user_id] = u |
---|
70 | |
---|
71 | @property |
---|
72 | def sports(self): |
---|
73 | catalog = get_catalog(self) |
---|
74 | return [s for s in catalog['sport']._fwd_index] |
---|
75 | |
---|
76 | @property |
---|
77 | def sports_json(self): |
---|
78 | return json.dumps(self.sports) |
---|