[5ec3a0b] | 1 | from pyramid.i18n import TranslationStringFactory |
---|
| 2 | from formencode import Schema, validators |
---|
[31adfa5] | 3 | from pytz import common_timezones |
---|
[5ec3a0b] | 4 | |
---|
| 5 | from ow.schemas.blob import FieldStorageBlob |
---|
[fd6da93] | 6 | from ow.utilities import get_available_locale_names |
---|
[5ec3a0b] | 7 | |
---|
| 8 | _ = TranslationStringFactory('OpenWorkouts') |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | class PasswordMatch(validators.UnicodeString): |
---|
| 12 | messages = { |
---|
| 13 | "dont_match": _('The given password does not match the existing one '), |
---|
| 14 | } |
---|
| 15 | |
---|
| 16 | def _validate_python(self, value, state): |
---|
| 17 | super(PasswordMatch, self)._validate_python(value, state) |
---|
| 18 | if not state.user.check_password(value): |
---|
| 19 | raise validators.Invalid( |
---|
| 20 | self.message('dont_match', state), value, state) |
---|
| 21 | |
---|
| 22 | |
---|
[1d92bf2] | 23 | class UniqueNickname(validators.UnicodeString): |
---|
[5ec3a0b] | 24 | messages = { |
---|
[1d92bf2] | 25 | "name_exists": _('Another user is already using the nickname %(name)s') |
---|
[5ec3a0b] | 26 | } |
---|
| 27 | |
---|
| 28 | def _validate_python(self, value, state): |
---|
[1d92bf2] | 29 | super(UniqueNickname, self)._validate_python(value, state) |
---|
[5ec3a0b] | 30 | if value.lower() in state.names: |
---|
| 31 | raise validators.Invalid( |
---|
| 32 | self.message('name_exists', state, name=value), value, state) |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | class UniqueEmail(validators.Email): |
---|
| 36 | messages = { |
---|
| 37 | "email_exists": _('Another user is already registered with the email ' |
---|
| 38 | '%(email)s') |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | def _validate_python(self, value, state): |
---|
| 42 | super(UniqueEmail, self)._validate_python(value, state) |
---|
| 43 | if value.lower() in state.emails: |
---|
| 44 | raise validators.Invalid( |
---|
| 45 | self.message('email_exists', state, email=value), value, state) |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | class UserAddSchema(Schema): |
---|
| 49 | """ |
---|
| 50 | Schema to add a new user |
---|
| 51 | """ |
---|
| 52 | allow_extra_fields = True |
---|
| 53 | filter_extra_fields = True |
---|
[1d92bf2] | 54 | email = UniqueEmail(not_empty=True) |
---|
| 55 | nickname = UniqueNickname(if_missing='') |
---|
[5ec3a0b] | 56 | firstname = validators.UnicodeString() |
---|
| 57 | lastname = validators.UnicodeString() |
---|
| 58 | group = validators.UnicodeString(if_missing='') |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | class UserProfileSchema(Schema): |
---|
| 62 | """ |
---|
| 63 | Schema for the "edit profile" form for users |
---|
| 64 | """ |
---|
| 65 | allow_extra_fields = True |
---|
| 66 | filter_extra_fields = True |
---|
| 67 | firstname = validators.UnicodeString(not_empty=True) |
---|
| 68 | lastname = validators.UnicodeString(not_empty=True) |
---|
| 69 | email = validators.Email(not_empty=True) |
---|
[722ae18] | 70 | nickname = UniqueNickname(if_missing='') |
---|
[5ec3a0b] | 71 | bio = validators.UnicodeString(if_missing='') |
---|
| 72 | birth_date = validators.DateConverter(month_style='dd/mm/yyyy') |
---|
| 73 | height = validators.Number() |
---|
| 74 | weight = validators.Number() |
---|
| 75 | gender = validators.OneOf(('male', 'female'), not_empty=True) |
---|
[6560b8f] | 76 | picture = FieldStorageBlob(if_emtpy=None, if_missing=None, |
---|
| 77 | whitelist=['jpg', 'jpeg', 'png', 'gif']) |
---|
[31adfa5] | 78 | timezone = validators.OneOf(common_timezones, if_missing='UTC') |
---|
[fd6da93] | 79 | locale = validators.OneOf( |
---|
| 80 | [locale[0] for locale in get_available_locale_names()], |
---|
| 81 | if_missing='en' |
---|
| 82 | ) |
---|
[5ec3a0b] | 83 | |
---|
| 84 | |
---|
| 85 | class ChangePasswordSchema(Schema): |
---|
| 86 | allow_extra_fields = True |
---|
| 87 | filter_extra_fields = True |
---|
| 88 | |
---|
| 89 | old_password = PasswordMatch(not_empty=True) |
---|
| 90 | password = validators.UnicodeString(min=9, not_empty=True) |
---|
| 91 | password_confirm = validators.UnicodeString(not_empty=True) |
---|
| 92 | chained_validators = [validators.FieldsMatch( |
---|
| 93 | 'password', 'password_confirm')] |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | class SignUpSchema(Schema): |
---|
| 97 | """ |
---|
| 98 | Schema for the sign up of new users |
---|
| 99 | """ |
---|
| 100 | allow_extra_fields = True |
---|
| 101 | filter_extra_fields = True |
---|
[722ae18] | 102 | nickname = UniqueNickname(if_missing='') |
---|
[5ec3a0b] | 103 | firstname = validators.UnicodeString(not_empty=True) |
---|
| 104 | lastname = validators.UnicodeString(not_empty=True) |
---|
| 105 | email = UniqueEmail(not_empty=True) |
---|
| 106 | password = validators.UnicodeString(min=9, not_empty=True) |
---|
| 107 | password_confirm = validators.UnicodeString(not_empty=True) |
---|
| 108 | chained_validators = [validators.FieldsMatch( |
---|
| 109 | 'password', 'password_confirm')] |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | class RecoverPasswordSchema(Schema): |
---|
| 113 | """ |
---|
| 114 | Schema for the password recovery |
---|
| 115 | """ |
---|
| 116 | allow_extra_fields = True |
---|
| 117 | filter_extra_fields = True |
---|
[722ae18] | 118 | nickname = UniqueNickname(if_missing='') |
---|
[5ec3a0b] | 119 | email = UniqueEmail(not_empty=True) |
---|