[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, |
---|
| 13 | reindex_object, |
---|
| 14 | remove_from_catalog, |
---|
| 15 | resources_from_query_results |
---|
| 16 | ) |
---|
[5ec3a0b] | 17 | |
---|
| 18 | |
---|
| 19 | class OpenWorkouts(Folder): |
---|
| 20 | """ |
---|
| 21 | Root object, contains basically all the users, and in turn |
---|
| 22 | the users contain their workouts. |
---|
[1d92bf2] | 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. |
---|
[5ec3a0b] | 26 | """ |
---|
| 27 | |
---|
| 28 | __parent__ = __name__ = None |
---|
| 29 | |
---|
| 30 | __acl__ = [ |
---|
| 31 | (Allow, Everyone, 'view'), |
---|
[1d92bf2] | 32 | (Allow, 'admins', 'edit') |
---|
[5ec3a0b] | 33 | ] |
---|
| 34 | |
---|
[1d92bf2] | 35 | def __init__(self, **kw): |
---|
| 36 | install_catalog(self) |
---|
| 37 | super(OpenWorkouts, self).__init__(**kw) |
---|
| 38 | |
---|
[5ec3a0b] | 39 | def _get_catalog_indexes(self): |
---|
| 40 | indexes = { |
---|
[1d92bf2] | 41 | 'email': CatalogFieldIndex('email'), |
---|
[5ec3a0b] | 42 | 'sport': CatalogFieldIndex('sport'), |
---|
| 43 | } |
---|
| 44 | return indexes |
---|
| 45 | |
---|
[1d92bf2] | 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): |
---|
[5ec3a0b] | 85 | """ |
---|
[1d92bf2] | 86 | Return all user objects |
---|
[5ec3a0b] | 87 | """ |
---|
[1d92bf2] | 88 | return [user for user in self.values() if isinstance(user, User)] |
---|
[5ec3a0b] | 89 | |
---|
[1d92bf2] | 90 | @property |
---|
| 91 | def all_nicknames(self): |
---|
[5ec3a0b] | 92 | """ |
---|
[1d92bf2] | 93 | Return all available nicknames |
---|
[5ec3a0b] | 94 | """ |
---|
[1d92bf2] | 95 | return [user.nickname for user in self.users if user.nickname] |
---|
[5ec3a0b] | 96 | |
---|
[1d92bf2] | 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 |
---|
[5ec3a0b] | 106 | def emails(self): |
---|
| 107 | """ |
---|
| 108 | Return all emails currently in use by users |
---|
| 109 | """ |
---|
[1d92bf2] | 110 | return [user.email for user in self.users] |
---|
[5ec3a0b] | 111 | |
---|
[1d92bf2] | 112 | @property |
---|
[5ec3a0b] | 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 | """ |
---|
[1d92bf2] | 119 | return [email.lower() for email in self.emails] |
---|
[5ec3a0b] | 120 | |
---|
| 121 | @property |
---|
| 122 | def sports(self): |
---|
[1d92bf2] | 123 | return [s for s in self.catalog['sport']._fwd_index] |
---|
[5ec3a0b] | 124 | |
---|
| 125 | @property |
---|
| 126 | def sports_json(self): |
---|
| 127 | return json.dumps(self.sports) |
---|