source: OpenWorkouts-current/ow/models/root.py @ bddf042

currentfeature/docs
Last change on this file since bddf042 was bddf042, checked in by Borja Lopez <borja@…>, 5 years ago

(#7) Allow users profiles to be accessed using a more friendly url:

https://openworkouts.org/profile/NICKNAME

IMPORTANT: This change adds a new index to the catalog, so ensure you
update any existing databases after pulling.

Enter pshell and run this code:

root._update_indexes()
for user in root.users:

root.reindex(user)

  • Property mode set to 100644
File size: 3.9 KB
Line 
1import json
2
3from repoze.folder import Folder
4from repoze.catalog.indexes.field import CatalogFieldIndex
5from repoze.catalog.query import Eq
6
7from pyramid.security import Allow, Everyone
8
9from ow.models.user import User
10from 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
20class 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        }
46        return indexes
47
48    def _update_indexes(self):
49        return update_indexes(self.catalog, self._get_catalog_indexes())
50
51    def reindex(self, obj):
52        """
53        Reindex the given object in the catalog
54        """
55        reindex_object(self.catalog, obj)
56
57    def query(self, query):
58        """
59        Execute the given query on the catalog, returning the results
60        (generator with events found or empty list if no results were found)
61        """
62        catalog = get_catalog(self)
63        number, results = catalog.query(query)
64        if number:
65            return resources_from_query_results(catalog, results, self)
66        return []
67
68    def add_user(self, user):
69        self.add(str(user.uid), user)
70        self.reindex(user)
71
72    def del_user(self, user):
73        remove_from_catalog(self.catalog, user)
74        self.remove(str(user.uid))
75
76    def get_user_by_uid(self, uid):
77        return self.get(str(uid), None)
78
79    def get_user_by_email(self, email):
80        if email is not None:
81            # for some reason, when searching for None
82            # the catalog will return all users
83            res = self.query(Eq('email', email))
84            if res:
85                return next(res)
86        return None
87
88    def get_user_by_nickname(self, nickname):
89        if nickname is not None:
90            # for some reason, when searching for None
91            # the catalog will return all users
92            res = self.query(Eq('nickname', nickname))
93            if res:
94                return next(res)
95        return None
96
97    @property
98    def users(self):
99        """
100        Return all user objects
101        """
102        return [user for user in self.values() if isinstance(user, User)]
103
104    @property
105    def all_nicknames(self):
106        """
107        Return all available nicknames
108        """
109        return [user.nickname for user in self.users if user.nickname]
110
111    @property
112    def lowercase_nicknames(self):
113        """
114        Return all available nicknames in lower case. Useful for
115        nickname uniqueness validation on signup
116        """
117        return [nick.lower() for nick in self.all_nicknames]
118
119    @property
120    def emails(self):
121        """
122        Return all emails currently in use by users
123        """
124        return [user.email for user in self.users]
125
126    @property
127    def lowercase_emails(self):
128        """
129        Returns all emails currently in use by users, transformed to
130        lower case. This is useful to validate a new user email address
131        against the currently used addresses
132        """
133        return [email.lower() for email in self.emails]
134
135    @property
136    def sports(self):
137        return [s for s in self.catalog['sport']._fwd_index]
138
139    @property
140    def sports_json(self):
141        return json.dumps(self.sports)
Note: See TracBrowser for help on using the repository browser.