source: OpenWorkouts-current/ow/models/user.py @ 31adfa5

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

(#14) Timezones support:

  • Added pytz as a new dependency, please install it in your existing envs:

pip install pytz

  • Added a timezone attribute to users, to store in which timezone they are, defaults to 'UTC'. Ensure any users you could have in your database have such attribute. You can add it in pshell:

for user in root.users:

user.timezone = 'UTC'

request.tm.commit()

  • Modified schemas/templates/views to let users choose their timezone based on a list of "common" timezones provided by pytz
  • Added two methods to the Workout model so we can get the start and end dates formatted in the appropiate timezone (all datetime objects are stored in UTC)
  • Modified the templates where we show workout dates and times so the new timezone-formatting methods are used.
  • 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.timezone = kw.get('timezone', 'UTC')
36        self.__password = None
37        self.last_workout_id = 0
38        super(User, self).__init__()
39
40    def __str__(self):
41        return u'User: %s (%s)' % (self.email, self.uid)
42
43    @property
44    def password(self):
45        return self.__password
46
47    @password.setter
48    def password(self, password=None):
49        """
50        Sets a password for the user, hashing with bcrypt.
51        """
52        password = password.encode('utf-8')
53        self.__password = bcrypt.hashpw(password, bcrypt.gensalt())
54
55    def check_password(self, password):
56        """
57        Check a plain text password against a hashed one
58        """
59        hashed = bcrypt.hashpw(password.encode('utf-8'), self.__password)
60        return hashed == self.__password
61
62    @property
63    def fullname(self):
64        """
65        Naive implementation of fullname: firstname + lastname
66        """
67        return u'%s %s' % (self.firstname, self.lastname)
68
69    def add_workout(self, workout):
70        # This returns the main catalog at the root folder
71        catalog = get_catalog(self)
72        self.last_workout_id += 1
73        workout_id = str(self.last_workout_id)
74        self[workout_id] = workout
75        reindex_object(catalog, workout)
76
77    def workouts(self):
78        """
79        Return this user workouts, sorted by date, from newer to older
80        """
81        workouts = sorted(self.values(), key=attrgetter('start'))
82        workouts.reverse()
83        return workouts
84
85    def workout_ids(self):
86        return self.keys()
87
88    @property
89    def num_workouts(self):
90        return len(self.workout_ids())
Note: See TracBrowser for help on using the repository browser.