[5ec3a0b] | 1 | import json |
---|
| 2 | |
---|
| 3 | from repoze.folder import Folder |
---|
| 4 | from repoze.catalog.indexes.field import CatalogFieldIndex |
---|
[1d92bf2] | 5 | from repoze.catalog.query import Eq |
---|
[5ec3a0b] | 6 | |
---|
| 7 | from pyramid.security import Allow, Everyone |
---|
| 8 | |
---|
| 9 | from ow.models.user import User |
---|
[1d92bf2] | 10 | from ow.catalog import ( |
---|
| 11 | get_catalog, |
---|
| 12 | install_catalog, |
---|
[bddf042] | 13 | update_indexes, |
---|
[1d92bf2] | 14 | reindex_object, |
---|
| 15 | remove_from_catalog, |
---|
| 16 | resources_from_query_results |
---|
| 17 | ) |
---|
[5ec3a0b] | 18 | |
---|
| 19 | |
---|
| 20 | class OpenWorkouts(Folder): |
---|
| 21 | """ |
---|
| 22 | Root object, contains basically all the users, and in turn |
---|
| 23 | the users contain their workouts. |
---|
[1d92bf2] | 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. |
---|
[5ec3a0b] | 27 | """ |
---|
| 28 | |
---|
| 29 | __parent__ = __name__ = None |
---|
| 30 | |
---|
| 31 | __acl__ = [ |
---|
| 32 | (Allow, Everyone, 'view'), |
---|
[1d92bf2] | 33 | (Allow, 'admins', 'edit') |
---|
[5ec3a0b] | 34 | ] |
---|
| 35 | |
---|
[1d92bf2] | 36 | def __init__(self, **kw): |
---|
| 37 | install_catalog(self) |
---|
| 38 | super(OpenWorkouts, self).__init__(**kw) |
---|
| 39 | |
---|
[5ec3a0b] | 40 | def _get_catalog_indexes(self): |
---|
| 41 | indexes = { |
---|
[1d92bf2] | 42 | 'email': CatalogFieldIndex('email'), |
---|
[bddf042] | 43 | 'nickname': CatalogFieldIndex('nickname'), |
---|
[5ec3a0b] | 44 | 'sport': CatalogFieldIndex('sport'), |
---|
[42baca4] | 45 | 'hashed': CatalogFieldIndex('hashed'), |
---|
[5ec3a0b] | 46 | } |
---|
| 47 | return indexes |
---|
| 48 | |
---|
[bddf042] | 49 | def _update_indexes(self): |
---|
| 50 | return update_indexes(self.catalog, self._get_catalog_indexes()) |
---|
| 51 | |
---|
[1d92bf2] | 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 | |
---|
[bddf042] | 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 | |
---|
[1d92bf2] | 98 | @property |
---|
| 99 | def users(self): |
---|
[5ec3a0b] | 100 | """ |
---|
[1d92bf2] | 101 | Return all user objects |
---|
[5ec3a0b] | 102 | """ |
---|
[1d92bf2] | 103 | return [user for user in self.values() if isinstance(user, User)] |
---|
[5ec3a0b] | 104 | |
---|
[1d92bf2] | 105 | @property |
---|
| 106 | def all_nicknames(self): |
---|
[5ec3a0b] | 107 | """ |
---|
[1d92bf2] | 108 | Return all available nicknames |
---|
[5ec3a0b] | 109 | """ |
---|
[1d92bf2] | 110 | return [user.nickname for user in self.users if user.nickname] |
---|
[5ec3a0b] | 111 | |
---|
[1d92bf2] | 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 |
---|
[5ec3a0b] | 121 | def emails(self): |
---|
| 122 | """ |
---|
| 123 | Return all emails currently in use by users |
---|
| 124 | """ |
---|
[1d92bf2] | 125 | return [user.email for user in self.users] |
---|
[5ec3a0b] | 126 | |
---|
[1d92bf2] | 127 | @property |
---|
[5ec3a0b] | 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 | """ |
---|
[1d92bf2] | 134 | return [email.lower() for email in self.emails] |
---|
[5ec3a0b] | 135 | |
---|
| 136 | @property |
---|
| 137 | def sports(self): |
---|
[1d92bf2] | 138 | return [s for s in self.catalog['sport']._fwd_index] |
---|
[5ec3a0b] | 139 | |
---|
| 140 | @property |
---|
| 141 | def sports_json(self): |
---|
| 142 | return json.dumps(self.sports) |
---|
[42baca4] | 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 |
---|