From d66ad362c73f7072812301e0725c274f0a34618e Mon Sep 17 00:00:00 2001 From: Guille Date: Fri, 3 May 2024 06:38:15 +0200 Subject: [PATCH] remove debug session and return file as response --- hub_api/config.py | 1 - hub_api/workflow/energy_plus.py | 16 +++++++--------- hub_api/workflow/insel_montly_energy_balance.py | 3 +-- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/hub_api/config.py b/hub_api/config.py index 809567c..3cb6a00 100644 --- a/hub_api/config.py +++ b/hub_api/config.py @@ -23,7 +23,6 @@ class Config: environment = 'PROD' database_name = 'montreal_retrofit' - self._base_uri = 'https://nextgenerations-cities.encs.concordia.ca/data/files/response_files' self._database = DBControl(db_name=database_name, app_env=environment, dotenv_path=dotenv_path) self._repository = Repository(db_name=database_name, app_env=environment, dotenv_path=dotenv_path) self._energy_systems_catalog = EnergySystemsCatalogFactory('montreal_custom').catalog diff --git a/hub_api/workflow/energy_plus.py b/hub_api/workflow/energy_plus.py index 37ad821..a85f11d 100644 --- a/hub_api/workflow/energy_plus.py +++ b/hub_api/workflow/energy_plus.py @@ -4,7 +4,7 @@ import shutil import zipfile from pathlib import Path -from flask import Response, request, send_file +from flask import Response, request, send_file, make_response from flask_restful import Resource from hub.exports.energy_building_exports_factory import EnergyBuildingsExportsFactory from hub.imports.construction_factory import ConstructionFactory @@ -67,11 +67,10 @@ class EnergyPlus(Resource, Config): token = request.headers.get('token', None) application_uuid = request.headers.get('application-uuid', None) _session = refresh_session(session_id, token, application_uuid) - _session = {'token': token} - response_token = {'token': _session['token']} if _session is None: return Response(json.dumps({'error': 'unauthorized'}), status=403) else: + response_token = {'token': _session['token']} tmp_path = (self._tmp_path / token).resolve() response_path = (self._response_path / session_id).resolve() try: @@ -109,11 +108,10 @@ class EnergyPlus(Resource, Config): str((tmp_path / f'{self._city.name}.idf').resolve()), ] result_zip = (response_path / f'{token}.zip').resolve() - with zipfile.ZipFile(result_zip, 'w') as zip: + with zipfile.ZipFile(result_zip, 'w') as zf: for result_file in result_files: - zip.write(result_file, Path(result_file).name) + zf.write(result_file, Path(result_file).name) shutil.rmtree(tmp_path) - return Response(json.dumps({ - 'result': 'succeed', - 'file': f'{self.base_uri}/{session_id}/{token}.zip' - }), status=200, headers=response_token) + response = make_response(send_file(result_zip)) + response.headers['token'] = token + return response diff --git a/hub_api/workflow/insel_montly_energy_balance.py b/hub_api/workflow/insel_montly_energy_balance.py index 45bc53b..2efd1da 100644 --- a/hub_api/workflow/insel_montly_energy_balance.py +++ b/hub_api/workflow/insel_montly_energy_balance.py @@ -69,11 +69,10 @@ class InselMonthlyEnergyBalance(Resource, Config): token = request.headers.get('token', None) application_uuid = request.headers.get('application-uuid', None) _session = refresh_session(session_id, token, application_uuid) - _session = {'token': token} - response_token = {'token': _session['token']} if _session is None: return Response(json.dumps({'error': 'unauthorized'}), status=403) else: + response_token = {'token': _session['token']} tmp_path = (self._tmp_path / token).resolve() try: os.mkdir(tmp_path)