Skip to content

Commit

Permalink
Add JSON response to GET Audit Template submission (#4477)
Browse files Browse the repository at this point in the history
* add JSON response support to audit template submission GET

* isort

---------

Co-authored-by: Nicholas Long <[email protected]>
Co-authored-by: Alex Swindler <[email protected]>
  • Loading branch information
3 people authored Jan 16, 2024
1 parent 9777ea1 commit 418e9c0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
11 changes: 5 additions & 6 deletions seed/audit_template/audit_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 13 additions & 6 deletions seed/views/v3/audit_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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({
Expand All @@ -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')
Expand Down

0 comments on commit 418e9c0

Please sign in to comment.