Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/190.canada.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a `filename` option to the DataStore Dump endpoint.
29 changes: 25 additions & 4 deletions ckanext/datastore/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
json_writer,
xml_writer,
)
# (canada fork only): filename to save stream to
import re


# (canada fork only): filename to save stream to
FILENAME_MATCH = re.compile('^[\w\-\.]+$')

int_validator = get_validator(u'int_validator')
boolean_validator = get_validator(u'boolean_validator')
Expand Down Expand Up @@ -75,6 +81,18 @@ def exclude_id_from_ds_dump(key, data, errors, context):
data[key] = value


# (canada fork only): filename to save stream to
def filename_safe(key, data, errors, context):
"""
Makes sure the passed filename is safe to stream back in the response.
"""
value = data.get(key)

if not re.search(FILENAME_MATCH, value):
errors[key].append(_('Invalid characters in filename'))
raise StopOnError


def dump_schema() -> Schema:
return {
u'offset': [default(0), int_validator],
Expand All @@ -88,6 +106,7 @@ def dump_schema() -> Schema:
u'language': [ignore_missing, unicode_only],
u'fields': [exclude_id_from_ds_dump, ignore_missing, list_of_strings_or_string], # (canada fork only): exclude _id field from Blueprint dump
u'sort': [default(u'_id'), list_of_strings_or_string],
'filename': [ignore_missing, unicode_only, filename_safe] # (canada fork only): filename to save stream to
}


Expand All @@ -111,6 +130,8 @@ def dump(resource_id: str):
limit = data.get('limit')
options = {'bom': data['bom']}
sort = data['sort']
# (canada fork only): filename to save stream to
filename = data.get('filename', resource_id)
search_params = {
k: v
for k, v in data.items()
Expand All @@ -127,19 +148,19 @@ def dump(resource_id: str):

if fmt == 'csv':
content_disposition = 'attachment; filename="{name}.csv"'.format(
name=resource_id)
name=filename) # (canada fork only): filename to save stream to
content_type = b'text/csv; charset=utf-8'
elif fmt == 'tsv':
content_disposition = 'attachment; filename="{name}.tsv"'.format(
name=resource_id)
name=filename) # (canada fork only): filename to save stream to
content_type = b'text/tab-separated-values; charset=utf-8'
elif fmt == 'json':
content_disposition = 'attachment; filename="{name}.json"'.format(
name=resource_id)
name=filename) # (canada fork only): filename to save stream to
content_type = b'application/json; charset=utf-8'
elif fmt == 'xml':
content_disposition = 'attachment; filename="{name}.xml"'.format(
name=resource_id)
name=filename) # (canada fork only): filename to save stream to
content_type = b'text/xml; charset=utf-8'
else:
abort(404, _('Unsupported format'))
Expand Down