Merge remote-tracking branch 'refs/remotes/rodney757/issue643' into mergetest
[mediagoblin.git] / mediagoblin / auth / forms.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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
17 import wtforms
18
19 from mediagoblin.tools.mail import normalize_email
20 from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
21
22 def normalize_user_or_email_field(allow_email=True, allow_user=True):
23 """Check if we were passed a field that matches a username and/or email pattern
24
25 This is useful for fields that can take either a username or email
26 address. Use the parameters if you want to only allow a username for
27 instance"""
28 message = _(u'Invalid User name or email address.')
29 nomail_msg = _(u"This field does not take email addresses.")
30 nouser_msg = _(u"This field requires an email address.")
31
32 def _normalize_field(form, field):
33 email = u'@' in field.data
34 if email: # normalize email address casing
35 if not allow_email:
36 raise wtforms.ValidationError(nomail_msg)
37 wtforms.validators.Email()(form, field)
38 field.data = normalize_email(field.data)
39 else: # lower case user names
40 if not allow_user:
41 raise wtforms.ValidationError(nouser_msg)
42 wtforms.validators.Length(min=3, max=30)(form, field)
43 wtforms.validators.Regexp(r'^\w+$')(form, field)
44 field.data = field.data.lower()
45 if field.data is None: # should not happen, but be cautious anyway
46 raise wtforms.ValidationError(message)
47 return _normalize_field
48
49
50 class RegistrationForm(wtforms.Form):
51 username = wtforms.TextField(
52 _('Username'),
53 [wtforms.validators.Required(),
54 normalize_user_or_email_field(allow_email=False)])
55 password = wtforms.PasswordField(
56 _('Password'),
57 [wtforms.validators.Required(),
58 wtforms.validators.Length(min=5, max=1024)])
59 email = wtforms.TextField(
60 _('Email address'),
61 [wtforms.validators.Required(),
62 normalize_user_or_email_field(allow_user=False)])
63
64
65 class LoginForm(wtforms.Form):
66 username = wtforms.TextField(
67 _('Username or Email'),
68 [wtforms.validators.Required(),
69 normalize_user_or_email_field()])
70 password = wtforms.PasswordField(
71 _('Password'),
72 [wtforms.validators.Required(),
73 wtforms.validators.Length(min=5, max=1024)])
74
75
76 class ForgotPassForm(wtforms.Form):
77 username = wtforms.TextField(
78 _('Username or email'),
79 [wtforms.validators.Required(),
80 normalize_user_or_email_field()])
81
82
83 class ChangePassForm(wtforms.Form):
84 password = wtforms.PasswordField(
85 'Password',
86 [wtforms.validators.Required(),
87 wtforms.validators.Length(min=5, max=1024)])
88 userid = wtforms.HiddenField(
89 '',
90 [wtforms.validators.Required()])
91 token = wtforms.HiddenField(
92 '',
93 [wtforms.validators.Required()])