Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/viur/core/bones/relational.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ class RelationalBone(BaseBone):
relational updates this will cascade. If Entity A references B with CascadeDeletion set, and
B references C also with CascadeDeletion; if C gets deleted, both B and A will be deleted as well.

:param consistency_check_fn:
Allows to define a callable that checks whether an existing target skeleton should be handled with a
different consistency than configured as `consistency`.
"""
type = "relational"
kind = None
Expand All @@ -157,6 +160,7 @@ def __init__(
self,
*,
consistency: RelationalConsistency = RelationalConsistency.Ignore,
consistency_check_fn: t.Optional[t.Callable[["SkeletonInstance"], RelationalConsistency]] = None,
format: str = "$(dest.name)",
kind: str = None,
module: t.Optional[str] = None,
Expand Down Expand Up @@ -283,6 +287,7 @@ def __init__(

self.updateLevel = updateLevel
self.consistency = consistency
self.consistency_check_fn = consistency_check_fn

if getSystemInitialized():
from viur.core.skeleton import RefSkel, SkeletonInstance
Expand Down Expand Up @@ -1026,10 +1031,14 @@ def refresh(self, skel: "SkeletonInstance", name: str) -> None:
if value and value["dest"]:
try:
target_skel = value["dest"].read()
found = not bool(self.consistency_check_fn) # Trigger target_skel as deleted by consistency_check_fn?
except ValueError:
target_skel = None
found = False

if not found:
# Handle removed reference according to the RelationalConsistency settings
match self.consistency:
match self.consistency_check_fn(target_skel) if self.consistency_check_fn else self.consistency:
case RelationalConsistency.CascadeDeletion:
logging.info(
f"{name}: "
Expand All @@ -1046,15 +1055,16 @@ def refresh(self, skel: "SkeletonInstance", name: str) -> None:
f"due removal of {value["dest"]["key"]!r} ({value["dest"]["name"]!r})"
)
value.clear()
continue

case _:
case _ if target_skel is None:
logging.info(
f"{name}: "
f"Relation from {skel["key"]!r} ({skel["name"]!r}) "
f"refers to deleted {value["dest"]["key"]!r} ({value["dest"]["name"]!r}), skipping"
)

continue
continue

# Copy over the refKey values
for key in self.refKeys:
Expand Down
Loading