Skip to content

Commit

Permalink
fix: Tree.add_or_edit() should require for parententry (#1410)
Browse files Browse the repository at this point in the history
  • Loading branch information
phorward authored Feb 11, 2025
1 parent b2ff645 commit 30c41fa
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/viur/core/prototypes/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@


class TreeSkel(Skeleton):
parententry = KeyBone(
parententry = KeyBone( # TODO VIUR4: Why is this not a RelationalBone?
descr="Parent",
visible=False,
readOnly=True,
)
parentrepo = KeyBone(

parentrepo = KeyBone( # TODO VIUR4: Why is this not a RelationalBone?
descr="BaseRepo",
visible=False,
readOnly=True,
)

sortindex = SortIndexBone(
visible=False,
readOnly=True,
Expand Down Expand Up @@ -432,6 +434,8 @@ def add(self, skelType: SkelType, node: db.Key | int | str, *args, **kwargs) ->

skel = self.addSkel(skelType)
parentNodeSkel = self.editSkel("node")

# TODO VIUR4: Why is this parameter called "node"?
if not parentNodeSkel.read(node):
raise errors.NotFound("The provided parent node could not be found.")
if not self.canAdd(skelType, parentNodeSkel):
Expand Down Expand Up @@ -478,6 +482,10 @@ def add_or_edit(self, skelType: SkelType, key: db.Key | int | str, **kwargs) ->
else:
skel = self.editSkel(skelType)

skel = skel.ensure_is_cloned()
skel.parententry.required = True
skel.parententry.readOnly = False

skel["key"] = db_key

if (
Expand All @@ -487,6 +495,17 @@ def add_or_edit(self, skelType: SkelType, key: db.Key | int | str, **kwargs) ->
# render the skeleton in the version it could as far as it could be read.
return self.render.render("add_or_edit", skel)

# Ensure the parententry exists
parentNodeSkel = self.editSkel("node")
if not parentNodeSkel.read(skel["parententry"]):
raise errors.NotFound("The provided parent node could not be found.")
if not self.canAdd(skelType, parentNodeSkel):
raise errors.Unauthorized()

skel["parententry"] = parentNodeSkel["key"]
# parentrepo may not exist in parentNodeSkel as it may be an rootNode
skel["parentrepo"] = parentNodeSkel["parentrepo"] or parentNodeSkel["key"]

if is_add:
self.onAdd(skelType, skel)
else:
Expand Down

0 comments on commit 30c41fa

Please sign in to comment.