From 9ac2668938880f180000fac2f11c1281c65058f2 Mon Sep 17 00:00:00 2001 From: Jesse Vickery Date: Mon, 28 Aug 2023 15:14:58 +0000 Subject: [PATCH] fix(views): member dump with new datastore csv writer; - Updated member dump view method to work with the new datastore streaming csv writer. --- ckanext/canada/view.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/ckanext/canada/view.py b/ckanext/canada/view.py index a4cdf3336..7b7fca994 100644 --- a/ckanext/canada/view.py +++ b/ckanext/canada/view.py @@ -8,7 +8,7 @@ from socket import error as socket_error from logging import getLogger from ckanext.datastore.writer import csv_writer -from six import string_types +from six import string_types, PY2 from ckan.plugins.toolkit import ( abort, @@ -73,6 +73,10 @@ from ckanext.canada.urlsafe import url_part_unescape, url_part_escape from ckanext.canada.helpers import canada_date_str_to_datetime +if PY2: + from cStringIO import StringIO +else: + from io import StringIO canada_views = Blueprint('canada', __name__) ottawa_tz = timezone('America/Montreal') @@ -1073,16 +1077,26 @@ def organization_member_dump(id): {'id': _('Name')}, {'id': _('Role')}] - response = make_response() + output_stream = StringIO() def start_writer(fields): - file_name = u'{org_id}-{members}'.format( - org_id=org_dict.name, - members=_(u'members')) - return writer_factory(response, fields, file_name, bom=True) + return writer_factory(output_stream, fields, bom=True) with start_writer(fields) as wr: wr.write_records(results) + file_name = u'{org_id}-{members}'.format( + org_id=org_dict.name, + members=_(u'members')) + + output_stream.seek(0) + response = make_response(output_stream.read()) + output_stream.close() + content_disposition = u'attachment; filename="{name}.csv"'.format( + name=file_name) + content_type = b'text/csv; charset=utf-8' + response.headers['Content-Type'] = content_type + response.headers['Content-Disposition'] = content_disposition + return response