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 UniqueUsername(validators.UnicodeString): |
---|
22 | messages = { |
---|
23 | "name_exists": _('Another user is already registered with the ' |
---|
24 | 'username %(name)s') |
---|
25 | } |
---|
26 | |
---|
27 | def _validate_python(self, value, state): |
---|
28 | super(UniqueUsername, 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 | user_id = validators.UnicodeString() |
---|
54 | firstname = validators.UnicodeString() |
---|
55 | lastname = validators.UnicodeString() |
---|
56 | email = validators.Email(not_empty=True) |
---|
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 | 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(whitelist=['jpg', 'jpeg', 'png', 'gif']) |
---|
75 | |
---|
76 | |
---|
77 | class 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 | |
---|
88 | class SignUpSchema(Schema): |
---|
89 | """ |
---|
90 | Schema for the sign up of new users |
---|
91 | """ |
---|
92 | allow_extra_fields = True |
---|
93 | filter_extra_fields = True |
---|
94 | username = UniqueUsername(not_empty=True) |
---|
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 | |
---|
104 | class RecoverPasswordSchema(Schema): |
---|
105 | """ |
---|
106 | Schema for the password recovery |
---|
107 | """ |
---|
108 | allow_extra_fields = True |
---|
109 | filter_extra_fields = True |
---|
110 | username = UniqueUsername(not_empty=True) |
---|
111 | email = UniqueEmail(not_empty=True) |
---|