- Timestamp:
- Dec 16, 2018, 1:07:04 AM (5 years ago)
- Branches:
- current, feature/docs, master
- Children:
- 6560b8f
- Parents:
- 929097a
- Location:
- ow/models
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
ow/models/root.py
r929097a r1d92bf2 3 3 from repoze.folder import Folder 4 4 from repoze.catalog.indexes.field import CatalogFieldIndex 5 from repoze.catalog.query import Eq 5 6 6 7 from pyramid.security import Allow, Everyone 7 8 8 9 from ow.models.user import User 9 from ow.catalog import get_catalog 10 from ow.catalog import ( 11 get_catalog, 12 install_catalog, 13 reindex_object, 14 remove_from_catalog, 15 resources_from_query_results 16 ) 10 17 11 18 … … 14 21 Root object, contains basically all the users, and in turn 15 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. 16 26 """ 17 27 … … 20 30 __acl__ = [ 21 31 (Allow, Everyone, 'view'), 22 (Allow, ' group:admins', 'edit')32 (Allow, 'admins', 'edit') 23 33 ] 34 35 def __init__(self, **kw): 36 install_catalog(self) 37 super(OpenWorkouts, self).__init__(**kw) 24 38 25 39 def _get_catalog_indexes(self): 26 40 indexes = { 41 'email': CatalogFieldIndex('email'), 27 42 'sport': CatalogFieldIndex('sport'), 28 43 } 29 44 return indexes 30 45 31 def all_usernames(self):46 def reindex(self, obj): 32 47 """ 33 Re turn all available usernames48 Reindex the given object in the catalog 34 49 """ 35 re turn self.keys()50 reindex_object(self.catalog, obj) 36 51 37 def lowercase_usernames(self):52 def query(self, query): 38 53 """ 39 Return all available usernames in lower case. Useful for40 username uniqueness validation on signup54 Execute the given query on the catalog, returning the results 55 (generator with events found or empty list if no results were found) 41 56 """ 42 return [name.lower() for name in self.keys()] 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 [] 43 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 44 106 def emails(self): 45 107 """ 46 108 Return all emails currently in use by users 47 109 """ 48 return [u .email for u in self.users()]110 return [user.email for user in self.users] 49 111 112 @property 50 113 def lowercase_emails(self): 51 114 """ … … 54 117 against the currently used addresses 55 118 """ 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, uid): 65 return self.get(uid, None) 66 67 def add_user(self, uid, **kw): 68 u = User(**kw) 69 self[uid] = u 119 return [email.lower() for email in self.emails] 70 120 71 121 @property 72 122 def sports(self): 73 catalog = get_catalog(self) 74 return [s for s in catalog['sport']._fwd_index] 123 return [s for s in self.catalog['sport']._fwd_index] 75 124 76 125 @property -
ow/models/user.py
r929097a r1d92bf2 23 23 def __init__(self, **kw): 24 24 self.uid = kw.get('uid', uuid1()) 25 self.nickname = kw.get('nickname', '') 25 26 self.firstname = kw.get('firstname', '') 26 27 self.lastname = kw.get('lastname', '')
Note: See TracChangeset
for help on using the changeset viewer.