source: OpenWorkouts-current/ow/schemas/user.py @ 5ec3a0b

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

Imported sources from the old python2-only repository:

  • Modified the code so it is python 3.6 compatible
  • Fixed deprecation warnings, pyramid 1.10.x supported now
  • Fixed deprecation warnings about some libraries, like pyramid-simpleform
  • Added pytest-pycodestyle and pytest-flakes for automatic checks on the source code files when running tests.
  • Added default pytest.ini setup to enforce some default parameters when running tests.
  • Cleaned up the code a bit, catched up with tests coverage.
  • 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 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
34class 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
47class 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
60class 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
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    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
104class 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)
Note: See TracBrowser for help on using the repository browser.