Skip to content

Commit ee5b5ed

Browse files
authored
IFC-1246 Add support for attribute rel in object templates (#5811)
1 parent ad22d9a commit ee5b5ed

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

backend/infrahub/core/node/__init__.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,26 @@ async def handle_object_template(self, fields: dict, db: InfrahubDatabase, error
305305

306306
# Handle attributes, copy values from template
307307
# Relationships handling in performed in GraphQL mutation to create nodes for relationships
308-
for attribute in template._attributes:
309-
if attribute in list(fields) + [OBJECT_TEMPLATE_NAME_ATTR]:
308+
for attribute_name in template._attributes:
309+
if attribute_name in list(fields) + [OBJECT_TEMPLATE_NAME_ATTR]:
310310
continue
311-
fields[attribute] = {"value": getattr(template, attribute).value}
311+
fields[attribute_name] = {"value": getattr(template, attribute_name).value}
312+
313+
for relationship_name in template._relationships:
314+
relationship_schema = template._schema.get_relationship(name=relationship_name)
315+
if (
316+
relationship_name in list(fields)
317+
or relationship_schema.kind != RelationshipKind.ATTRIBUTE
318+
or relationship_name == OBJECT_TEMPLATE_RELATIONSHIP_NAME
319+
):
320+
continue
321+
322+
relationship: RelationshipManager = getattr(template, relationship_name)
323+
if relationship_schema.cardinality == RelationshipCardinality.ONE:
324+
if relationship_peer := await relationship.get_peer(db=db):
325+
fields[relationship_name] = {"id": relationship_peer.id}
326+
elif relationship_peers := await relationship.get_peers(db=db):
327+
fields[relationship_name] = [{"id": peer_id} for peer_id in relationship_peers]
312328

313329
async def _process_fields(self, fields: dict, db: InfrahubDatabase) -> None:
314330
errors = []

backend/infrahub/core/schema/schema_branch.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,7 @@ def manage_object_template_relationships(self) -> None:
17921792
"name": "object_template",
17931793
"identifier": "node__objecttemplate",
17941794
"peer": self._get_object_template_kind(node.kind),
1795-
"kind": RelationshipKind.ATTRIBUTE,
1795+
"kind": RelationshipKind.TEMPLATE,
17961796
"cardinality": RelationshipCardinality.ONE,
17971797
"branch": BranchSupportType.AWARE,
17981798
"order_weight": 1,
@@ -1829,16 +1829,24 @@ def add_relationships_to_template(self, node: NodeSchema) -> None:
18291829
if relationship.peer in [
18301830
InfrahubKind.GENERICGROUP,
18311831
InfrahubKind.PROFILE,
1832-
] or relationship.kind not in [RelationshipKind.COMPONENT, RelationshipKind.PARENT]:
1832+
] or relationship.kind not in [
1833+
RelationshipKind.COMPONENT,
1834+
RelationshipKind.PARENT,
1835+
RelationshipKind.ATTRIBUTE,
1836+
]:
18331837
continue
18341838

1835-
rel_template_peer = self._get_object_template_kind(node_kind=relationship.peer)
1839+
rel_template_peer = (
1840+
self._get_object_template_kind(node_kind=relationship.peer)
1841+
if relationship.kind != RelationshipKind.ATTRIBUTE
1842+
else relationship.peer
1843+
)
18361844
template_schema.relationships.append(
18371845
RelationshipSchema(
18381846
name=relationship.name,
18391847
peer=rel_template_peer,
18401848
kind=relationship.kind,
1841-
optional=relationship.kind == RelationshipKind.COMPONENT,
1849+
optional=relationship.kind in [RelationshipKind.COMPONENT, RelationshipKind.ATTRIBUTE],
18421850
cardinality=relationship.cardinality,
18431851
branch=relationship.branch,
18441852
identifier=self._generate_identifier_string(template_schema.kind, rel_template_peer),

0 commit comments

Comments
 (0)