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

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

(#32) Improvements on the edit profile form:

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