Skip to content

Commit

Permalink
[#3390] datastore: dump CSV for Excel with &bom=true
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Jan 6, 2017
1 parent 97da20e commit 49817be
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
8 changes: 7 additions & 1 deletion ckanext/datastore/controller.py
Expand Up @@ -18,12 +18,15 @@
)

int_validator = get_validator('int_validator')
boolean_validator = get_validator('boolean_validator')

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

PAGINATE_BY = 10000


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

wr = None
while True:
Expand All @@ -57,6 +61,8 @@ def dump(self, resource_id):
wr = csv.writer(response, encoding='utf-8')

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')
action='dump_csv')
return m

def before_show(self, resource_dict):
Expand Down
2 changes: 2 additions & 0 deletions doc/maintaining/datastore.rst
Expand Up @@ -280,6 +280,8 @@ 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``

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


Expand Down

0 comments on commit 49817be

Please sign in to comment.