1 | import json |
---|
2 | |
---|
3 | from repoze.folder import Folder |
---|
4 | from repoze.catalog.indexes.field import CatalogFieldIndex |
---|
5 | from repoze.catalog.query import Eq |
---|
6 | |
---|
7 | from pyramid.security import Allow, Everyone |
---|
8 | |
---|
9 | from ow.models.user import User |
---|
10 | from ow.catalog import ( |
---|
11 | get_catalog, |
---|
12 | install_catalog, |
---|
13 | update_indexes, |
---|
14 | reindex_object, |
---|
15 | remove_from_catalog, |
---|
16 | resources_from_query_results |
---|
17 | ) |
---|
18 | |
---|
19 | |
---|
20 | class OpenWorkouts(Folder): |
---|
21 | """ |
---|
22 | Root object, contains basically all the users, and in turn |
---|
23 | the users contain their workouts. |
---|
24 | |
---|
25 | Users are stored in a dict-like structure, using a string containing |
---|
26 | the user id (uid) as a key, and the User object as the value. |
---|
27 | """ |
---|
28 | |
---|
29 | __parent__ = __name__ = None |
---|
30 | |
---|
31 | __acl__ = [ |
---|
32 | (Allow, Everyone, 'view'), |
---|
33 | (Allow, 'admins', 'edit') |
---|
34 | ] |
---|
35 | |
---|
36 | def __init__(self, **kw): |
---|
37 | install_catalog(self) |
---|
38 | super(OpenWorkouts, self).__init__(**kw) |
---|
39 | |
---|
40 | def _get_catalog_indexes(self): |
---|
41 | indexes = { |
---|
42 | 'email': CatalogFieldIndex('email'), |
---|
43 | 'nickname': CatalogFieldIndex('nickname'), |
---|
44 | 'sport': CatalogFieldIndex('sport'), |
---|
45 | 'hashed': CatalogFieldIndex('hashed'), |
---|
46 | } |
---|
47 | return indexes |
---|
48 | |
---|
49 | def _update_indexes(self): |
---|
50 | return update_indexes(self.catalog, self._get_catalog_indexes()) |
---|
51 | |
---|
52 | def reindex(self, obj): |
---|
53 | """ |
---|
54 | Reindex the given object in the catalog |
---|
55 | """ |
---|
56 | reindex_object(self.catalog, obj) |
---|
57 | |
---|
58 | def query(self, query): |
---|
59 | """ |
---|
60 | Execute the given query on the catalog, returning the results |
---|
61 | (generator with events found or empty list if no results were found) |
---|
62 | """ |
---|
63 | catalog = get_catalog(self) |
---|
64 | number, results = catalog.query(query) |
---|
65 | if number: |
---|
66 | return resources_from_query_results(catalog, results, self) |
---|
67 | return [] |
---|
68 | |
---|
69 | def add_user(self, user): |
---|
70 | self.add(str(user.uid), user) |
---|
71 | self.reindex(user) |
---|
72 | |
---|
73 | def del_user(self, user): |
---|
74 | remove_from_catalog(self.catalog, user) |
---|
75 | self.remove(str(user.uid)) |
---|
76 | |
---|
77 | def get_user_by_uid(self, uid): |
---|
78 | return self.get(str(uid), None) |
---|
79 | |
---|
80 | def get_user_by_email(self, email): |
---|
81 | if email is not None: |
---|
82 | # for some reason, when searching for None |
---|
83 | # the catalog will return all users |
---|
84 | res = self.query(Eq('email', email)) |
---|
85 | if res: |
---|
86 | return next(res) |
---|
87 | return None |
---|
88 | |
---|
89 | def get_user_by_nickname(self, nickname): |
---|
90 | if nickname is not None: |
---|
91 | # for some reason, when searching for None |
---|
92 | # the catalog will return all users |
---|
93 | res = self.query(Eq('nickname', nickname)) |
---|
94 | if res: |
---|
95 | return next(res) |
---|
96 | return None |
---|
97 | |
---|
98 | @property |
---|
99 | def users(self): |
---|
100 | """ |
---|
101 | Return all user objects |
---|
102 | """ |
---|
103 | return [user for user in self.values() if isinstance(user, User)] |
---|
104 | |
---|
105 | @property |
---|
106 | def all_nicknames(self): |
---|
107 | """ |
---|
108 | Return all available nicknames |
---|
109 | """ |
---|
110 | return [user.nickname for user in self.users if user.nickname] |
---|
111 | |
---|
112 | @property |
---|
113 | def lowercase_nicknames(self): |
---|
114 | """ |
---|
115 | Return all available nicknames in lower case. Useful for |
---|
116 | nickname uniqueness validation on signup |
---|
117 | """ |
---|
118 | return [nick.lower() for nick in self.all_nicknames] |
---|
119 | |
---|
120 | @property |
---|
121 | def emails(self): |
---|
122 | """ |
---|
123 | Return all emails currently in use by users, sorted alphabetically |
---|
124 | """ |
---|
125 | return sorted([user.email for user in self.users]) |
---|
126 | |
---|
127 | @property |
---|
128 | def lowercase_emails(self): |
---|
129 | """ |
---|
130 | Returns all emails currently in use by users, transformed to |
---|
131 | lower case. This is useful to validate a new user email address |
---|
132 | against the currently used addresses |
---|
133 | """ |
---|
134 | return [email.lower() for email in self.emails] |
---|
135 | |
---|
136 | @property |
---|
137 | def sports(self): |
---|
138 | return [s for s in self.catalog['sport']._fwd_index] |
---|
139 | |
---|
140 | @property |
---|
141 | def sports_json(self): |
---|
142 | return json.dumps(self.sports) |
---|
143 | |
---|
144 | def get_workout_by_hash(self, hashed): |
---|
145 | if hashed is not None: |
---|
146 | # for some reason, when searching for None |
---|
147 | # the catalog will return all workouts |
---|
148 | res = self.query(Eq('hashed', hashed)) |
---|
149 | if res: |
---|
150 | return next(res) |
---|
151 | return None |
---|