-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.py
More file actions
256 lines (216 loc) · 17.2 KB
/
Copy pathforms.py
File metadata and controls
256 lines (216 loc) · 17.2 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
from flask_wtf import FlaskForm
from wtforms import *
from wtforms.validators import *
from datetime import timedelta, date
from new_iata_codes import all_cities_international
city_list = [all_cities_international[iata] for iata in all_cities_international]
# ------------------------------------------------------------------------------------------- #
# Custom validators for user's selection of date preferences
def invalid_max_nights(form, field):
if field.data < form.min_nights.data:
raise ValidationError("Max number of nights can't be less than your minimum number of nights.", "error")
def invalid_date(form, field):
if field.data < date.today():
raise ValidationError(f"You can't choose a {field.label.text} that is in the past! "
"Only future dates are accepted", "error")
def date_range_check(form, field):
if form.specific_search_start_date.data:
if field.data < form.specific_search_start_date.data:
raise ValidationError("You can't choose a Specific End Date that's "
"earlier than your Specific Start Date.", "error")
if field.data < (form.specific_search_start_date.data + timedelta(days=form.min_nights.data)):
raise ValidationError("You can't have a specific date range that is "
"shorter than your minimum nights setting.", "error")
# ------------------------------------------------------------------------------------------- #
# List of flask forms
# ChangeEmailForm
# ChangeNameForm
# ChangePasswordForm
# CityPriceForm
# DeleteAccountForm
# DestinationForm
# LoginForm
# PreferenceForm
# RegisterForm
# ResetPassword
# SendResetEmail
# SubmitTicketForm
# ------------------------------------------------------------------------------------------- #
# Flask forms (wtforms)
class ChangeEmailForm(FlaskForm):
email = EmailField("New Email Address", validators=[InputRequired(message="'New Email Address' field is required"),
Email(granular_message=True, check_deliverability=True)])
confirm_email = EmailField("Confirm New Email",
validators=[InputRequired(message="'Confirm New Email' field is required"),
EqualTo('email', message='The email addresses must match exactly')])
password = PasswordField("Current Password",
validators=[InputRequired(message="'Current Password' field is required")])
submit = SubmitField("Send Confirmation Email")
class ChangeNameForm(FlaskForm):
name = StringField("New Name", validators=[InputRequired(message="New Name field is required")])
submit = SubmitField("Update Name")
class ChangePasswordForm(FlaskForm):
password = PasswordField("Current Password",
validators=[InputRequired(message="'Current Password' field is required")])
new_password = PasswordField("New Password",
validators=[InputRequired(message="'New Password' field is required"),
Length(min=8, max=22, message="Password must be between 8 "
"and 22 characters")])
confirm_password = PasswordField("Confirm Password",
validators=[InputRequired(message="'Confirm New Password' field is required"),
Length(min=8, max=22, message="Password must be between 8 "
"and 22 characters"),
EqualTo('new_password', message='The passwords must match exactly')])
submit = SubmitField("Change Password")
class CityPriceForm(Form):
city = StringField("City Name",
validators=[InputRequired(message="'City Name' field is required"),
AnyOf(values=city_list,
message="Invalid 'City Name'")])
price_ceiling = IntegerField("Price Ceiling",
validators=[InputRequired(message="'Price Ceiling' field is required"),
NumberRange(min=1, message="Price Ceiling must be greater than 0")])
class DeleteAccountForm(FlaskForm):
email = EmailField("Email", validators=[InputRequired(message="'Email' field is required"),
Email(granular_message=True)])
password = PasswordField("Password", validators=[InputRequired(message="'Password' field is required")])
submit = SubmitField("Delete Account")
# Note this form uses a 'FieldList and FormField' for destinations. This allows users to dynamically add destinations
# However, this creates a need for some work-arounds in main.py as it doesn't operate like normal flask form fields.
class DestinationForm(FlaskForm):
search = SearchField("Search field")
home_airport = StringField("Home Airport", validators=[InputRequired(message="'Home Airport' field is required"),
AnyOf(values=city_list,
message="Invalid 'Home Airport'")])
currency = SelectField("Currency", choices=['USD', 'EUR', 'SGD', 'AUD', 'THB', 'CNY', 'HUF', 'GBP', 'CAD'],
validators=[InputRequired(message="'Currency' field is required")])
destinations = FieldList(FormField(CityPriceForm), min_entries=3, max_entries=10)
class LoginForm(FlaskForm):
email = EmailField("Email", validators=[InputRequired(message="'Email' field is required"),
Email(granular_message=True)])
password = PasswordField("Password", validators=[InputRequired(message="'Password' field is required")])
submit = SubmitField("Login")
# While not actually used directly, the 'description' kwarg matches the tooltip description that users see on the page
class PreferenceForm(FlaskForm):
email_frequency = SelectField("Email Frequency", choices=[(1, "Once a week"), (2, "Once every two weeks"),
(4, "Once a month")],
validators=[InputRequired(message="'Email Frequency' field is required")])
email_day = SelectField("Day of Week to Receive Email",
choices=[(0, "Monday"), (1, "Tuesday"), (2, "Wednesday"), (3, "Thursday"),
(4, "Friday"), (5, "Saturday"), (6, "Sunday")])
cabin_class = SelectField("Cabin Class", choices=[('M', 'Economy'), ('W', 'Premium Economy'),
('C', 'Business'), ('F', 'First Class')], validators=[Optional()])
max_stops = IntegerField("Max Stops (1-way)",
validators=[Optional(), NumberRange(min=0, message="'Max Stops' can't be less than 0")],
description="Max number of stops for one-way of the trip. If you have destinations "
"that are remote or quite far from your home airport "
"(overseas, islands, rural cities, etc.), "
"direct flights (i.e. 0 stops) might not be available.")
max_flight_time = IntegerField("Max Travel Time (1-way)",
validators=[Optional(), NumberRange(min=1, message="'Max Flight Duration' "
"must be greater than 0")],
description="Max number of hours for one-way trip duration. Trip duration includes "
"layovers and stops. If you have destinations that are remote or quite "
"far from your home airport (overseas, islands, rural cities, etc.), "
"short flight times might not be available.")
exclude_airlines = SelectField("Exclude Bad Airlines?",
choices=[('false', 'Include All Airlines'),
('true', 'Exclude Bad Airlines')],
validators=[Optional()],
description="Excludes airlines rated lowest in safety, service, claims processing, "
"and punctuality from your flight search (e.g. Ryan Air, EasyJet, "
"Lion Air, China Eastern, Spirit, etc.)")
ret_to_diff_airport = SelectField("Diff Airport Return?",
choices=[(0, 'Don\'t Include'),
(1, 'Include')],
validators=[Optional()],
description="Choose whether or not to include flights that return to a different "
"airport than the one from where you departed in your flight results. "
"For example, leaving JFK airport in New York City and returning "
"home to LGA in New York City.")
num_adults = IntegerField("Adults", validators=[InputRequired(message="'Adults' field is required"),
NumberRange(min=1, max=6,
message="Number of 'Adult' passengers must be "
"between 1 and 6.")])
num_children = IntegerField("Children (Age 2-11)",
validators=[InputRequired(message="'Children' field is required"),
NumberRange(min=0, max=4,
message="Number of 'Child' passengers must be "
"between 0 and 4.")])
num_infants = IntegerField("Infants (Age < 2)", validators=[InputRequired(message="'Infants' field is required"),
NumberRange(min=0, max=3,
message="Number of 'Infant' passengers must"
"be between 0 and 3.")])
min_nights = IntegerField("Min Nights", validators=[InputRequired(message="'Min Nights' field is required"),
NumberRange(min=0,
message="'Min Nights' can't be less than 0.")],
description="The minimum possible length of time spent at your travel destination. "
"Actual trip duration might be somewhere in-between the min and max nights")
max_nights = IntegerField("Max Nights", validators=[InputRequired(message="'Max Nights' field is required"),
NumberRange(min=0,
message="'Max Nights' can't be less than 0."),
invalid_max_nights],
description="The maximum possible length of time spent at your travel destination. "
"Actual trip duration might be somewhere in-between the min and max nights")
search_start_date = SelectField("Search Lead Time", coerce=int,
choices=[(1, 'One day'), (7, 'One week'),
(14, 'Two weeks'), (21, 'Three weeks'), (30, 'One month'),
(60, 'Two months'), (90, 'Three months')],
validators=[InputRequired(message="'Lead Time' field is required")],
description="This is a rolling date, meaning it will move as time passes. "
"For example: If you choose 'two weeks', then if the flight search "
"occurs on March 10th, it will look for flights starting from "
"March 24th onwards.")
search_length = SelectField("Search Time Window", coerce=int,
choices=[(14, 'Two weeks'), (21, 'Three weeks'), (30, 'One month'),
(60, 'Two months'), (90, 'Three months'), (120, 'Four months'),
(150, 'Five months'), (180, 'Six months')],
validators=[InputRequired(message="'Time Window' field is required")],
description="This is a rolling date range, meaning it will move as time passes. "
"For example, if you have a lead time of '1 month' and a time window of "
"'3 months' , then if it searches on September 10th, it would look for all "
"flights from October 10th to January 10th.")
specific_search_start_date = DateField('Specific Start Date', validators=[Optional(), invalid_date],
description="This is a fixed date which will not move as time passes. "
"Choose this if you have a specific date range available to "
"travel (holidays, summer vacation, etc.), "
"otherwise leave blank.")
specific_search_end_date = DateField('Specific End Date',
validators=[Optional(), invalid_date, date_range_check],
description="This is a fixed date which will not move as time passes. "
"Choose this if you have a specific date range available to "
"travel (holidays, summer vacation, etc.), otherwise leave blank.")
submit = SubmitField("Save Changes")
class RegisterForm(FlaskForm):
name = StringField("First Name", validators=[InputRequired(message="'Name' field is required")])
email = EmailField("Email", validators=[InputRequired(message="'Email' field is required"),
Email(granular_message=True, check_deliverability=True)])
password = PasswordField("Password", validators=[InputRequired(message="'Password' field is required"),
Length(min=8, max=22, message="Password must be between 8 "
"and 22 characters")])
confirm_password = PasswordField("Confirm Password",
validators=[InputRequired(message="'Confirm Password' field is required"),
Length(min=8, max=22, message="Password must be between 8 "
"and 22 characters"),
EqualTo('password', message='The passwords must match exactly')])
submit = SubmitField("Register")
class ResetPassword(FlaskForm):
email = EmailField("Your Email", validators=[InputRequired(message="'Your Email' field is required")])
password = PasswordField("New Password", validators=[InputRequired(message="'New Password' field is required"),
Length(min=8, max=22, message="Password must be between 8 "
"and 22 characters")])
confirm_password = PasswordField("Confirm Password",
validators=[InputRequired(message="'Confirm Password' field is required"),
Length(min=8, max=22, message="Password must be between 8 "
"and 22 characters"),
EqualTo('password', message='Passwords must match exactly')])
submit = SubmitField("Send Reset Email")
class SendResetEmail(FlaskForm):
email = EmailField("Email Address", validators=[InputRequired(message="'Email Address' field is required")])
submit = SubmitField("Send Reset Email")
class SubmitTicketForm(FlaskForm):
issue_subject = StringField("Issue Subject",
validators=[InputRequired(message="'Issue Subject' field is required")])
issue_description = TextAreaField("Description of Issue",
validators=[InputRequired(message="'Description of Issue' field is required")])
submit = SubmitField("Submit")