Skip to content

Commit 565d782

Browse files
committed
Refactor get_CREs
1 parent 9f4f9a7 commit 565d782

File tree

2 files changed

+262
-311
lines changed

2 files changed

+262
-311
lines changed

application/database/db.py

Lines changed: 41 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,12 @@ def to_cre_def(self, node, parse_links=True):
201201
def get_links(self, links_dict):
202202
links = []
203203
for key in links_dict:
204-
links.extend([cre_defs.Link(c.to_cre_def(c, parse_links=False), key) for c in links_dict[key]])
204+
links.extend(
205+
[
206+
cre_defs.Link(c.to_cre_def(c, parse_links=False), key)
207+
for c in links_dict[key]
208+
]
209+
)
205210
return links
206211

207212

@@ -265,7 +270,7 @@ def to_cre_def(self, node, parse_links=True) -> cre_defs.Code:
265270

266271
class NeoCRE(NeoDocument): # type: ignore
267272
external_id = StringProperty()
268-
contained_in = RelationshipFrom('NeoCRE', 'CONTAINS', model=ContainsRel)
273+
contained_in = RelationshipFrom("NeoCRE", "CONTAINS", model=ContainsRel)
269274
contains = RelationshipTo("NeoCRE", "CONTAINS", model=ContainsRel)
270275
linked = RelationshipTo("NeoStandard", "LINKED_TO", model=LinkedToRel)
271276
same_as = RelationshipTo("NeoStandard", "SAME", model=SameRel)
@@ -277,12 +282,16 @@ def to_cre_def(self, node, parse_links=True) -> cre_defs.CRE:
277282
id=node.external_id,
278283
description=node.description,
279284
tags=node.tags,
280-
links=self.get_links({
281-
'Contains': [*node.contains, *node.contained_in],
282-
'Linked To': node.linked,
283-
'Same as': node.same_as,
284-
'Related': node.related
285-
}) if parse_links else []
285+
links=self.get_links(
286+
{
287+
"Contains": [*node.contains, *node.contained_in],
288+
"Linked To": node.linked,
289+
"Same as": node.same_as,
290+
"Related": node.related,
291+
}
292+
)
293+
if parse_links
294+
else [],
286295
)
287296

288297

@@ -343,7 +352,7 @@ def add_cre(self, dbcre: CRE):
343352
"description": dbcre.description,
344353
"links": [], # dbcre.links,
345354
"tags": [dbcre.tags] if isinstance(dbcre.tags, str) else dbcre.tags,
346-
"external_id": dbcre.external_id
355+
"external_id": dbcre.external_id,
347356
}
348357
)
349358

@@ -503,7 +512,7 @@ def standards(self) -> List[str]:
503512
@staticmethod
504513
def parse_node(node: NeoDocument) -> cre_defs.Document:
505514
return node.to_cre_def(node)
506-
515+
507516
@staticmethod
508517
def parse_node_no_links(node: NeoDocument) -> cre_defs.Document:
509518
return node.to_cre_def(node, parse_links=False)
@@ -1015,83 +1024,30 @@ def get_CREs(
10151024
"You need to search by external_id, internal_id name or description"
10161025
)
10171026
return []
1018-
if external_id:
1019-
if not partial:
1020-
return [NEO_DB.parse_node(NeoCRE.nodes.get(external_id=external_id))]
1021-
# query = query.filter(CRE.external_id == external_id)
1022-
else:
1023-
query = query.filter(CRE.external_id.like(external_id))
1024-
if name:
1025-
if not partial:
1026-
query = query.filter(func.lower(CRE.name) == name.lower())
1027-
else:
1028-
query = query.filter(func.lower(CRE.name).like(name.lower()))
1029-
if description:
1030-
if not partial:
1031-
query = query.filter(func.lower(CRE.description) == description.lower())
1032-
else:
1033-
query = query.filter(
1034-
func.lower(CRE.description).like(description.lower())
1035-
)
1036-
if internal_id:
1037-
query = CRE.query.filter(CRE.id == internal_id)
1038-
1039-
dbcres = query.all()
1040-
if not dbcres:
1041-
logger.warning(
1042-
"CRE %s:%s:%s does not exist in the db"
1043-
% (external_id, name, description)
1027+
params = dict(
1028+
external_id=external_id,
1029+
name=name,
1030+
description=description,
1031+
id=internal_id,
1032+
)
1033+
if partial:
1034+
params = dict(
1035+
external_id__icontains=external_id,
1036+
name__icontains=name,
1037+
description__icontains=description,
1038+
id__icontains=internal_id,
10441039
)
1045-
return []
10461040

1047-
# todo figure a way to return both the Node
1048-
# and the link_type for that link
1049-
for dbcre in dbcres:
1050-
cre = CREfromDB(dbcre)
1051-
linked_nodes = self.session.query(Links).filter(Links.cre == dbcre.id).all()
1052-
for ls in linked_nodes:
1053-
nd = self.session.query(Node).filter(Node.id == ls.node).first()
1054-
if not include_only or (include_only and nd.name in include_only):
1055-
cre.add_link(
1056-
cre_defs.Link(
1057-
document=nodeFromDB(nd),
1058-
ltype=cre_defs.LinkTypes.from_str(ls.type),
1059-
)
1060-
)
1061-
# todo figure the query to merge the following two
1062-
internal_links = (
1063-
self.session.query(InternalLinks)
1064-
.filter(
1065-
sqla.or_(
1066-
InternalLinks.cre == dbcre.id, InternalLinks.group == dbcre.id
1067-
)
1068-
)
1069-
.all()
1070-
)
1071-
for il in internal_links:
1072-
q = self.session.query(CRE)
1073-
1074-
res: CRE
1075-
ltype = cre_defs.LinkTypes.from_str(il.type)
1076-
1077-
if il.cre == dbcre.id: # if we are a CRE in this relationship
1078-
res = q.filter(
1079-
CRE.id == il.group
1080-
).first() # get the group in order to add the link
1081-
# if this CRE is the lower level cre the relationship will be tagged "Contains"
1082-
# in that case the implicit relationship is "Is Part Of"
1083-
# otherwise the relationship will be "Related" and we don't need to do anything
1084-
if ltype == cre_defs.LinkTypes.Contains:
1085-
# important, this is the only implicit link we have for now
1086-
ltype = cre_defs.LinkTypes.PartOf
1087-
elif ltype == cre_defs.LinkTypes.PartOf:
1088-
ltype = cre_defs.LinkTypes.Contains
1089-
elif il.group == dbcre.id:
1090-
res = q.filter(CRE.id == il.cre).first()
1091-
ltype = cre_defs.LinkTypes.from_str(il.type)
1092-
cre.add_link(cre_defs.Link(document=CREfromDB(res), ltype=ltype))
1093-
cres.append(cre)
1094-
return cres
1041+
params_filtered = {k: v for k, v in params.items() if v is not None}
1042+
parsed_response = [
1043+
NEO_DB.parse_node(x) for x in NeoCRE.nodes.filter(**params_filtered)
1044+
]
1045+
if include_only:
1046+
for node in parsed_response:
1047+
node.links = [
1048+
link for link in node.links if link.document.name in include_only
1049+
]
1050+
return parsed_response
10951051

10961052
def export(self, dir: str = None, dry_run: bool = False) -> List[cre_defs.Document]:
10971053
"""Exports the database to a CRE file collection on disk"""

0 commit comments

Comments
 (0)