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

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

Imported sources from the old python2-only repository:

  • Modified the code so it is python 3.6 compatible
  • Fixed deprecation warnings, pyramid 1.10.x supported now
  • Fixed deprecation warnings about some libraries, like pyramid-simpleform
  • Added pytest-pycodestyle and pytest-flakes for automatic checks on the source code files when running tests.
  • Added default pytest.ini setup to enforce some default parameters when running tests.
  • Cleaned up the code a bit, catched up with tests coverage.
  • Property mode set to 100644
File size: 1.9 KB
Line 
1import json
2
3from repoze.folder import Folder
4from repoze.catalog.indexes.field import CatalogFieldIndex
5
6from pyramid.security import Allow, Everyone
7
8from ow.models.user import User
9from ow.catalog import get_catalog
10
11
12class OpenWorkouts(Folder):
13    """
14    Root object, contains basically all the users, and in turn
15    the users contain their workouts.
16    """
17
18    __parent__ = __name__ = None
19
20    __acl__ = [
21               (Allow, Everyone, 'view'),
22               (Allow, 'group:admins', 'edit')
23              ]
24
25    def _get_catalog_indexes(self):
26        indexes = {
27            'sport': CatalogFieldIndex('sport'),
28        }
29        return indexes
30
31    def all_usernames(self):
32        """
33        Return all available usernames
34        """
35        return self.keys()
36
37    def lowercase_usernames(self):
38        """
39        Return all available usernames in lower case. Useful for
40        username uniqueness validation on signup
41        """
42        return [name.lower() for name in self.keys()]
43
44    def emails(self):
45        """
46        Return all emails currently in use by users
47        """
48        return [u.email for u in self.users()]
49
50    def lowercase_emails(self):
51        """
52        Returns all emails currently in use by users, transformed to
53        lower case. This is useful to validate a new user email address
54        against the currently used addresses
55        """
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, user_id):
65        return self.get(user_id, None)
66
67    def add_user(self, user_id, **kw):
68        u = User(**kw)
69        self[user_id] = u
70
71    @property
72    def sports(self):
73        catalog = get_catalog(self)
74        return [s for s in catalog['sport']._fwd_index]
75
76    @property
77    def sports_json(self):
78        return json.dumps(self.sports)
Note: See TracBrowser for help on using the repository browser.