Compare View

switch
from
...
to
 
Commits (2)
VERSION
1   -1.3.6
  1 +1.3.11
... ...
flaskr/controllers/main_controller.py
... ... @@ -6,6 +6,7 @@ from io import StringIO
6 6 from os import unlink, getenv
7 7 from os.path import join
8 8  
  9 +import chardet
9 10 import geopy
10 11 import pandas
11 12 import sqlalchemy
... ... @@ -83,7 +84,14 @@ def gather_addresses(from_list, from_file):
83 84 addresses = []
84 85 if from_file:
85 86 file_mimetype = from_file.mimetype
86   - file_contents = from_file.read().decode()
  87 + file_contents_raw = from_file.read()
  88 + detected = chardet.detect(file_contents_raw)
  89 + if detected['encoding']:
  90 + file_contents = file_contents_raw.decode(
  91 + encoding=detected['encoding']
  92 + )
  93 + else:
  94 + file_contents = file_contents_raw.decode()
87 95  
88 96 rows_dicts = None
89 97  
... ... @@ -198,6 +206,12 @@ def estimate(): # register new estimation request, more accurately
198 206 except validators.ValidationError as e:
199 207 form.origin_addresses_file.errors.append(str(e))
200 208 return show_form()
  209 + except UnicodeDecodeError as e:
  210 + form.origin_addresses_file.errors.append(
  211 + "We only accept UTF-8 and UTF-16 encoded files, \n" +
  212 + "or files we can detect encoding from."
  213 + )
  214 + return show_form()
201 215  
202 216 try:
203 217 estimation.destination_addresses = gather_addresses(
... ... @@ -207,6 +221,12 @@ def estimate(): # register new estimation request, more accurately
207 221 except validators.ValidationError as e:
208 222 form.destination_addresses_file.errors.append(str(e))
209 223 return show_form()
  224 + except UnicodeDecodeError as e:
  225 + form.origin_addresses_file.errors.append(
  226 + "We only accept UTF-8 and UTF-16 encoded files, \n" +
  227 + "or files we can detect encoding from."
  228 + )
  229 + return show_form()
210 230  
211 231 estimation.use_train_below_km = form.use_train_below_km.data
212 232  
... ...
requirements.txt
... ... @@ -26,6 +26,7 @@ enum34==1.1.6
26 26 geopy==1.23.0
27 27 certifi==2019.11.28
28 28 python-dotenv==0.10.3
  29 +chardet==4.0.0
29 30  
30 31 # Force stable werkzeug
31 32 # see https://gitlab.irap.omp.eu/carbon/travel-carbon-footprint.irap.omp.eu/issues/38
... ...