source: OpenWorkouts-current/ow/models/user.py @ 1d92bf2

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

(#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.
  • Property mode set to 100644
File size: 2.5 KB
Line 
1
2from uuid import uuid1
3from operator import attrgetter
4
5import bcrypt
6from repoze.folder import Folder
7from pyramid.security import Allow
8
9from ow.catalog import get_catalog, reindex_object
10
11
12class User(Folder):
13
14    __parent__ = __name__ = None
15
16    def __acl__(self):
17        permissions = [
18            (Allow, str(self.uid), 'edit'),
19            (Allow, str(self.uid), 'view'),
20        ]
21        return permissions
22
23    def __init__(self, **kw):
24        self.uid = kw.get('uid', uuid1())
25        self.nickname = kw.get('nickname', '')
26        self.firstname = kw.get('firstname', '')
27        self.lastname = kw.get('lastname', '')
28        self.email = kw.get('email', '')
29        self.bio = kw.get('bio', '')
30        self.birth_date = kw.get('birth_date', None)
31        self.height = kw.get('height', None)
32        self.weight = kw.get('weight', None)
33        self.gender = kw.get('gender', 'female')
34        self.picture = kw.get('picture', None)  # blob
35        self.__password = None
36        self.last_workout_id = 0
37        super(User, self).__init__()
38
39    def __str__(self):
40        return u'User: %s (%s)' % (self.email, self.uid)
41
42    @property
43    def password(self):
44        return self.__password
45
46    @password.setter
47    def password(self, password=None):
48        """
49        Sets a password for the user, hashing with bcrypt.
50        """
51        password = password.encode('utf-8')
52        self.__password = bcrypt.hashpw(password, bcrypt.gensalt())
53
54    def check_password(self, password):
55        """
56        Check a plain text password against a hashed one
57        """
58        hashed = bcrypt.hashpw(password.encode('utf-8'), self.__password)
59        return hashed == self.__password
60
61    @property
62    def fullname(self):
63        """
64        Naive implementation of fullname: firstname + lastname
65        """
66        return u'%s %s' % (self.firstname, self.lastname)
67
68    def add_workout(self, workout):
69        # This returns the main catalog at the root folder
70        catalog = get_catalog(self)
71        self.last_workout_id += 1
72        workout_id = str(self.last_workout_id)
73        self[workout_id] = workout
74        reindex_object(catalog, workout)
75
76    def workouts(self):
77        """
78        Return this user workouts, sorted by date, from newer to older
79        """
80        workouts = sorted(self.values(), key=attrgetter('start'))
81        workouts.reverse()
82        return workouts
83
84    def workout_ids(self):
85        return self.keys()
86
87    @property
88    def num_workouts(self):
89        return len(self.workout_ids())
Note: See TracBrowser for help on using the repository browser.