Commit a4c03d8e24df344c33abe0dbdbeb5527e21439f6
1 parent
96ddf8ec
Exists in
master
Add the controller route to consult an estimation.
Also doubles as CSV generator.
Showing
1 changed file
with
66 additions
and
4 deletions
Show diff stats
flaskr/controllers/main_controller.py
1 | 1 | import importlib |
2 | 2 | |
3 | -from flask import Blueprint, render_template, flash, request, redirect, url_for | |
3 | +from flask import Blueprint, render_template, flash, request, redirect, \ | |
4 | + url_for, abort | |
4 | 5 | |
5 | 6 | from flaskr.extensions import cache |
6 | 7 | from flaskr.forms import LoginForm, EstimateForm |
... | ... | @@ -10,6 +11,7 @@ from flaskr.geocoder import CachedGeocoder |
10 | 11 | from flaskr.core import generate_unique_id |
11 | 12 | from flaskr.content import content |
12 | 13 | |
14 | +# from io import StringIO | |
13 | 15 | from yaml import safe_dump as yaml_dump |
14 | 16 | |
15 | 17 | import sqlalchemy |
... | ... | @@ -33,7 +35,7 @@ def estimate(): |
33 | 35 | id = generate_unique_id() |
34 | 36 | |
35 | 37 | estimation = Estimation() |
36 | - estimation.email = form.email.data | |
38 | + # estimation.email = form.email.data | |
37 | 39 | estimation.first_name = form.first_name.data |
38 | 40 | estimation.last_name = form.last_name.data |
39 | 41 | estimation.status = StatusEnum.pending |
... | ... | @@ -45,7 +47,11 @@ def estimate(): |
45 | 47 | db.session.commit() |
46 | 48 | |
47 | 49 | flash("Estimation request submitted successfully.", "success") |
48 | - return redirect(url_for(".home")) | |
50 | + return redirect(url_for( | |
51 | + endpoint=".consult_estimation", | |
52 | + public_id=estimation.public_id, | |
53 | + format='html' | |
54 | + )) | |
49 | 55 | # return render_template("estimate-debrief.html", form=form) |
50 | 56 | |
51 | 57 | return render_template("estimate.html", form=form) |
... | ... | @@ -58,7 +64,7 @@ def compute(): # process the queue of estimation requests |
58 | 64 | return "<pre>%s</pre>" % _msg |
59 | 65 | |
60 | 66 | def _handle_failure(_estimation, _failure_message): |
61 | - _estimation.status = StatusEnum.failed | |
67 | + _estimation.status = StatusEnum.failure | |
62 | 68 | _estimation.errors = _failure_message |
63 | 69 | db.session.commit() |
64 | 70 | |
... | ... | @@ -260,6 +266,62 @@ def compute(): # process the queue of estimation requests |
260 | 266 | else: |
261 | 267 | pass |
262 | 268 | |
269 | + # WRITE RESULTS INTO THE DATABASE ######################################### | |
270 | + | |
271 | + estimation.status = StatusEnum.success | |
272 | + estimation.output_yaml = yaml_dump(results) | |
273 | + db.session.commit() | |
274 | + | |
275 | + # FINALLY, RESPOND ######################################################## | |
276 | + | |
263 | 277 | response += yaml_dump(results) + "\n" |
264 | 278 | |
265 | 279 | return _respond(response) |
280 | + | |
281 | + | |
282 | +@main.route("/estimation/<public_id>.<format>") | |
283 | +def consult_estimation(public_id, format): | |
284 | + try: | |
285 | + estimation = Estimation.query \ | |
286 | + .filter_by(public_id=public_id) \ | |
287 | + .one() | |
288 | + except sqlalchemy.orm.exc.NoResultFound: | |
289 | + return abort(404) | |
290 | + except Exception as e: | |
291 | + # TODO: log | |
292 | + return abort(500) | |
293 | + | |
294 | + # allowed_formats = ['html'] | |
295 | + # if format not in allowed_formats: | |
296 | + # abort(404) | |
297 | + | |
298 | + if 'html' == format: | |
299 | + if estimation.status in [StatusEnum.pending]: | |
300 | + return render_template( | |
301 | + "estimation-queue-wait.html", | |
302 | + estimation=estimation | |
303 | + ) | |
304 | + else: | |
305 | + return render_template( | |
306 | + "estimation.html", | |
307 | + estimation=estimation | |
308 | + ) | |
309 | + | |
310 | + elif 'csv' == format: | |
311 | + | |
312 | + import csv | |
313 | + from cStringIO import StringIO | |
314 | + | |
315 | + si = StringIO() | |
316 | + cw = csv.writer(si, quoting=csv.QUOTE_ALL) | |
317 | + cw.writerow([u"city", u"co2 (g)"]) | |
318 | + | |
319 | + results = estimation.get_output_dict() | |
320 | + for city_name in results['mean_footprint']['cities'].keys(): | |
321 | + cw.writerow([city_name, results['mean_footprint']['cities'][city_name]]) | |
322 | + | |
323 | + # Where are the headers? | |
324 | + return si.getvalue().strip('\r\n') | |
325 | + | |
326 | + else: | |
327 | + abort(404) | ... | ... |