helper recursice data fetch
This commit is contained in:
@@ -4,10 +4,15 @@ from re import T
|
|||||||
from bson.json_util import loads
|
from bson.json_util import loads
|
||||||
from mongoengine import *
|
from mongoengine import *
|
||||||
|
|
||||||
from .enumeration import BitumenCategoryEnum
|
from paveit.helper import fetch_recursive, mongo_to_dict
|
||||||
from .norm_specification import DeliveryGrain, EnumerateBase
|
|
||||||
from .norm_documents import NormDocumentAggregate, NormDocumentBitumen, NormDocumentAsphalt
|
|
||||||
|
|
||||||
|
from .enumeration import BitumenCategoryEnum
|
||||||
|
from .norm_documents import (
|
||||||
|
NormDocumentAggregate,
|
||||||
|
NormDocumentAsphalt,
|
||||||
|
NormDocumentBitumen,
|
||||||
|
)
|
||||||
|
from .norm_specification import DeliveryGrain, EnumerateBase
|
||||||
from .project import Project
|
from .project import Project
|
||||||
from .usermanagement import Organisation, User
|
from .usermanagement import Organisation, User
|
||||||
|
|
||||||
@@ -36,25 +41,11 @@ class Material(Document):
|
|||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
# convert data to dict
|
# convert data to dict
|
||||||
data = self.to_mongo().to_dict()
|
data = fetch_recursive(self)
|
||||||
|
data = mongo_to_dict(data)
|
||||||
# Laden und hinzufügen der referenzierten Daten
|
|
||||||
try:
|
|
||||||
data['parameter'] = self.parameter.to_mongo().to_dict()
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
|
||||||
norm = self.norm.fetch() # Trigger the dereferencing
|
|
||||||
data['norm'] = norm.to_mongo().to_dict()
|
|
||||||
except:
|
|
||||||
raise
|
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
meta = {
|
meta = {
|
||||||
'allow_inheritance': True,
|
'allow_inheritance': True,
|
||||||
'index_opts': {},
|
'index_opts': {},
|
||||||
|
|||||||
@@ -2,8 +2,18 @@ from mongoengine import *
|
|||||||
|
|
||||||
from .enumeration import RelationalOperatorsEnum, BitumenCategoryEnum
|
from .enumeration import RelationalOperatorsEnum, BitumenCategoryEnum
|
||||||
from .norm_documents import NormDocumentAggregate, NormDocumentBitumen, NormDocumentAsphalt
|
from .norm_documents import NormDocumentAggregate, NormDocumentBitumen, NormDocumentAsphalt
|
||||||
|
|
||||||
|
from paveit.helper import fetch_recursive, mongo_to_dict
|
||||||
|
|
||||||
class EnumerateBase(Document):
|
class EnumerateBase(Document):
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
# convert data to dict
|
||||||
|
data = fetch_recursive(self)
|
||||||
|
data = mongo_to_dict(data)
|
||||||
|
print(data)
|
||||||
|
return data
|
||||||
|
|
||||||
meta = {
|
meta = {
|
||||||
'allow_inheritance': True,
|
'allow_inheritance': True,
|
||||||
'index_opts': {},
|
'index_opts': {},
|
||||||
@@ -13,6 +23,8 @@ class EnumerateBase(Document):
|
|||||||
'collection': 'norm_specification',
|
'collection': 'norm_specification',
|
||||||
"db_alias": 'dblabtests',
|
"db_alias": 'dblabtests',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Gesteine
|
# Gesteine
|
||||||
class DeliveryGrain(EnumerateBase):
|
class DeliveryGrain(EnumerateBase):
|
||||||
@@ -21,10 +33,10 @@ class DeliveryGrain(EnumerateBase):
|
|||||||
norm = LazyReferenceField(NormDocumentAggregate, required=True)
|
norm = LazyReferenceField(NormDocumentAggregate, required=True)
|
||||||
|
|
||||||
# Bitumen
|
# Bitumen
|
||||||
|
class BitumenParameterLimitsBaseEnum(EnumerateBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class BitumenParameterLimitsStrassenbaubitumen(BitumenParameterLimitsBaseEnum):
|
||||||
|
|
||||||
class BitumenParameterLimitsStrassenbaubitumen(EnumerateBase):
|
|
||||||
|
|
||||||
name = StringField()
|
name = StringField()
|
||||||
category = EnumField(BitumenCategoryEnum, required=True)
|
category = EnumField(BitumenCategoryEnum, required=True)
|
||||||
@@ -73,10 +85,3 @@ class BitumenParameterLimitsStrassenbaubitumen(EnumerateBase):
|
|||||||
hardening_resistance_masschange_operator = EnumField(RelationalOperatorsEnum, default=RelationalOperatorsEnum.lt)
|
hardening_resistance_masschange_operator = EnumField(RelationalOperatorsEnum, default=RelationalOperatorsEnum.lt)
|
||||||
hardening_resistance_masschange_max = FloatField(min_value=0, max_value=100)
|
hardening_resistance_masschange_max = FloatField(min_value=0, max_value=100)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Asphalt
|
|
||||||
class Asphalt(EnumerateBase):
|
|
||||||
name = StringField()
|
|
||||||
category = StringField()
|
|
||||||
norm = LazyReferenceField(NormDocumentAsphalt, required=True)
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
from .filehandling import read_file_to_bytesio
|
from .filehandling import read_file_to_bytesio
|
||||||
from .filehasher import calc_hash_of_bytes
|
from .filehasher import calc_hash_of_bytes
|
||||||
from .minio import get_minio_client_archive, get_minio_client_processing
|
from .minio import get_minio_client_archive, get_minio_client_processing
|
||||||
from .mongo import connect_mongo_db, mongo_get_results
|
from .mongo import connect_mongo_db, fetch_recursive, mongo_get_results, mongo_to_dict
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'read_file_to_bytesio', 'connect_mongo_db', 'mongo_get_results',
|
'read_file_to_bytesio', 'connect_mongo_db', 'mongo_get_results', 'fetch_recursive', 'mongo_to_dict',
|
||||||
'get_minio_client_processing', 'get_minio_client_archive',
|
'get_minio_client_processing', 'get_minio_client_archive',
|
||||||
'calc_hash_of_bytes'
|
'calc_hash_of_bytes'
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
import mongoengine
|
||||||
from bson import ObjectId
|
from bson import ObjectId
|
||||||
from mongoengine import connect as mongo_connect
|
from mongoengine import connect as mongo_connect
|
||||||
from pandas import DataFrame
|
from pandas import DataFrame
|
||||||
import os
|
|
||||||
|
|
||||||
def connect_mongo_db(username=os.environ['MONGO_USER'] ,
|
def connect_mongo_db(username=os.environ['MONGO_USER'] ,
|
||||||
password=os.environ['MONGO_PASSWD'] ,
|
password=os.environ['MONGO_PASSWD'] ,
|
||||||
@@ -65,4 +68,47 @@ def mongo_get_results(resultsmodel, results: DataFrame, datamodel,
|
|||||||
project_id: ObjectId, material_id: ObjectId,
|
project_id: ObjectId, material_id: ObjectId,
|
||||||
user_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