source: OpenWorkouts-current/ow/models/user.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: 2.4 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.firstname = kw.get('firstname', '')
26        self.lastname = kw.get('lastname', '')
27        self.email = kw.get('email', '')
28        self.bio = kw.get('bio', '')
29        self.birth_date = kw.get('birth_date', None)
30        self.height = kw.get('height', None)
31        self.weight = kw.get('weight', None)
32        self.gender = kw.get('gender', 'female')
33        self.picture = kw.get('picture', None)  # blob
34        self.__password = None
35        self.last_workout_id = 0
36        super(User, self).__init__()
37
38    def __str__(self):
39        return u'User: %s (%s)' % (self.email, self.uid)
40
41    @property
42    def password(self):
43        return self.__password
44
45    @password.setter
46    def password(self, password=None):
47        """
48        Sets a password for the user, hashing with bcrypt.
49        """
50        password = password.encode('utf-8')
51        self.__password = bcrypt.hashpw(password, bcrypt.gensalt())
52
53    def check_password(self, password):
54        """
55        Check a plain text password against a hashed one
56        """
57        hashed = bcrypt.hashpw(password.encode('utf-8'), self.__password)
58        return hashed == self.__password
59
60    @property
61    def fullname(self):
62        """
63        Naive implementation of fullname: firstname + lastname
64        """
65        return u'%s %s' % (self.firstname, self.lastname)
66
67    def add_workout(self, workout):
68        # This returns the main catalog at the root folder
69        catalog = get_catalog(self)
70        self.last_workout_id += 1
71        workout_id = str(self.last_workout_id)
72        self[workout_id] = workout
73        reindex_object(catalog, workout)
74
75    def workouts(self):
76        """
77        Return this user workouts, sorted by date, from newer to older
78        """
79        workouts = sorted(self.values(), key=attrgetter('start'))
80        workouts.reverse()
81        return workouts
82
83    def workout_ids(self):
84        return self.keys()
85
86    @property
87    def num_workouts(self):
88        return len(self.workout_ids())
Note: See TracBrowser for help on using the repository browser.