From 6a15eba05c01553db6daebd5466bf9eea5af4f6b Mon Sep 17 00:00:00 2001 From: Vermaas <vermaas@astron.nl> Date: Fri, 22 Mar 2024 10:24:33 +0100 Subject: [PATCH] combine the summary construction functions into 1 endpoint --- .../templates/taskdatabase/index.html | 2 +- atdb/taskdatabase/urls.py | 5 +- atdb/taskdatabase/views.py | 96 +++++-------------- 3 files changed, 29 insertions(+), 74 deletions(-) diff --git a/atdb/taskdatabase/templates/taskdatabase/index.html b/atdb/taskdatabase/templates/taskdatabase/index.html index b982c513..a6347201 100644 --- a/atdb/taskdatabase/templates/taskdatabase/index.html +++ b/atdb/taskdatabase/templates/taskdatabase/index.html @@ -31,7 +31,7 @@ {% include 'taskdatabase/pagination.html' %} </div> </div> - <p class="footer"> Version 19 Mar 2024 + <p class="footer"> Version 22 Mar 2024 </div> {% include 'taskdatabase/refresh.html' %} diff --git a/atdb/taskdatabase/urls.py b/atdb/taskdatabase/urls.py index 26a4b3a9..03577e50 100644 --- a/atdb/taskdatabase/urls.py +++ b/atdb/taskdatabase/urls.py @@ -102,9 +102,8 @@ urlpatterns = [ name='get-unique-values-for-key-view'), # ancillary dataproducts endpoints (retrieved by archiver service for copy to LTA dcache). - path('get_summary_html/<sas_id>', views.GetSummaryHtml, name='get-summary-html'), - path('get_summary_json/<sas_id>', views.GetSummaryJson, name='get-summary-json'), - path('get_summary_pdf/<sas_id>', views.GetSummaryPdf, name='get-summary-pdf'), + # /atdb/get_summary/606942/html + path('get_summary/<sas_id>/<format>', views.GetSummary, name='get-summary'), # format can be 'json', 'html' or 'pdf' # --- controller resources --- path('tasks/<int:pk>/setstatus/<new_status>/<page>', views.TaskSetStatus, name='task-setstatus-view'), diff --git a/atdb/taskdatabase/views.py b/atdb/taskdatabase/views.py index 14c9ae55..295d0977 100644 --- a/atdb/taskdatabase/views.py +++ b/atdb/taskdatabase/views.py @@ -1704,13 +1704,14 @@ class GetUniqueValuesForKey(generics.ListAPIView): }) -def GetSummaryHtml(request, sas_id): + +def GetSummary(request, sas_id, format): """ - Construct and return a summary html structure for given sas_id + Construct and return a summary structure for given sas_id in the requested format (json, html or pdf) This is the same informtion and algorithm as used when the user clicks the SUM button on the Validation page. See documentation: https://drive.google.com/file/d/16R8L06OFiKHFHBUA6FhrNVZVAaQBC2tU/view?usp=sharing - example: /atdb/get_summary_html/606942 + example: /atdb/get_summary/606942/json """ try: @@ -1718,41 +1719,14 @@ def GetSummaryHtml(request, sas_id): queryset = Task.objects.filter(sas_id=sas_id) task = queryset[0] - head_html=""" - <head> - <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"> - <link href='https://fonts.googleapis.com/css?family=Raleway' rel='stylesheet' type='text/css'> - </head> - """ - - template = get_template("taskdatabase/validation/summary.html") - summary_html = algorithms.construct_summary(task) - context = {'task': task, 'my_summary': summary_html} - html = head_html + template.render(context) - - return HttpResponse(html) - - except Exception as error: - logger.error(error) - return JsonResponse({ - 'error': str(error) - }) - - -def GetSummaryPdf(request, sas_id): - """ - Construct and return a summary html structure for given sas_id - This is the same informtion and algorithm as used when the user clicks the SUM button on the Validation page. - See documentation: https://drive.google.com/file/d/16R8L06OFiKHFHBUA6FhrNVZVAaQBC2tU/view?usp=sharing - - example: /atdb/get_summary_pdf/606942 - """ - try: + if format == 'json': + summary_json = algorithms.construct_summary(task,format='json') - # use a trick to be able to use the existing task based code - queryset = Task.objects.filter(sas_id=sas_id) - task = queryset[0] + return JsonResponse({ + 'summary': summary_json + }) + # for both the formats 'html' and 'pdf' the html must be constructed first # add some basic layout without using the summary.html template head_html=""" <head> @@ -1766,42 +1740,25 @@ def GetSummaryPdf(request, sas_id): context = {'task': task, 'my_summary': summary_html} html = head_html + template.render(context) - # Create a BytesIO object to receive the PDF data - result = BytesIO() - - # Convert HTML to PDF - # TODO: fonts zijn fout, is daar nog iets aan te doen? - pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), result) - pdf_name = sas_id + '_summary.pdf' - if not pdf.err: - # Return the PDF as a response - response = HttpResponse(result.getvalue(), content_type='application/pdf') - response['Content-Disposition'] = f'attachment; filename={pdf_name}' - return response - - except: - return HttpResponse(f'Failed to generate PDF: {pdf.err}') - - -def GetSummaryJson(request, sas_id): - """ - Construct and return a summary json structure for given sas_id - This is the same informtion and algorithm as used when the user clicks the SUM button on the Validation page. - See documentation: https://drive.google.com/file/d/16R8L06OFiKHFHBUA6FhrNVZVAaQBC2tU/view?usp=sharing - - example: /atdb/get_summary_json/606942 - """ - try: + if format == 'html': + # for 'html' the operation is ready, return the html + return HttpResponse(html) - # use a trick to be able to use the existing task based code - queryset = Task.objects.filter(sas_id=sas_id) - task = queryset[0] - summary_json = algorithms.construct_summary(task,format='json') + if format == 'pdf': + # for pdf, convert the html to pdf - return JsonResponse({ - 'summary': summary_json - }) + # Create a BytesIO object to receive the PDF data + result = BytesIO() + # Convert HTML to PDF + # TODO: fonts are wrong, can that be fixed somehow? + pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), result) + pdf_name = sas_id + '_summary.pdf' + if not pdf.err: + # Return the PDF as a response + response = HttpResponse(result.getvalue(), content_type='application/pdf') + response['Content-Disposition'] = f'attachment; filename={pdf_name}' + return response except Exception as error: logger.error(error) @@ -1810,7 +1767,6 @@ def GetSummaryJson(request, sas_id): }) - @staff_member_required def AssociateActivities(request): # disconnect the signals to avoid unneccesary updates -- GitLab