Commit f2fbbb72ab85c4f7ae3517ec526240bdc4e36107
1 parent
00e35997
Exists in
master
Display a nice error message when uploaded files are not up to spec.
Showing
1 changed file
with
32 additions
and
11 deletions
Show diff stats
flaskr/controllers/main_controller.py
... | ... | @@ -15,6 +15,8 @@ from flaskr.geocoder import CachedGeocoder |
15 | 15 | from flaskr.core import generate_unique_id, get_emission_models |
16 | 16 | from flaskr.content import content |
17 | 17 | |
18 | +from wtforms import validators | |
19 | + | |
18 | 20 | from yaml import safe_dump as yaml_dump |
19 | 21 | |
20 | 22 | import csv |
... | ... | @@ -118,9 +120,13 @@ def gather_addresses(from_list, from_file): |
118 | 120 | if address is not None: |
119 | 121 | addresses.append(address) |
120 | 122 | else: |
121 | - pass # what should we do here? raise? | |
123 | + raise validators.ValidationError( | |
124 | + "We could not find Address data in the spreadsheet." | |
125 | + ) | |
122 | 126 | else: |
123 | - pass # what should we do here? raise? | |
127 | + raise validators.ValidationError( | |
128 | + "We could not find any data in the spreadsheet." | |
129 | + ) | |
124 | 130 | |
125 | 131 | else: |
126 | 132 | addresses = from_list.replace("\r", '').split("\n") |
... | ... | @@ -134,6 +140,9 @@ def estimate(): |
134 | 140 | models = get_emission_models() |
135 | 141 | form = EstimateForm() |
136 | 142 | |
143 | + def show_form(): | |
144 | + return render_template("estimate.html", form=form, models=models) | |
145 | + | |
137 | 146 | if form.validate_on_submit(): |
138 | 147 | |
139 | 148 | id = generate_unique_id() |
... | ... | @@ -144,15 +153,27 @@ def estimate(): |
144 | 153 | estimation.last_name = form.last_name.data |
145 | 154 | estimation.institution = form.institution.data |
146 | 155 | estimation.status = StatusEnum.pending |
147 | - estimation.origin_addresses = gather_addresses( | |
148 | - form.origin_addresses.data, | |
149 | - form.origin_addresses_file.data | |
150 | - ) | |
151 | - estimation.destination_addresses = gather_addresses( | |
152 | - form.destination_addresses.data, | |
153 | - form.destination_addresses_file.data | |
154 | - ) | |
156 | + | |
157 | + try: | |
158 | + estimation.origin_addresses = gather_addresses( | |
159 | + form.origin_addresses.data, | |
160 | + form.origin_addresses_file.data | |
161 | + ) | |
162 | + except validators.ValidationError as e: | |
163 | + form.origin_addresses_file.errors.append(e.message) | |
164 | + return show_form() | |
165 | + | |
166 | + try: | |
167 | + estimation.destination_addresses = gather_addresses( | |
168 | + form.destination_addresses.data, | |
169 | + form.destination_addresses_file.data | |
170 | + ) | |
171 | + except validators.ValidationError as e: | |
172 | + form.destination_addresses_file.errors.append(e.message) | |
173 | + return show_form() | |
174 | + | |
155 | 175 | estimation.use_train_below_km = form.use_train_below_km.data |
176 | + | |
156 | 177 | models_slugs = [] |
157 | 178 | for model in models: |
158 | 179 | if getattr(form, 'use_model_%s' % model.slug).data: |
... | ... | @@ -170,7 +191,7 @@ def estimate(): |
170 | 191 | )) |
171 | 192 | # return render_template("estimate-debrief.html", form=form) |
172 | 193 | |
173 | - return render_template("estimate.html", form=form, models=models) | |
194 | + return show_form() | |
174 | 195 | |
175 | 196 | |
176 | 197 | @main.route("/invalidate") | ... | ... |