From c9b889eb9cea011017b99bb21af54d25dc81e1b1 Mon Sep 17 00:00:00 2001 From: andrew Date: Mon, 11 Dec 2023 13:29:43 -0500 Subject: [PATCH] Add PGN Archive download buttons, and API endpoint --- OpenBench/urls.py | 1 + OpenBench/views.py | 25 ++++++++++++++++++++++++- Templates/OpenBench/test.html | 9 +++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/OpenBench/urls.py b/OpenBench/urls.py index 4a0d1fd..621b4f0 100644 --- a/OpenBench/urls.py +++ b/OpenBench/urls.py @@ -87,6 +87,7 @@ urlpatterns = [ django.urls.path(r'api/networks//', OpenBench.views.api_networks), django.urls.path(r'api/networks///', OpenBench.views.api_network_download), django.urls.path(r'api/buildinfo/', OpenBench.views.api_build_info), + django.urls.path(r'api/pgns//', OpenBench.views.api_pgns), # Redirect anything else to the Index django.urls.path(r'', OpenBench.views.index), diff --git a/OpenBench/views.py b/OpenBench/views.py index d2fe4f0..fa2c94e 100644 --- a/OpenBench/views.py +++ b/OpenBench/views.py @@ -41,12 +41,14 @@ from OpenSite.settings import MEDIA_ROOT from django.db import transaction from django.db.models import F, Q -from django.http import HttpResponse, JsonResponse +from django.http import HttpResponse, JsonResponse, FileResponse from django.views.decorators.csrf import csrf_exempt from django.core.files.storage import FileSystemStorage from django.core.files.base import ContentFile from django.utils import timezone +from wsgiref.util import FileWrapper + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # GENERAL UTILITIES # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # @@ -889,6 +891,27 @@ def api_build_info(request): return api_response(data) +@csrf_exempt +def api_pgns(request, pgn_id): + + if not api_authenticate(request): + return api_response({ 'error' : 'API requires authentication for this server' }) + + # Possible to request a PGN that does not exist + pgn_path = FileSystemStorage('Media/PGNs').path('%d.pgn.tar' % (pgn_id)) + if not os.path.exists(pgn_path): + return api_response({ 'error' : 'Unable to find PGN for Workload #%d' % (pgn_id) }) + + # Craft the download HTML response + fwrapper = FileWrapper(open(pgn_path, 'rb'), 8192) + response = FileResponse(fwrapper, content_type='application/octet-stream') + + # Set all headers and return response + response['Expires'] = -1 + response['Content-Length'] = os.path.getsize(pgn_path) + response['Content-Disposition'] = 'attachment; filename=%d.pgn.tar' % (pgn_id) + return response + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # BUSINESS VIEWS # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # diff --git a/Templates/OpenBench/test.html b/Templates/OpenBench/test.html index 05d947b..d33506d 100644 --- a/Templates/OpenBench/test.html +++ b/Templates/OpenBench/test.html @@ -105,6 +105,15 @@ + {% if test.upload_pgns != 'FALSE' %} + + {% endif %} +
{{test|longStatBlock|linebreaksbr}}