From a9af75bc3b8d153a8bb09d14a1261271a33504a4 Mon Sep 17 00:00:00 2001 From: Guille Date: Fri, 21 Jun 2024 07:50:09 +0200 Subject: [PATCH] Partial implementation of mongo logic --- hub_api/persistence/retrofit_results.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/hub_api/persistence/retrofit_results.py b/hub_api/persistence/retrofit_results.py index 60c3504..12a8835 100644 --- a/hub_api/persistence/retrofit_results.py +++ b/hub_api/persistence/retrofit_results.py @@ -1,6 +1,7 @@ import json import threading +from bson.json_util import dumps from co2_emission.co2_emission import Co2Emission from costs.cost import Cost from flask import Response, request @@ -118,22 +119,26 @@ class RetrofitResults(Resource, Config): 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 + def get(self): 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) + _session = {'token': 1} + results = {} if _session is None: return Response(json.dumps({'error': 'unauthorized'}), status=403) else: + client = MongoClient(self.mongodb) + montreal_retrofit_db = client[self.mongodb_database] + meb_collection = montreal_retrofit_db[self.mongodb_collection] 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) + buildings = request.get_json()['buildings'] + building_query = '' + for building in buildings: + building_query = f'{building_query} {{"alias": "{building}"}},' + query = f'{{"$or": [{building_query[:-1]}]}}' + results = dumps(meb_collection.find(json.loads(query))) + return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)