1 | from pyramid.i18n import TranslationStringFactory |
---|
2 | from formencode import Schema, validators |
---|
3 | |
---|
4 | from ow.schemas.blob import FieldStorageBlob |
---|
5 | |
---|
6 | _ = TranslationStringFactory('OpenWorkouts') |
---|
7 | |
---|
8 | |
---|
9 | class 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 | |
---|
21 | class 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 | |
---|
33 | class 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 | |
---|
46 | class 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 | |
---|
59 | class 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(whitelist=['jpg', 'jpeg', 'png', 'gif']) |
---|
74 | |
---|
75 | |
---|
76 | class ChangePasswordSchema(Schema): |
---|
77 | allow_extra_fields = True |
---|
78 | filter_extra_fields = True |
---|
79 | |
---|
80 | old_password = PasswordMatch(not_empty=True) |
---|
81 | password = validators.UnicodeString(min=9, not_empty=True) |
---|
82 | password_confirm = validators.UnicodeString(not_empty=True) |
---|
83 | chained_validators = [validators.FieldsMatch( |
---|
84 | 'password', 'password_confirm')] |
---|
85 | |
---|
86 | |
---|
87 | class SignUpSchema(Schema): |
---|
88 | """ |
---|
89 | Schema for the sign up of new users |
---|
90 | """ |
---|
91 | allow_extra_fields = True |
---|
92 | filter_extra_fields = True |
---|
93 | nickname = UniqueNickname() |
---|
94 | firstname = validators.UnicodeString(not_empty=True) |
---|
95 | lastname = validators.UnicodeString(not_empty=True) |
---|
96 | email = UniqueEmail(not_empty=True) |
---|
97 | password = validators.UnicodeString(min=9, not_empty=True) |
---|
98 | password_confirm = validators.UnicodeString(not_empty=True) |
---|
99 | chained_validators = [validators.FieldsMatch( |
---|
100 | 'password', 'password_confirm')] |
---|
101 | |
---|
102 | |
---|
103 | class RecoverPasswordSchema(Schema): |
---|
104 | """ |
---|
105 | Schema for the password recovery |
---|
106 | """ |
---|
107 | allow_extra_fields = True |
---|
108 | filter_extra_fields = True |
---|
109 | nickname = UniqueNickname() |
---|
110 | email = UniqueEmail(not_empty=True) |
---|