Commit 9621b8a5b76fd8bbb03b274c3aecd563299b3e3e

Authored by Antoine Goutenoir
1 parent e11f438a
Exists in master

Fix the CSV generation choking on unicode.

Showing 1 changed file with 14 additions and 3 deletions   Show diff stats
flaskr/controllers/main_controller.py
@@ -21,6 +21,9 @@ from yaml import safe_dump as yaml_dump @@ -21,6 +21,9 @@ from yaml import safe_dump as yaml_dump
21 main = Blueprint('main', __name__) 21 main = Blueprint('main', __name__)
22 22
23 23
  24 +OUT_ENCODING = 'utf-8'
  25 +
  26 +
24 @main.route('/favicon.ico') 27 @main.route('/favicon.ico')
25 def favicon(): # we want it served from the root, not from static/ 28 def favicon(): # we want it served from the root, not from static/
26 return send_from_directory( 29 return send_from_directory(
@@ -397,15 +400,23 @@ def consult_estimation(public_id, format): @@ -397,15 +400,23 @@ def consult_estimation(public_id, format):
397 400
398 si = StringIO() 401 si = StringIO()
399 cw = csv.writer(si, quoting=csv.QUOTE_ALL) 402 cw = csv.writer(si, quoting=csv.QUOTE_ALL)
400 - cw.writerow([u"city", u"co2 (g)"]) 403 + cw.writerow([u"city", u"address", u"co2 (g)"])
401 404
402 results = estimation.get_output_dict() 405 results = estimation.get_output_dict()
403 if 'mean_footprint' in results: 406 if 'mean_footprint' in results:
404 for city in results['mean_footprint']['cities']: 407 for city in results['mean_footprint']['cities']:
405 - cw.writerow([city['city'], city['address'], city['footprint']]) 408 + cw.writerow([
  409 + city['city'].encode(OUT_ENCODING),
  410 + city['address'].encode(OUT_ENCODING),
  411 + city['footprint']
  412 + ])
406 elif 'cities' in results: 413 elif 'cities' in results:
407 for city in results['cities']: 414 for city in results['cities']:
408 - cw.writerow([city['city'], city['address'], city['total']]) 415 + cw.writerow([
  416 + city['city'].encode(OUT_ENCODING),
  417 + city['address'].encode(OUT_ENCODING),
  418 + city['total']
  419 + ])
409 420
410 # HTTP headers? 421 # HTTP headers?
411 return si.getvalue().strip('\r\n') 422 return si.getvalue().strip('\r\n')