Skip to content

Commit

Permalink
Add METS download link to AIP page. (#46)
Browse files Browse the repository at this point in the history
Adds a METS download link to the AIP detail page.
  • Loading branch information
mcantelon committed Sep 8, 2023
1 parent a315d91 commit afd78ed
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
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
33 changes: 33 additions & 0 deletions AIPscan/Reporter/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest
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 MockRespoonse(object):
pass

return_response = MockRespoonse()
return_response.content = "METS content"
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.status_code == 200
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)

0 comments on commit afd78ed

Please sign in to comment.