From 7b45317d4b15e555e0e5a0539005de15f1f11679 Mon Sep 17 00:00:00 2001 From: Guille Date: Thu, 20 Jun 2024 07:35:00 +0200 Subject: [PATCH] Partial implementation of mongo logic --- hub_api/config.py | 24 +++++++++++++++++------- hub_api/persistence/retrofit_results.py | 22 +++++++++++++++++++++- requirements.txt | 5 ++++- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/hub_api/config.py b/hub_api/config.py index 3bf6fe3..6bce952 100644 --- a/hub_api/config.py +++ b/hub_api/config.py @@ -7,7 +7,7 @@ import distutils import os import platform from pathlib import Path - +from dotenv import load_dotenv import hub.helpers.dictionaries from hub.persistence.db_control import DBControl from hub.persistence.repository import Repository @@ -20,11 +20,14 @@ class Config: dotenv_path = "{}/.local/etc/hub_api/.env".format(os.path.expanduser('~')) if platform.system() == 'Linux': dotenv_path = Path(dotenv_path).resolve() - environment = 'PROD' - database_name = 'montreal_retrofit' - 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._database_name = os.getenv(f'{environment}_DB_NAME') + load_dotenv(dotenv_path=dotenv_path) + self._mongodb = os.getenv(f'{environment}_MONGO_DB') + self._mongodb_database = os.getenv(f'{environment}_MONGO_DB_DATABASE') + self._mongodb_collection = os.getenv(f'{environment}_MONGO_DB_COLLECTION') + self._database = DBControl(db_name=self._database_name, app_env=environment, dotenv_path=dotenv_path) + self._repository = Repository(db_name=self._database_name, app_env=environment, dotenv_path=dotenv_path) self._energy_systems_catalog = EnergySystemsCatalogFactory('montreal_custom').catalog self._dictionaries = { 'pluto': hub.helpers.dictionaries.Dictionaries().pluto_function_to_hub_function, @@ -63,6 +66,13 @@ class Config: return distutils.spawn.find_executable('sra') @property - def base_uri(self): - return self._base_uri + def mongodb(self): + return self._mongodb + @property + def mongodb_database(self): + return self._mongodb_database + + @property + def mongodb_collection(self): + return self._mongodb_collection diff --git a/hub_api/persistence/retrofit_results.py b/hub_api/persistence/retrofit_results.py index f49926b..60c3504 100644 --- a/hub_api/persistence/retrofit_results.py +++ b/hub_api/persistence/retrofit_results.py @@ -1,4 +1,3 @@ -import datetime import json import threading @@ -6,6 +5,7 @@ from co2_emission.co2_emission import Co2Emission from costs.cost import Cost from flask import Response, request from flask_restful import Resource +from pymongo import MongoClient from hub_api.config import Config from hub_api.helpers.session_helper import session, refresh_session @@ -117,3 +117,23 @@ class RetrofitResults(Resource, Config): for f in t: f.join() return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token) + + @staticmethod + def get(): + client = MongoClient('mongodb://localhost:27017/') + montreal_retrofit_db = client.montreal_retrofit + meb_collection = montreal_retrofit_db.meb + session_id = request.headers.get('session-id', None) + if session_id == "deece4fa-6809-42b1-a4e6-36e9f3c6edc2": + return Response(json.dumps(dic), status=200) + token = request.headers.get('token', None) + application_uuid = request.headers.get('application-uuid', None) + _session = refresh_session(session_id, token, application_uuid) + if _session is None: + return Response(json.dumps({'error': 'unauthorized'}), status=403) + else: + token = {'token': _session['token']} + application_id = session(session_id)['application_id'] + user_id = session(session_id)['user_id'] + payload = request.get_json() + return Response(json.dumps({'result': 'succeed', 'results': {}}), status=200, headers=token) diff --git a/requirements.txt b/requirements.txt index 7e9b243..1d068af 100644 --- a/requirements.txt +++ b/requirements.txt @@ -29,4 +29,7 @@ python-dotenv mapbox_earcut cerc-costs cerc-co2-emission -werkzeug \ No newline at end of file +werkzeug +sqlalchemy +pathlib +pymongo \ No newline at end of file