Restructure ForgotPassword view
[mediagoblin.git] / mediagoblin / auth / forms.py
CommitLineData
8e1e744d 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
24181820
CAW
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17import wtforms
25ba955e 18import re
24181820 19
a89df961 20from mediagoblin.tools.mail import normalize_email
ae3bc7fa 21from mediagoblin.tools.translate import fake_ugettext_passthrough as _
4b1adc13 22
a89df961
SS
23def normalize_user_or_email_field(allow_email=True, allow_user=True):
24 """Check if we were passed a field that matches a username and/or email pattern
25
26 This is useful for fields that can take either a username or email
27 address. Use the parameters if you want to only allow a username for
28 instance"""
29 message = _(u'Invalid User name or email address.')
30 nomail_msg = _(u"This field does not take email addresses.")
31 nouser_msg = _(u"This field requires an email address.")
32
33 def _normalize_field(form, field):
34 email = u'@' in field.data
35 if email: # normalize email address casing
36 if not allow_email:
37 raise wtforms.ValidationError(nomail_msg)
38 wtforms.validators.Email()(form, field)
39 field.data = normalize_email(field.data)
40 else: # lower case user names
41 if not allow_user:
42 raise wtforms.ValidationError(nouser_msg)
43 wtforms.validators.Length(min=3, max=30)(form, field)
44 wtforms.validators.Regexp(r'^\w+$')(form, field)
45 field.data = field.data.lower()
46 if field.data is None: # should not happen, but be cautious anyway
47 raise wtforms.ValidationError(message)
48 return _normalize_field
49
24181820
CAW
50
51class RegistrationForm(wtforms.Form):
52 username = wtforms.TextField(
4b1adc13 53 _('Username'),
24181820 54 [wtforms.validators.Required(),
a89df961 55 normalize_user_or_email_field(allow_email=False)])
f5def6fe 56 password = wtforms.PasswordField(
4b1adc13 57 _('Password'),
24181820 58 [wtforms.validators.Required(),
a89df961 59 wtforms.validators.Length(min=5, max=1024)])
24181820 60 email = wtforms.TextField(
4b1adc13 61 _('Email address'),
24181820 62 [wtforms.validators.Required(),
a89df961 63 normalize_user_or_email_field(allow_user=False)])
a3776717
CAW
64
65
66class LoginForm(wtforms.Form):
67 username = wtforms.TextField(
4b1adc13 68 _('Username'),
a3776717 69 [wtforms.validators.Required(),
a89df961 70 normalize_user_or_email_field(allow_email=False)])
a3776717 71 password = wtforms.PasswordField(
4b1adc13 72 _('Password'),
a89df961
SS
73 [wtforms.validators.Required(),
74 wtforms.validators.Length(min=5, max=1024)])
25ba955e
AV
75
76
77class ForgotPassForm(wtforms.Form):
78 username = wtforms.TextField(
f646e2e1 79 _('Username or email'),
a89df961
SS
80 [wtforms.validators.Required(),
81 normalize_user_or_email_field()])
25ba955e
AV
82
83
84class ChangePassForm(wtforms.Form):
85 password = wtforms.PasswordField(
86 'Password',
87 [wtforms.validators.Required(),
a89df961 88 wtforms.validators.Length(min=5, max=1024)])
25ba955e
AV
89 userid = wtforms.HiddenField(
90 '',
91 [wtforms.validators.Required()])
92 token = wtforms.HiddenField(
93 '',
94 [wtforms.validators.Required()])