1 | from pyramid.i18n import TranslationStringFactory |
---|
2 | from formencode import Schema, validators |
---|
3 | from pytz import common_timezones |
---|
4 | |
---|
5 | from ow.schemas.blob import FieldStorageBlob |
---|
6 | |
---|
7 | _ = TranslationStringFactory('OpenWorkouts') |
---|
8 | |
---|
9 | |
---|
10 | class 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 | |
---|
22 | class 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 | |
---|
34 | class 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 | |
---|
47 | class 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 | |
---|
60 | class 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 | |
---|
80 | class 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 | |
---|
91 | class 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 | |
---|
107 | class 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) |
---|