Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Evaluate x-viur-bonelist on default viewSkel() #1384

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/viur/core/prototypes/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def viewSkel(self, *args, **kwargs) -> SkeletonInstance:
:return: Returns a Skeleton instance for viewing an entry.
"""
return self.baseSkel(*args, **kwargs)
return self.skel(bones_from_request=True, **kwargs)
Copy link
Member

@sveneberth sveneberth Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • *args get lost
  • bones_from_request cannot be controlled by calling viewSkel()? --> Please add in viewSkels signature and pass it.
        def viewSkel(self, *args, bones_from_request : bool=True, **kwargs) -> SkeletonInstance:
Suggested change
return self.skel(bones_from_request=True, **kwargs)
return self.skel(*args, **kwargs)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • *args get lost

I want to get rid of this. It is a relic from days gone by and should be eliminated. To do it, we have to refactor all *Skel()-related stuff in prototypes, and it will also become a breaking change. Therefore, just throw it away for now here.

  • bones_from_request cannot be controlled by calling viewSkel()? --> Please add in viewSkels signature and pass it.

We can do it this, do you want it to overwrite this as a default behavior?


def addSkel(self, *args, **kwargs) -> SkeletonInstance:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/viur/core/prototypes/singleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def viewSkel(self, *args, **kwargs) -> SkeletonInstance:

:return: Returns a Skeleton instance for viewing the singleton entry.
"""
return self.baseSkel(*args, **kwargs)
return self.skel(bones_from_request=True, **kwargs)

def editSkel(self, *args, **kwargs) -> SkeletonInstance:
"""
Expand Down
40 changes: 39 additions & 1 deletion src/viur/core/prototypes/skelmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,45 @@ def baseSkel(self, *args, **kwargs) -> SkeletonInstance:

By default, baseSkel is used by :func:`~viewSkel`, :func:`~addSkel`, and :func:`~editSkel`.
"""
return self._resolveSkelCls(*args, **kwargs)()
return self._resolveSkelCls(**kwargs)()

def skel(
self,
bones: t.Iterable[str] = (),
bones_from_request: bool = False,
**kwargs,
) -> SkeletonInstance:
"""
Retrieve module-specific skeleton, optionally as subskel.

:param bones: ALlows to specify a list of bones to form a subskel.
:param bones_from_request: Evaluates header X-VIUR-BONELIST to contain a comma-separated list of bones.
Using this parameter enforces that the Skeleton class has a subskel named "*" for required bones that
must exist.

The parameters `bones` and `bones_from_request` can be combined.
"""
skel_cls = self._resolveSkelCls(**kwargs)
bones = set(bones) if bones else set()

if (
bones_from_request # feature generally enabled?
and skel_cls.subSkels.get("*") # a named subSkel "*"" must exist
# and (bonelist := current.request.get().kwargs.get("x-viur-bonelist")) # param must be given (DEBUG!)
and (bonelist := current.request.get().request.headers.get("x-viur-bonelist")) # header must be given
phorward marked this conversation as resolved.
Show resolved Hide resolved
):
bones |= {bone.strip() for bone in bonelist.split(",")}

# Return a subskel?
if bones:
# When coming from outside of a request, "*" must always be contained.
if bones_from_request:
return skel_cls.subskel("*", bones=bones)
phorward marked this conversation as resolved.
Show resolved Hide resolved
phorward marked this conversation as resolved.
Show resolved Hide resolved

return skel_cls.subskel(bones=bones)

# Otherwise, return full skeleton
return skel_cls()

def _apply_default_order(self, query: db.Query):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/viur/core/prototypes/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def viewSkel(self, skelType: SkelType, *args, **kwargs) -> SkeletonInstance:

:return: Returns a Skeleton instance for viewing an entry.
"""
return self.baseSkel(skelType, *args, **kwargs)
return self.skel(bones_from_request=True, skelType=skelType, **kwargs)

def addSkel(self, skelType: SkelType, *args, **kwargs) -> SkeletonInstance:
"""
Expand Down