Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add METS download link to AIP page. (#46) #207

Merged
merged 1 commit into from
Sep 8, 2023
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
2 changes: 2 additions & 0 deletions AIPscan/Reporter/templates/aip.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
Originals: {{ original_file_count }}
<br>
Preservation copies: {{ preservation_file_count }}
<br>
<a href="{{ url_for('reporter.download_mets', aip_id=aip.id) }}">Download METS</a>
</div>

<h3>Original files</h3>
Expand Down
34 changes: 34 additions & 0 deletions AIPscan/Reporter/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from flask import current_app
from werkzeug.datastructures import Headers


def test_download_mets(app_with_populated_files, mocker):
# Mock storage server API request response
request = mocker.patch("requests.get")

class MockResponse(object):
pass

return_response = MockResponse()
return_response.content = bytes("METS content", "utf-8")
return_response.status_code = 200

request.return_value = return_response

# Test view logic
with current_app.test_client() as test_client:
response = test_client.get("/reporter/download_mets/1")

assert response.headers == Headers(
[
(
"Content-Disposition",
'attachment; filename="METS-111111111111-1111-1111-11111111.xml"',
),
("Content-Length", "12"),
("Content-Type", "text/html; charset=utf-8"),
]
)

assert response.data == return_response.content
assert response.status_code == return_response.status_code
30 changes: 29 additions & 1 deletion AIPscan/Reporter/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

from datetime import datetime

from flask import jsonify, make_response, render_template, request, session
import requests
from flask import Response, jsonify, make_response, render_template, request, session

from AIPscan.Aggregator.task_helpers import get_mets_url
from AIPscan.models import (
AIP,
Event,
Expand Down Expand Up @@ -272,3 +274,29 @@ def update_dates():
req = request.get_json()
session["end_date"] = request.json.get("end_date")
return make_response(jsonify(req), 200)


@reporter.route("/download_mets/<aip_id>", methods=["GET"])
def download_mets(aip_id):
aip = AIP.query.get(aip_id)
storage_service = StorageService.query.get(aip.storage_service_id)

api_url = {
"baseUrl": storage_service.url,
"userName": storage_service.user_name,
"apiKey": storage_service.api_key,
}

mets_response = requests.get(
get_mets_url(
api_url,
aip.uuid,
f"{aip.transfer_name}-{aip.uuid}/data/METS.{aip.uuid}.xml",
)
)

headers = {
"Content-Disposition": f'attachment; filename="METS-{aip.uuid}.xml"',
"Content-length": len(mets_response.content),
}
return Response(mets_response.content, mets_response.status_code, headers)