Skip to content

Commit

Permalink
BGDIINF_SB-2378: Corrected the CSV output
Browse files Browse the repository at this point in the history
The profile.csv endpoint did not returned a well csv formed format, but
a python dictionary. This has been fixed also fixing the 203 answer
code.
  • Loading branch information
ltshb committed May 2, 2022
1 parent 897b9cf commit 17698fd
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions app/routes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import csv
import json
import logging
from io import StringIO

from shapely.geometry import Point
from werkzeug.exceptions import HTTPException
Expand Down Expand Up @@ -97,7 +99,17 @@ def profile_csv_route():
if "callback" in request.args:
abort(400, 'callback parameter not supported')
profile, status_code = _get_profile(False)
return str(profile), status_code, {'Content-Type': 'text/csv'}
csv.register_dialect(
'semi-colon', delimiter=';', quoting=csv.QUOTE_ALL, quotechar='"', lineterminator='\r\n'
)
buffer = StringIO()
writer = csv.writer(buffer, dialect='semi-colon')
# write header
writer.writerow(profile['headers'])
writer.writerows(profile['rows'])
buffer.seek(0)

return buffer.read(), status_code, {'Content-Type': 'text/csv'}


def _get_profile(output_to_json):
Expand Down Expand Up @@ -143,8 +155,11 @@ def _get_profile(output_to_json):
# need to add points closer to each other than the min resolution of 2m), we return HTTP 203 to
# notify that nb_points couldn't be match.
status_code = 200
if is_custom_nb_points and len(result) < nb_points:
if output_to_json and is_custom_nb_points and len(result) < nb_points:
status_code = 203
elif not output_to_json and is_custom_nb_points and len(result['rows']) < nb_points:
status_code = 203

return result, status_code


Expand Down

0 comments on commit 17698fd

Please sign in to comment.