Skip to content

Commit

Permalink
feat: Add read method for RefSkel (#1193)
Browse files Browse the repository at this point in the history
Resolves #1101

---------

Co-authored-by: Jan Max Meyer <jmm@phorward.de>
Co-authored-by: Sven Eberth <mail@sveneberth.de>
  • Loading branch information
3 people authored Jul 30, 2024
1 parent a8778ee commit 5d1e14d
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/viur/core/skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def __getattr__(self, item: str):
# Load a @classmethod from the Skeleton class and bound this SkeletonInstance
elif item in {"fromDB", "toDB", "all", "unserialize", "serialize", "fromClient", "getCurrentSEOKeys",
"preProcessSerializedData", "preProcessBlobLocks", "postSavedHandler", "setBoneValue",
"delete", "postDeletedHandler", "refresh"}:
"delete", "postDeletedHandler", "refresh", "read"}:
return partial(getattr(self.skeletonCls, item), self)
# Load a @property from the Skeleton class
try:
Expand Down Expand Up @@ -1394,6 +1394,22 @@ def fromSkel(cls, kindName: str, *args: list[str]) -> t.Type[RefSkel]:
newClass.__boneMap__ = bone_map
return newClass

def read(self, key: t.Optional[db.Key | str | int] = None) -> SkeletonInstance:
"""
Read full skeleton instance referenced by the RefSkel from the database.
Can be used for reading the full Skeleton from a RefSkel.
The `key` parameter also allows to read another, given key from the related kind.
:raise ValueError: If the entry is no longer in the database.
"""
skel = skeletonByKind(self.kindName)()

if not skel.fromDB(key or self["key"]):
raise ValueError(f"""The key {key or self["key"]!r} seems to be gone""")

return skel


class SkelList(list):
"""
Expand Down

0 comments on commit 5d1e14d

Please sign in to comment.