Skip to content

Commit

Permalink
Endpoint to download an Audit Template Report Submission and store in…
Browse files Browse the repository at this point in the history
… SEED (#36)

* start of AT report download method

* adding endpoint to download AT report submission and store in SEED
  • Loading branch information
kflemin authored Nov 22, 2023
1 parent 321ccfd commit 95fb89d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
65 changes: 65 additions & 0 deletions pyseed/seed_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,71 @@ def retrieve_at_building_and_update(self, audit_template_building_id: int, cycle

return response

def retrieve_at_submission_and_update(self, audit_template_submission_id: int, cycle_id: int, seed_id: int, report_format: str = 'pdf', filename: str = None) -> dict:
"""Connect to audit template and retrieve audit report by submission ID
Args:
audit_template_submission_id (int): ID of the AT submission report (different than building ID)
cycle_id (int): Cycle ID in SEED (needed for XML but not actually for PDF)
seed_id (int): PropertyView ID in SEED
file_format (str): pdf or xml report, defaults to pdf
filename (str): filename to use to upload to SEED
Returns:
dict: Response from the SEED API
including the PDF file (if that format was requested)
"""

# api/v3/audit_template/pk/get_submission
# accepts pdf or xml
response = self.client.get(
None,
required_pk=False,
endpoint="audit_template_submission",
url_args={"PK": audit_template_submission_id},
report_format=report_format
)

if response['status'] == 'success':
if report_format.lower() == 'pdf':
pdf_file = response['content']
if not filename:
filename = 'at_submission_report_' + str(audit_template_submission_id) + '.pdf'
files = [
('file', (filename, pdf_file)),
('file_type', (None, 1))
]
response2 = self.client.put(
None,
required_pk=False,
endpoint="properties_upload_inventory_document",
url_args={"PK": seed_id},
files=files
)
response2['pdf_report'] = pdf_file
else:
# assume XML
# now post to api/v3/properties/PK/update_with_buildingsync
xml_file = response['content']
if not filename:
filename = 'at_' + str(int(time.time() * 1000)) + '.xml'

files = [
('file', (filename, xml_file)),
('file_type', (None, 1))
]

response2 = self.client.put(
None,
required_pk=False,
endpoint="properties_update_with_buildingsync",
url_args={"PK": seed_id},
files=files,
cycle_id=cycle_id
)

return response2

def retrieve_portfolio_manager_property(self, username: str, password: str, pm_property_id: int, save_file_name: Path) -> dict:
"""Connect to portfolio manager and download an individual properties data in Excel format
Expand Down
6 changes: 6 additions & 0 deletions pyseed/seed_client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
# PUTs with replaceable keys:
'properties_update_with_buildingsync': 'api/v3/properties/PK/update_with_building_sync/',
'property_update_with_espm': 'api/v3/properties/PK/update_with_espm/',
'properties_upload_inventory_document': 'api/v3/properties/PK/upload_inventory_document',
# GETs with replaceable keys
'analyses_views': '/api/v3/analyses/PK/views/ANALYSIS_VIEW_PK/',
'import_files_matching_results': '/api/v3/import_files/PK/matching_and_geocoding_results/',
Expand All @@ -75,6 +76,7 @@
'properties_meter_usage': '/api/v3/properties/PK/meter_usage/',
'properties_analyses': '/api/v3/properties/PK/analyses/',
'audit_template_building_xml': '/api/v3/audit_template/PK/get_building_xml',
'audit_template_submission': '/api/v3/audit_template/PK/get_submission',
# GET & POST with replaceable keys
'properties_meters_reading': '/api/v3/properties/PK/meters/METER_PK/readings/',
}
Expand Down Expand Up @@ -217,6 +219,9 @@ def _check_response(self, response, *args, **kwargs):
elif 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' in response_content_types:
# spreadsheet response
error = False
elif 'application/pdf' in response_content_types:
# PDF report response
error = False
elif 'application/json' not in response_content_types:
# get as text
if not response.content:
Expand Down Expand Up @@ -481,6 +486,7 @@ def put(self, pk, endpoint=None, data_name=None, **kwargs):
data_name = _set_default(self, 'data_name', data_name, required=False)
url = add_pk(self.urls[endpoint], pk, required=kwargs.pop('required_pk', True), slash=True)
url = _replace_url_args(url, url_args)

response = super(UpdateMixin, self)._put(url=url, **kwargs)
self._check_response(response, **kwargs)
return self._get_result(response, data_name=data_name, **kwargs)
Expand Down

0 comments on commit 95fb89d

Please sign in to comment.