source: OpenWorkouts-current/ow/schemas/user.py @ fd6da93

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

(#56) Add support for different locale/language:

  • Let users choose their lang/locale in the edit profile page
  • Set the currently selected locale as a cookie (following pyramid docs on how to set the locale using the default locale negotiator)
  • Save the locale setting for each user as an attribute on the User model
  • Set the proper locale as a cookie on login
  • Unset the locale cookie on logout

Default available locales for now are en (english) and es (spanish)

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