Skip to content

Commit

Permalink
utils: add "download" parameter to UI view
Browse files Browse the repository at this point in the history
* Adds a "download" querystring parameter to the file_download_ui view,
  which allows the file to be sent as an attachment.
  • Loading branch information
slint committed May 29, 2018
1 parent cae10d7 commit 508f938
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion invenio_records_files/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

from __future__ import absolute_import, print_function

from flask import abort
from flask import abort, request
from invenio_files_rest.models import ObjectVersion
from invenio_files_rest.views import ObjectResource
from invenio_records.errors import MissingModelError
Expand Down Expand Up @@ -83,6 +83,9 @@ def file_download_ui(pid, record, _record_file_factory=None, **kwargs):
)
)
If ``download`` is passed as a querystring argument, the file is sent as an
attachment.
:param pid: The :class:`invenio_pidstore.models.PersistentIdentifier`
instance.
:param record: The record metadata.
Expand Down Expand Up @@ -110,4 +113,5 @@ def file_download_ui(pid, record, _record_file_factory=None, **kwargs):
'pid_type': pid.pid_type,
'pid_value': pid.pid_value,
},
as_attachment=('download' in request.args)
)
13 changes: 13 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,21 @@ def test_file_download_ui(app, db, location, record_with_bucket, generic_file):
assert response.status_code == 200
assert response.mimetype == 'text/plain'

response = file_download_ui(
pid, record_with_bucket, filename=generic_file
)

with pytest.raises(NotFound):
file_download_ui(pid, record_with_bucket)

with pytest.raises(NotFound):
file_download_ui(pid, record_with_bucket, filename='not_found')

with app.test_request_context('/?download'):
response = file_download_ui(
pid, record_with_bucket, filename=generic_file
)
assert response.status_code == 200
assert response.mimetype == 'text/plain'
assert response.headers['Content-Disposition'] == \
'attachment; filename={}'.format(generic_file)

0 comments on commit 508f938

Please sign in to comment.