source: OpenWorkouts-current/ow/schemas/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: 3.8 KB
Line 
1from pyramid.i18n import TranslationStringFactory
2from formencode import Schema, validators
3from pytz import common_timezones
4
5from ow.schemas.blob import FieldStorageBlob
6
7_ = TranslationStringFactory('OpenWorkouts')
8
9
10class PasswordMatch(validators.UnicodeString):
11    messages = {
12        "dont_match": _('The given password does not match the existing one '),
13    }
14
15    def _validate_python(self, value, state):
16        super(PasswordMatch, self)._validate_python(value, state)
17        if not state.user.check_password(value):
18            raise validators.Invalid(
19                self.message('dont_match', state), value, state)
20
21
22class UniqueNickname(validators.UnicodeString):
23    messages = {
24        "name_exists": _('Another user is already using the nickname %(name)s')
25    }
26
27    def _validate_python(self, value, state):
28        super(UniqueNickname, self)._validate_python(value, state)
29        if value.lower() in state.names:
30            raise validators.Invalid(
31                self.message('name_exists', state, name=value), value, state)
32
33
34class UniqueEmail(validators.Email):
35    messages = {
36        "email_exists": _('Another user is already registered with the email '
37                          '%(email)s')
38    }
39
40    def _validate_python(self, value, state):
41        super(UniqueEmail, self)._validate_python(value, state)
42        if value.lower() in state.emails:
43            raise validators.Invalid(
44                self.message('email_exists', state, email=value), value, state)
45
46
47class UserAddSchema(Schema):
48    """
49    Schema to add a new user
50    """
51    allow_extra_fields = True
52    filter_extra_fields = True
53    email = UniqueEmail(not_empty=True)
54    nickname = UniqueNickname(if_missing='')
55    firstname = validators.UnicodeString()
56    lastname = validators.UnicodeString()
57    group = validators.UnicodeString(if_missing='')
58
59
60class UserProfileSchema(Schema):
61    """
62    Schema for the "edit profile" form for users
63    """
64    allow_extra_fields = True
65    filter_extra_fields = True
66    firstname = validators.UnicodeString(not_empty=True)
67    lastname = validators.UnicodeString(not_empty=True)
68    email = validators.Email(not_empty=True)
69    nickname = UniqueNickname(if_missing='')
70    bio = validators.UnicodeString(if_missing='')
71    birth_date = validators.DateConverter(month_style='dd/mm/yyyy')
72    height = validators.Number()
73    weight = validators.Number()
74    gender = validators.OneOf(('male', 'female'), not_empty=True)
75    picture = FieldStorageBlob(if_emtpy=None, if_missing=None,
76                               whitelist=['jpg', 'jpeg', 'png', 'gif'])
77    timezone = validators.OneOf(common_timezones, if_missing='UTC')
78
79
80class ChangePasswordSchema(Schema):
81    allow_extra_fields = True
82    filter_extra_fields = True
83
84    old_password = PasswordMatch(not_empty=True)
85    password = validators.UnicodeString(min=9, not_empty=True)
86    password_confirm = validators.UnicodeString(not_empty=True)
87    chained_validators = [validators.FieldsMatch(
88        'password', 'password_confirm')]
89
90
91class SignUpSchema(Schema):
92    """
93    Schema for the sign up of new users
94    """
95    allow_extra_fields = True
96    filter_extra_fields = True
97    nickname = UniqueNickname(if_missing='')
98    firstname = validators.UnicodeString(not_empty=True)
99    lastname = validators.UnicodeString(not_empty=True)
100    email = UniqueEmail(not_empty=True)
101    password = validators.UnicodeString(min=9, not_empty=True)
102    password_confirm = validators.UnicodeString(not_empty=True)
103    chained_validators = [validators.FieldsMatch(
104        'password', 'password_confirm')]
105
106
107class RecoverPasswordSchema(Schema):
108    """
109    Schema for the password recovery
110    """
111    allow_extra_fields = True
112    filter_extra_fields = True
113    nickname = UniqueNickname(if_missing='')
114    email = UniqueEmail(not_empty=True)
Note: See TracBrowser for help on using the repository browser.