Skip to content

Commit

Permalink
Merge branch 'master' of github.com:DataShades/ckanext-collection
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Jan 24, 2024
2 parents 41fec28 + 2d99daa commit e1bf089
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
23 changes: 17 additions & 6 deletions ckanext/collection/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ckan import types
from ckan.common import streaming_response
from ckan.logic import parse_params
from werkzeug.utils import secure_filename

from ckanext.collection import config, shared

Expand All @@ -29,8 +30,9 @@ def render(name: str) -> str | bytes:
return collection.serializer.render()


@bp.route("/api/util/collection/<name>/export")
@bp.route("/api/util/collection/<name>/export/<format>")
def export(name: str, format: str) -> types.Response:
def export(name: str, format: str | None = None) -> types.Response:
try:
tk.check_access("collection_view_export", {}, {"name": name})
except tk.NotAuthorized:
Expand All @@ -39,13 +41,22 @@ def export(name: str, format: str) -> types.Response:
params = parse_params(tk.request.args)

collection = shared.get_collection(name, params)
serializer_factory = config.serializer(format)

if not collection or not serializer_factory:
if not collection:
return tk.abort(404)

serializer = serializer_factory(collection)
if format:
serializer_factory = config.serializer(format)
if not serializer_factory:
return tk.abort(404)
serializer = serializer_factory(collection)
filename = f"collection.{format}"

else:
serializer = collection.serializer
filename = "collection"

filename = secure_filename(tk.request.args.get("filename", filename))

resp = streaming_response(serializer.stream(), with_context=True)
resp.headers["Content-Disposition"] = f"attachment; filename=collection.{format}"
resp.headers["Content-Disposition"] = f'attachment; filename="{filename}"'
return resp
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = ckanext-collection
version = 0.1.0a8
version = 0.1.0a10
description =
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down

0 comments on commit e1bf089

Please sign in to comment.