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 (Optional[Iterable[str]]) – The allowed entity kinds for the KeyBone.

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

Initializes a new Bone.

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

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

  • parse_only (bool) –

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.

singleValueUnserialize(val)

Unserializes a single value of the bone from the stored database value.

Derived bone classes should overwrite this method to implement their own logic for unserializing single values. The unserialized value should be suitable for use in the application logic.

unserialize(skel, name)

Deserialize bone data from the datastore and populate the bone with the deserialized values.

This function is the inverse of the serialize function. It converts data from the datastore into a format that can be used by the bones in the skeleton.

Parameters:
  • skel (SkeletonInstance) – A SkeletonInstance object containing the values to be deserialized.

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

Returns:

True if deserialization is successful, False otherwise.

Return type:

bool

serialize(skel, name, parentIndexed)

Serializes this bone into a format that can be written into the datastore.

Parameters:
  • skel (SkeletonInstance) – A SkeletonInstance object containing the values to be serialized.

  • name (str) – A string representing the property name of the bone in its Skeleton (not the description).

  • parentIndexed (bool) – A boolean indicating whether the parent bone is indexed.

Returns:

A boolean indicating whether the serialization was successful.

Return type:

bool

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.