diff --git a/seed/audit_template/audit_template.py b/seed/audit_template/audit_template.py index 80a97cb96e..b822640877 100644 --- a/seed/audit_template/audit_template.py +++ b/seed/audit_template/audit_template.py @@ -59,24 +59,23 @@ def get_submission(self, audit_template_submission_id: int, report_format: str = Args: audit_template_submission_id (int): value of the "Submission ID" as seen on Audit Template - report_format (str, optional): Report format, either `xml` or `pdf`. Defaults to 'pdf'. + report_format (str, optional): Report format, either `json`, `xml`, or `pdf`. Defaults to 'pdf'. Returns: requests.response: Result from Audit Template website """ - # supporting 'PDF' and 'XML' formats only for now + # supporting 'JSON', PDF', and 'XML' formats only for now token, message = self.get_api_token() if not token: return None, message # validate format - if report_format.lower() not in ['xml', 'pdf']: + if report_format.lower() not in ['json', 'xml', 'pdf']: report_format = 'pdf' # set headers - headers = {'accept': 'application/pdf'} - if report_format.lower() == 'xml': - headers = {'accept': 'application/xml'} + accept_type = 'application/' + report_format.lower() + headers = {'accept': accept_type} url = f'{self.API_URL}/rp/submissions/{audit_template_submission_id}.{report_format}?token={token}' try: diff --git a/seed/views/v3/audit_template.py b/seed/views/v3/audit_template.py index 1dfb398d10..20082ea92c 100644 --- a/seed/views/v3/audit_template.py +++ b/seed/views/v3/audit_template.py @@ -4,6 +4,8 @@ SEED Platform (TM), Copyright (c) Alliance for Sustainable Energy, LLC, and other contributors. See also https://github.com/seed-platform/seed/main/LICENSE.md """ +import json + from django.http import HttpResponse, JsonResponse from drf_yasg.utils import swagger_auto_schema from rest_framework import viewsets @@ -37,7 +39,7 @@ def get_submission(self, request, pk): default_report_format = 'pdf' report_format = request.query_params.get('report_format', default_report_format) - valid_file_formats = ['xml', 'pdf'] + valid_file_formats = ['json', 'xml', 'pdf'] if report_format.lower() not in valid_file_formats: message = f"The report_format specified is invalid. Must be one of: {valid_file_formats}." return JsonResponse({ @@ -49,18 +51,23 @@ def get_submission(self, request, pk): at = AuditTemplate(self.get_organization(self.request)) response, message = at.get_submission(pk, report_format) + # error if response is None: return JsonResponse({ 'success': False, 'message': message }, status=400) + # json + if report_format.lower() == 'json': + return JsonResponse(json.loads(response.content)) + # xml if report_format.lower() == 'xml': return HttpResponse(response.text) - else: - response2 = HttpResponse(response.content) - response2.headers["Content-Type"] = 'application/pdf' - response2.headers["Content-Disposition"] = f'attachment; filename="at_submission_{pk}.pdf"' - return response2 + # pdf + response2 = HttpResponse(response.content) + response2.headers["Content-Type"] = 'application/pdf' + response2.headers["Content-Disposition"] = f'attachment; filename="at_submission_{pk}.pdf"' + return response2 @swagger_auto_schema(manual_parameters=[AutoSchemaHelper.query_org_id_field()]) @has_perm_class('can_view_data')