Skip to content

Commit

Permalink
[#3390] datastore: dump TSV with format=tsv
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Jan 6, 2017
1 parent 49817be commit 417f1ea
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
36 changes: 27 additions & 9 deletions ckanext/datastore/controller.py
Expand Up @@ -21,12 +21,12 @@
boolean_validator = get_validator('boolean_validator')

UTF8_BOM = u'\uFEFF'.encode('utf-8')

DUMP_FORMATS = 'csv', 'tsv'
PAGINATE_BY = 10000


class DatastoreController(BaseController):
def dump_csv(self, resource_id):
def dump(self, resource_id):
try:
offset = int_validator(request.GET.get('offset', 0), {})
except Invalid as e:
Expand All @@ -36,6 +36,30 @@ def dump_csv(self, resource_id):
except Invalid as e:
abort(400, u'limit: ' + e.error)
bom = boolean_validator(request.GET.get('bom'), {})
fmt = request.GET.get('format', 'csv')

def start_writer():
if fmt == 'csv':
response.headers['Content-Type'] = 'text/csv; charset=utf-8'
response.headers['Content-disposition'] = (
'attachment; filename="{name}.csv"'.format(
name=resource_id))
wr = csv.writer(response, encoding='utf-8')
elif fmt == 'tsv':
response.headers['Content-Type'] = (
'text/tab-separated-values; charset=utf-8')
response.headers['Content-disposition'] = (
'attachment; filename="{name}.tsv"'.format(
name=resource_id))
wr = csv.writer(
response, encoding='utf-8', dialect=csv.excel_tab)
else:
abort(400,
_(u'format: must be one of %s') % u', '.join(DUMP_FORMATS))

if bom:
response.write(UTF8_BOM)
return wr

wr = None
while True:
Expand All @@ -54,15 +78,9 @@ def dump_csv(self, resource_id):
abort(404, _('DataStore resource not found'))

if not wr:
response.headers['Content-Type'] = 'text/csv; charset=utf-8'
response.headers['Content-disposition'] = (
'attachment; filename="{name}.csv"'.format(
name=resource_id))
wr = csv.writer(response, encoding='utf-8')
wr = start_writer()

header = [x['id'] for x in result['fields']]
if bom:
response.write(UTF8_BOM)
wr.writerow(header)

for record in result['records']:
Expand Down
2 changes: 1 addition & 1 deletion ckanext/datastore/plugin.py
Expand Up @@ -248,7 +248,7 @@ def get_auth_functions(self):
def before_map(self, m):
m.connect('/datastore/dump/{resource_id}',
controller='ckanext.datastore.controller:DatastoreController',
action='dump_csv')
action='dump')
return m

def before_show(self, resource_dict):
Expand Down
4 changes: 3 additions & 1 deletion doc/maintaining/datastore.rst
Expand Up @@ -280,7 +280,9 @@ Download resource as CSV

A DataStore resource can be downloaded in the `CSV`_ file format from ``{CKAN-URL}/datastore/dump/{RESOURCE-ID}``.

For an Excel-compatible CSV file use ``{CKAN-URL}/datastore/dump/{RESOURCE-ID}&bom=true``
For an Excel-compatible CSV file use ``{CKAN-URL}/datastore/dump/{RESOURCE-ID}?bom=true``.

For tab-separated values use ``{CKAN-URL}/datastore/dump/{RESOURCE-ID}?format=tsv``.

.. _CSV: https://en.wikipedia.org/wiki/Comma-separated_values

Expand Down

0 comments on commit 417f1ea

Please sign in to comment.