Changeset 1d92bf2 in OpenWorkouts-current for ow/models


Ignore:
Timestamp:
Dec 16, 2018, 1:07:04 AM (5 years ago)
Author:
borja <borja@…>
Branches:
current, feature/docs, master
Children:
6560b8f
Parents:
929097a
Message:

(#37) Allow login using email address instead of username:

  • Use user uids as keys in the root folder for referencing user objects (instead of username)
  • Use uids for referencing users all over the place (auth, permissions, traversal urls, etc)
  • Replaced the username concept with nickname. This nickname will be used as a shortcut to access "public profile" pages for users
  • Reworked lots of basic methods in the OpenWorkouts root object (s/username/nickname, marked as properties some methods like users, emails, etc)
  • Added new add_user() and delete_user() helpers to the OpenWorkouts root object
  • Fixed bug in the dashboard redirect view, causing an endless loop if an authenticated user does not exist anymore when loading a page.
  • Lots of tests fixes, adaptations and catch up.
Location:
ow/models
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ow/models/root.py

    r929097a r1d92bf2  
    33from repoze.folder import Folder
    44from repoze.catalog.indexes.field import CatalogFieldIndex
     5from repoze.catalog.query import Eq
    56
    67from pyramid.security import Allow, Everyone
    78
    89from ow.models.user import User
    9 from ow.catalog import get_catalog
     10from ow.catalog import (
     11    get_catalog,
     12    install_catalog,
     13    reindex_object,
     14    remove_from_catalog,
     15    resources_from_query_results
     16)
    1017
    1118
     
    1421    Root object, contains basically all the users, and in turn
    1522    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.
    1626    """
    1727
     
    2030    __acl__ = [
    2131               (Allow, Everyone, 'view'),
    22                (Allow, 'group:admins', 'edit')
     32               (Allow, 'admins', 'edit')
    2333              ]
     34
     35    def __init__(self, **kw):
     36        install_catalog(self)
     37        super(OpenWorkouts, self).__init__(**kw)
    2438
    2539    def _get_catalog_indexes(self):
    2640        indexes = {
     41            'email': CatalogFieldIndex('email'),
    2742            'sport': CatalogFieldIndex('sport'),
    2843        }
    2944        return indexes
    3045
    31     def all_usernames(self):
     46    def reindex(self, obj):
    3247        """
    33         Return all available usernames
     48        Reindex the given object in the catalog
    3449        """
    35         return self.keys()
     50        reindex_object(self.catalog, obj)
    3651
    37     def lowercase_usernames(self):
     52    def query(self, query):
    3853        """
    39         Return all available usernames in lower case. Useful for
    40         username uniqueness validation on signup
     54        Execute the given query on the catalog, returning the results
     55        (generator with events found or empty list if no results were found)
    4156        """
    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 []
    4362
     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
    44106    def emails(self):
    45107        """
    46108        Return all emails currently in use by users
    47109        """
    48         return [u.email for u in self.users()]
     110        return [user.email for user in self.users]
    49111
     112    @property
    50113    def lowercase_emails(self):
    51114        """
     
    54117        against the currently used addresses
    55118        """
    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]
    70120
    71121    @property
    72122    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]
    75124
    76125    @property
  • ow/models/user.py

    r929097a r1d92bf2  
    2323    def __init__(self, **kw):
    2424        self.uid = kw.get('uid', uuid1())
     25        self.nickname = kw.get('nickname', '')
    2526        self.firstname = kw.get('firstname', '')
    2627        self.lastname = kw.get('lastname', '')
Note: See TracChangeset for help on using the changeset viewer.