-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
97 lines (80 loc) · 4.5 KB
/
forms.py
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
from cgi import escape
from models import User, Address, Contact
from wtforms import Form, BooleanField, FileField, SelectField, StringField, PasswordField
from wtforms.compat import text_type
from wtforms.ext.appengine.ndb import model_form
from wtforms.validators import InputRequired, Email, EqualTo, Optional, Regexp, ValidationError
from wtforms.widgets import html_params, HTMLString
import functools
state_list = ['State',
'AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA', 'HI',
'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI',
'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV',
'NY', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT',
'VA', 'VT', 'WA', 'WI', 'WV', 'WY']
state_select = [(state, state) for state in state_list]
state_select[0] = ("", 'State')
class SelectWithDisable(object):
def __init__(self, multiple=False):
self.multiple = multiple
def __call__(self, field, **kwargs):
kwargs.setdefault('id', field.id)
if self.multiple:
kwargs['multiple'] = True
html = ['<select %s>' % html_params(name=field.name, **kwargs)]
for val, label, selected, disabled in field.iter_choices():
html.append(self.render_option(val, label, selected, disabled))
html.append('</select>')
return HTMLString(''.join(html))
@classmethod
def render_option(cls, value, label, selected, disabled=False, **kwargs):
options = dict(kwargs, value=value)
if selected:
options['selected'] = True
if disabled:
options['disabled'] = True
return HTMLString('<option %s>%s</option>' % (html_params(**options), escape(text_type(label))))
class SelectFieldWithDisable(SelectField):
widget = SelectWithDisable()
def __init__(self, label=None, validators=None, coerce=text_type,
choices=None, disabled=[], **kwargs):
super(SelectFieldWithDisable, self).__init__(label, validators, coerce, choices, **kwargs)
self.disabled = dict((opt, True) for opt in disabled)
def iter_choices(self):
for value, label in self.choices:
# This line is odd. According to the docs, the values for a select
# field are tuples of the form (value, label). However, in order
# to get an empty string for the value of 'State' (my default
# option), while also disabling it, I have to do 'value in
# self.disabled' instead of 'label in self.disabled', and pass the
# tuple '("", 'State')'. Strange.
yield (value, label, self.coerce(value) == self.data, value in self.disabled)
class ContactForm(Form):
fname = StringField('First name', [InputRequired(message="You must provide a first name")])
lname = StringField('Last name', [InputRequired(message="You must provide a last name")])
email = StringField('Email', [Email(message="That's not a valid email address"), Optional()])
phone = StringField('Phone number', [Regexp('\d{3}\D*\d{3}\D*\d{4}\D*\d*',
message="That's not a valid phone number"),
Optional()])
street = StringField('Street Address', [Regexp('\d+\s+[\w\s.\d]+'),
Optional()])
city = StringField('City')
state = SelectFieldWithDisable('State', choices=state_select,
disabled=['State'], default='State')
photo = FileField('Photo', [Regexp('\Aimage/', message="That's not an image file"), Optional()])
class ContactSearchForm(Form):
query = StringField('Search')
state = SelectFieldWithDisable('Filter by State', choices=state_select,
disabled=['State'], default='State')
class LoginForm(Form):
username = StringField('Username', [InputRequired(message="Please provide your username")])
password = PasswordField('Password', [InputRequired(message="Please provide your password")])
class SignupForm(Form):
email = StringField('Email', [Email(message="That's not a valid email address"),
InputRequired()])
username = StringField('Username', [InputRequired(message="Please provide a username")])
password = PasswordField(
'Password',
[InputRequired("Please provide a password"),
EqualTo('password', message="Passwords must match")])
check_password = PasswordField('Re-enter password')