source: OpenWorkouts-current/ow/schemas/user.py @ 1d92bf2

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

(#37) Allow login using email address instead of username:

  • Use user uids as keys in the root folder for referencing user objects (instead of username)
  • Use uids for referencing users all over the place (auth, permissions, traversal urls, etc)
  • Replaced the username concept with nickname. This nickname will be used as a shortcut to access "public profile" pages for users
  • Reworked lots of basic methods in the OpenWorkouts root object (s/username/nickname, marked as properties some methods like users, emails, etc)
  • Added new add_user() and delete_user() helpers to the OpenWorkouts root object
  • Fixed bug in the dashboard redirect view, causing an endless loop if an authenticated user does not exist anymore when loading a page.
  • Lots of tests fixes, adaptations and catch up.
  • Property mode set to 100644
File size: 3.5 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 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
33class 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
46class 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
59class 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
76class 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
87class 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
103class 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)
Note: See TracBrowser for help on using the repository browser.