Commit c81df6e760c647ab27335ecd7de4f66ef7816e29
1 parent
94906fa5
Exists in
master
Add the forms models.
A good chunk of our logic will be in here. Trying to refactor the textual content out of it and into YAML… More commits will follow.
Showing
1 changed file
with
117 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,117 @@ | @@ -0,0 +1,117 @@ | ||
1 | +from flask_wtf import Form | ||
2 | +from wtforms import \ | ||
3 | + StringField, \ | ||
4 | + PasswordField, \ | ||
5 | + TextAreaField, \ | ||
6 | + BooleanField | ||
7 | +from wtforms import validators | ||
8 | + | ||
9 | +from .models import User | ||
10 | + | ||
11 | + | ||
12 | +# ESTIMATION FORM ############################################################# | ||
13 | + | ||
14 | +class EstimateForm(Form): | ||
15 | + email = StringField( | ||
16 | + label=u"Email Address", | ||
17 | + description=u"Make sure you provide a valid address " | ||
18 | + u"or you won't receive the results.", | ||
19 | + validators=[ | ||
20 | + validators.DataRequired(), | ||
21 | + validators.Email(), | ||
22 | + ], | ||
23 | + ) | ||
24 | + first_name = StringField( | ||
25 | + label=u"First Name", | ||
26 | + description=u"Also known as given name, eg. `Didier`.", | ||
27 | + validators=[ | ||
28 | + validators.Optional(), | ||
29 | + validators.Length(max=1024), | ||
30 | + ], | ||
31 | + ) | ||
32 | + last_name = StringField( | ||
33 | + label=u"Last Name", | ||
34 | + description=u"Also known as family name, eg. `Barret`.", | ||
35 | + validators=[ | ||
36 | + validators.Optional(), | ||
37 | + validators.Length(max=1024), | ||
38 | + ], | ||
39 | + ) | ||
40 | + institution = StringField( | ||
41 | + label=u"Institution / Enterprise", | ||
42 | + description=u"If any.", | ||
43 | + validators=[ | ||
44 | + validators.Optional(), | ||
45 | + validators.Length(max=1024), | ||
46 | + ], | ||
47 | + ) | ||
48 | + comment = TextAreaField( | ||
49 | + label=u"Leave a comment", | ||
50 | + description=u"Any input is appreciated. Everyone's a critic.", | ||
51 | + validators=[ | ||
52 | + validators.Optional(), | ||
53 | + validators.Length(max=2048), | ||
54 | + ], | ||
55 | + ) | ||
56 | + origin_addresses = TextAreaField( | ||
57 | + label=u"Origin Cities", | ||
58 | + description=u"One address per line, in the form `City, Country`. " | ||
59 | + u"Make sure your addresses are correctly spelled.", | ||
60 | + validators=[ | ||
61 | + validators.DataRequired(), | ||
62 | + ], | ||
63 | + ) | ||
64 | + destination_addresses = TextAreaField( | ||
65 | + label=u"Destination Cities", | ||
66 | + description=u"One address per line, in the form `City, Country`. " | ||
67 | + u"Make sure your addresses are correctly spelled.", | ||
68 | + validators=[ | ||
69 | + validators.DataRequired(), | ||
70 | + ], | ||
71 | + ) | ||
72 | + should_compute_optimal_destination = BooleanField( | ||
73 | + label=u"Compute the destination city " | ||
74 | + u"that will minimize emissions? <br>" | ||
75 | + u"(useful when setting up a meeting/conference)", | ||
76 | + description=u"", | ||
77 | + default=False, | ||
78 | + validators=[ | ||
79 | + validators.Optional(), | ||
80 | + ], | ||
81 | + ) | ||
82 | + | ||
83 | + def validate(self): | ||
84 | + check_validate = super(EstimateForm, self).validate() | ||
85 | + | ||
86 | + # Do our validators pass? | ||
87 | + if not check_validate: | ||
88 | + return False | ||
89 | + | ||
90 | + return True | ||
91 | + | ||
92 | + | ||
93 | +# LOGIN FORM ################################################################## | ||
94 | + | ||
95 | +class LoginForm(Form): | ||
96 | + username = StringField(u'Username', validators=[validators.required()]) | ||
97 | + password = PasswordField(u'Password', validators=[validators.optional()]) | ||
98 | + | ||
99 | + def validate(self): | ||
100 | + check_validate = super(LoginForm, self).validate() | ||
101 | + | ||
102 | + # Do our validators pass? | ||
103 | + if not check_validate: | ||
104 | + return False | ||
105 | + | ||
106 | + # Does the user even exist? | ||
107 | + user = User.query.filter_by(username=self.username.data).first() | ||
108 | + if not user: | ||
109 | + self.username.errors.append('Invalid username or password') | ||
110 | + return False | ||
111 | + | ||
112 | + # Do the passwords match? | ||
113 | + if not user.check_password(self.password.data): | ||
114 | + self.username.errors.append('Invalid username or password') | ||
115 | + return False | ||
116 | + | ||
117 | + return True |