core.bones.key

Module Contents

Classes

KeyBone

The KeyBone is used for managing keys in the database. It provides various methods for validating,

class core.bones.key.KeyBone(*, descr='Key', readOnly=True, visible=False, allowed_kinds=None, check=False, **kwargs)

Bases: viur.core.bones.base.BaseBone

The KeyBone is used for managing keys in the database. It provides various methods for validating, converting, and storing key values, as well as querying the database. Key management is crucial for maintaining relationships between entities in the database, and the KeyBone class helps ensure that keys are handled correctly and efficiently throughout the system.

Parameters:
  • descr (str) – The description of the KeyBone.

  • readOnly (bool) – Whether the KeyBone is read-only.

  • visible (bool) – Whether the KeyBone is visible.

  • allowed_kinds (None | list[str]) – The allowed entity kinds for the KeyBone.

  • check (bool) – Whether to check for entity existence.

  • allowed_kinds

Initializes a new Bone.

type = 'key'
singleValueFromClient(value, skel, bone_name, client_data)

Load a single value from a client

Parameters:
  • value – The single value which should be loaded.

  • skel – The SkeletonInstance where the value should be loaded into.

  • bone_name – The bone name of this bone in the SkeletonInstance.

  • client_data – The data taken from the client, a dictionary with usually bone names as key

Returns:

A tuple. If the value is valid, the first element is the parsed value and the second is None. If the value is invalid or not parseable, the first element is a empty value and the second a list of ReadFromClientError.

unserialize(skel, name)

This method is the inverse of :meth:serialize. It reads the key value from the datastore and populates the corresponding KeyBone in the Skeleton. The method converts the value from the datastore into an appropriate format for further use in the program.

Parameters:
  • skel (viur.core.skeleton.SkeletonValues) – The SkeletonValues instance this bone is a part of.

  • name (str) – The property name of this bone in the Skeleton (not the description).

Returns:

A boolean value indicating whether the operation was successful. Returns True if the key value was successfully unserialized and added to the accessedValues of the Skeleton, and False otherwise.

Return type:

bool

Note

The method contains an inner function, fixVals(val), which normalizes and validates the key values before populating the bone.

serialize(skel, name, parentIndexed)

This method serializes the KeyBone into a format that can be written to the datastore. It converts the key value from the Skeleton object into a format suitable for storage in the datastore.

Parameters:
  • skel (SkeletonInstance) – The SkeletonInstance this bone is a part of.

  • name (str) – The property name of this bone in the Skeleton (not the description).

  • parentIndexed (bool) – A boolean value indicating whether the parent entity is indexed or not.

Returns:

A boolean value indicating whether the operation was successful. Returns True if the key value was successfully serialized and added to the datastore entity, and False otherwise.

Return type:

bool

Note

Key values are always indexed, so the method discards any exclusion from indexing for key values.

buildDBFilter(name, skel, dbFilter, rawFilter, prefix=None)

This method parses the search filter specified by the client in their request and converts it into a format that can be understood by the datastore. It takes care of ignoring filters that do not target this bone and safely handles malformed data in the raw filter.

Parameters:
  • name (str) – The property name of this bone in the Skeleton (not the description).

  • skel (viur.core.skeleton.SkeletonInstance) – The :class:viur.core.skeleton.SkeletonInstance this bone is a part of.

  • dbFilter (viur.core.db.Query) – The current :class:viur.core.db.Query instance the filters should be applied to.

  • rawFilter (dict) – The dictionary of filters the client wants to have applied.

  • prefix (Optional[str]) – An optional string to prepend to the filter key. Defaults to None.

Returns:

The modified :class:viur.core.db.Query.

Return type:

viur.core.db.Query

The method takes the following steps:

  1. Decodes the provided key(s) from the raw filter.

  2. If the filter contains a list of keys, it iterates through the list, creating a new

    filter for each key and appending it to the list of queries.

  3. If the filter contains a single key, it applies the filter directly to the query.

  4. In case of any invalid key or other issues, it raises a RuntimeError.