Repository navigation
Expand file tree
/
Copy pathforms.py
More file actions
125 lines (94 loc) · 5.72 KB
/
Copy pathforms.py
File metadata and controls
125 lines (94 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
Created on June 10, 2012
@author: peta15
"""
from wtforms import fields
from wtforms import Form
from wtforms import validators
from lib import utils
from webapp2_extras.i18n import lazy_gettext as _
from webapp2_extras.i18n import ngettext, gettext
FIELD_MAXLENGTH = 50 # intended to stop maliciously long input
class FormTranslations(object):
def gettext(self, string):
return gettext(string)
def ngettext(self, singular, plural, n):
return ngettext(singular, plural, n)
class BaseForm(Form):
def __init__(self, request_handler):
super(BaseForm, self).__init__(request_handler.request.POST)
def _get_translations(self):
return FormTranslations()
# ==== Mixins ====
class PasswordConfirmMixin(BaseForm):
password = fields.TextField(_('Password'), [validators.Required(),
validators.Length(max=FIELD_MAXLENGTH, message=_(
"Field cannot be longer than %(max)d characters."))])
c_password = fields.TextField(_('Confirm Password'),
[validators.Required(), validators.EqualTo('password', _('Passwords must match.')),
validators.Length(max=FIELD_MAXLENGTH,
message=_("Field cannot be longer than %(max)d characters."))])
class UsernameMixin(BaseForm):
username = fields.TextField(_('Username'), [validators.Required(),
validators.Length(max=FIELD_MAXLENGTH, message=_(
"Field cannot be longer than %(max)d characters.")),
validators.regexp(utils.VALID_USERNAME_REGEXP, message=_(
"Username invalid. Use only letters and numbers."))])
class UsernameEmailMixin(BaseForm):
username = fields.TextField(_('Username'), [validators.Required(),
validators.Length(max=FIELD_MAXLENGTH, message=_(
"Field cannot be longer than %(max)d characters."))])
class NameMixin(BaseForm):
name = fields.TextField(_('Name'), [
validators.Length(max=FIELD_MAXLENGTH, message=_("Field cannot be longer than %(max)d characters.")),
validators.regexp(utils.NAME_LASTNAME_REGEXP, message=_(
"Name invalid. Use only letters and numbers."))])
last_name = fields.TextField(_('Last Name'), [
validators.Length(max=FIELD_MAXLENGTH, message=_("Field cannot be longer than %(max)d characters.")),
validators.regexp(utils.NAME_LASTNAME_REGEXP, message=_(
"Last Name invalid. Use only letters and numbers."))])
class EmailMixin(BaseForm):
email = fields.TextField(_('Email'), [validators.Required(),
validators.Length(min=8, max=FIELD_MAXLENGTH, message=_(
"Field must be between %(min)d and %(max)d characters long.")),
validators.regexp(utils.EMAIL_REGEXP, message=_('Invalid email address.'))])
# ==== Forms ====
class PasswordResetCompleteForm(PasswordConfirmMixin):
pass
class LoginForm(UsernameEmailMixin):
password = fields.TextField(_('Password'), [validators.Required(),
validators.Length(max=FIELD_MAXLENGTH, message=_(
"Field cannot be longer than %(max)d characters."))],
id='l_password')
pass
class ContactForm(EmailMixin):
name = fields.TextField(_('Name'), [validators.Required(),
validators.Length(max=FIELD_MAXLENGTH, message=_(
"Field cannot be longer than %(max)d characters.")),
validators.regexp(utils.NAME_LASTNAME_REGEXP, message=_(
"Name invalid. Use only letters and numbers."))])
message = fields.TextAreaField(_('Message'), [validators.Required(), validators.Length(max=65536)])
pass
class RegisterForm(PasswordConfirmMixin, UsernameMixin, NameMixin, EmailMixin):
#country = fields.SelectField(_('Country'), choices=[])
#tz = fields.SelectField(_('Timezone'), choices=[])
pass
class EditProfileForm(UsernameMixin, NameMixin):
country = fields.SelectField(_('Country'), choices=[])
tz = fields.SelectField(_('Timezone'), choices=[])
pass
class EditPasswordForm(PasswordConfirmMixin):
current_password = fields.TextField(_('Password'), [validators.Required(),
validators.Length(max=FIELD_MAXLENGTH, message=_(
"Field cannot be longer than %(max)d characters."))])
pass
class EditEmailForm(BaseForm):
new_email = fields.TextField(_('Email'), [validators.Required(),
validators.Length(min=8, max=FIELD_MAXLENGTH, message=_(
"Field must be between %(min)d and %(max)d characters long.")),
validators.regexp(utils.EMAIL_REGEXP,
message=_('Invalid email address.'))])
password = fields.TextField(_('Password'), [validators.Required(),
validators.Length(max=FIELD_MAXLENGTH, message=_(
"Field cannot be longer than %(max)d characters."))])
pass