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