helper recursice data fetch
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import os
|
||||
|
||||
import mongoengine
|
||||
from bson import ObjectId
|
||||
from mongoengine import connect as mongo_connect
|
||||
from pandas import DataFrame
|
||||
import os
|
||||
|
||||
|
||||
def connect_mongo_db(username=os.environ['MONGO_USER'] ,
|
||||
password=os.environ['MONGO_PASSWD'] ,
|
||||
@@ -65,4 +68,47 @@ def mongo_get_results(resultsmodel, results: DataFrame, datamodel,
|
||||
project_id: ObjectId, material_id: ObjectId,
|
||||
user_id: ObjectId):
|
||||
|
||||
return True
|
||||
return True
|
||||
|
||||
def fetch_recursive(data, fetch_parameter=['norm']):
|
||||
|
||||
fields = data._fields
|
||||
|
||||
data_out = data.to_mongo().to_dict()
|
||||
|
||||
for par in fetch_parameter:
|
||||
if par in fields.keys():
|
||||
|
||||
try:
|
||||
# if is LazyReferenceField
|
||||
if isinstance(fields[par], mongoengine.fields.LazyReferenceField):
|
||||
d = data[par].fetch()
|
||||
else:
|
||||
d = data[par]
|
||||
|
||||
except:
|
||||
continue
|
||||
|
||||
if d is None:
|
||||
continue
|
||||
|
||||
data_out[par] = d.to_mongo().to_dict()
|
||||
|
||||
return data_out
|
||||
|
||||
def mongo_to_dict(data, drop_parameters=['_cls','user_id', 'org_id', 'project_id']):
|
||||
'''
|
||||
data: dict
|
||||
|
||||
'''
|
||||
for key in list(data.keys()):
|
||||
if key in drop_parameters:
|
||||
del data[key] # Remove the unwanted key
|
||||
elif isinstance(data[key], dict):
|
||||
mongo_to_dict(data[key]) # Recurse into nested dictionaries
|
||||
else:
|
||||
# process data
|
||||
if isinstance(data[key], ObjectId):
|
||||
data[key] = str(data[key])
|
||||
|
||||
return data
|
||||
Reference in New Issue
Block a user