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

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

Fixed a bug in the edit_profile view that was breaking the edit profile

operation when a profile image was not provided.

This comes from the latest Formencode in python 3.x, this patch is a quick
and dirty hack while upstream does not fix it.

  • Property mode set to 100644
File size: 3.6 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    bio = validators.UnicodeString(if_missing='')
69    birth_date = validators.DateConverter(month_style='dd/mm/yyyy')
70    height = validators.Number()
71    weight = validators.Number()
72    gender = validators.OneOf(('male', 'female'), not_empty=True)
73    picture = FieldStorageBlob(if_emtpy=None, if_missing=None,
74                               whitelist=['jpg', 'jpeg', 'png', 'gif'])
75
76
77class ChangePasswordSchema(Schema):
78    allow_extra_fields = True
79    filter_extra_fields = True
80
81    old_password = PasswordMatch(not_empty=True)
82    password = validators.UnicodeString(min=9, not_empty=True)
83    password_confirm = validators.UnicodeString(not_empty=True)
84    chained_validators = [validators.FieldsMatch(
85        'password', 'password_confirm')]
86
87
88class SignUpSchema(Schema):
89    """
90    Schema for the sign up of new users
91    """
92    allow_extra_fields = True
93    filter_extra_fields = True
94    nickname = UniqueNickname()
95    firstname = validators.UnicodeString(not_empty=True)
96    lastname = validators.UnicodeString(not_empty=True)
97    email = UniqueEmail(not_empty=True)
98    password = validators.UnicodeString(min=9, not_empty=True)
99    password_confirm = validators.UnicodeString(not_empty=True)
100    chained_validators = [validators.FieldsMatch(
101        'password', 'password_confirm')]
102
103
104class RecoverPasswordSchema(Schema):
105    """
106    Schema for the password recovery
107    """
108    allow_extra_fields = True
109    filter_extra_fields = True
110    nickname = UniqueNickname()
111    email = UniqueEmail(not_empty=True)
Note: See TracBrowser for help on using the repository browser.