core.skeleton

Module Contents

Classes

MetaBaseSkel

This is the metaclass for Skeletons.

SkeletonInstance

The actual wrapper around a Skeleton-Class. An object of this class is what's actually returned when you

BaseSkeleton

This is a container-object holding information about one database entity.

MetaSkel

This is the metaclass for Skeletons.

CustomDatabaseAdapter

ViurTagsSearchAdapter

This Adapter implements a simple fulltext search on top of the datastore.

SeoKeyBone

Special kind of StringBone saving its contents as viurCurrentSeoKeys into the entity's viur dict.

Skeleton

This is a container-object holding information about one database entity.

RelSkel

This is a Skeleton-like class that acts as a container for Skeletons used as a

RefSkel

This is a Skeleton-like class that acts as a container for Skeletons used as a

SkelList

This class is used to hold multiple skeletons together with other, commonly used information.

TaskUpdateSearchIndex

This tasks loads and saves every entity of the given module.

RebuildSearchIndex

BaseClass to run a database Query and process each entry matched.

TaskVacuumRelations

Checks entries in viur-relations and verifies that the src-kind

Functions

skeletonByKind(kindName)

Returns the Skeleton-Class for the given kindName. That skeleton must exist, otherwise an exception is raised.

listKnownSkeletons()

return:

A list of all known kindnames (all kindnames for which a skeleton is defined)

iterAllSkelClasses()

return:

An iterator that yields each Skeleton-Class once. (Only top-level skeletons are returned, so no

processRemovedRelations(removedKey[, cursor])

updateRelations(destKey, minChangeTime, changedBone[, ...])

This function updates Entities, which may have a copy of values from another entity which has been recently

processVacuumRelationsChunk(module, cursor[, ...])

Processes 25 Entries and calls the next batch

__getattr__(attr)

Attributes

_undefined

ABSTRACT_SKEL_CLS_SUFFIX

__DEPRECATED_NAMES

core.skeleton._undefined
core.skeleton.ABSTRACT_SKEL_CLS_SUFFIX = 'AbstractSkel'
class core.skeleton.MetaBaseSkel(name, bases, dct)

Bases: type

This is the metaclass for Skeletons. It is used to enforce several restrictions on bone names, etc.

_skelCache
_allSkelClasses
__reserved_keywords
__allowed_chars
static generate_bonemap(cls)

Recursively constructs a dict of bones from

__setattr__(key, value)

Implement setattr(self, name, value).

core.skeleton.skeletonByKind(kindName)

Returns the Skeleton-Class for the given kindName. That skeleton must exist, otherwise an exception is raised. :param kindName: The kindname to retreive the skeleton for :return: The skeleton-class for that kind

Parameters:

kindName (str) –

Return type:

Type[Skeleton]

core.skeleton.listKnownSkeletons()
Returns:

A list of all known kindnames (all kindnames for which a skeleton is defined)

Return type:

list[str]

core.skeleton.iterAllSkelClasses()
Returns:

An iterator that yields each Skeleton-Class once. (Only top-level skeletons are returned, so no RefSkel classes will be included)

Return type:

Iterable[Skeleton]

class core.skeleton.SkeletonInstance(skelCls, subSkelNames=None, fullClone=False, clonedBoneMap=None)

The actual wrapper around a Skeleton-Class. An object of this class is what’s actually returned when you call a Skeleton-Class. With ViUR3, you don’t get an instance of a Skeleton-Class any more - it’s always this class. This is much faster as this is a small class.

__slots__
items(yieldBoneValues=False)
Parameters:

yieldBoneValues (bool) –

Return type:

Iterable[tuple[str, viur.core.bones.BaseBone]]

keys()
Return type:

Iterable[str]

values()
Return type:

Iterable[Any]

__iter__()
Return type:

Iterable[str]

__contains__(item)
get(item, default=None)
__setitem__(key, value)
__getitem__(key)
__getattr__(item)

Get a special attribute from the SkeletonInstance

__getattr__ is called when an attribute access fails with an AttributeError. So we know that this is not a real attribute of the SkeletonInstance. But there are still a few special cases in which attributes are loaded from the skeleton class.

Parameters:

item (str) –

__delattr__(item)

Implement delattr(self, name).

__setattr__(key, value)

Implement setattr(self, name, value).

__repr__()

Return repr(self).

Return type:

str

__str__()

Return str(self).

Return type:

str

__len__()
Return type:

int

clone()

Clones a SkeletonInstance into a modificable, stand-alone instance. This will also allow to modify the underlying data model.

ensure_is_cloned()

Ensured this SkeletonInstance is a stand-alone clone, which can be modified. Does nothing in case it was already cloned before.

setEntity(entity)
Parameters:

entity (viur.core.db.Entity) –

structure()
Return type:

dict

__deepcopy__(memodict)
class core.skeleton.BaseSkeleton

Bases: object

This is a container-object holding information about one database entity.

It has to be sub-classed with individual information about the kindName of the entities and its specific data attributes, the so called bones. The Skeleton stores its bones in an OrderedDict-Instance, so the definition order of the contained bones remains constant.

Variables:
  • key (server.bones.BaseBone) – This bone stores the current database key of this entity. Assigning to this bones value is dangerous and does not affect the actual key its stored in.

  • creationdate (server.bones.DateBone) – The date and time where this entity has been created.

  • changedate (server.bones.DateBone) – The date and time of the last change to this entity.

__viurBaseSkeletonMarker__ = True
boneMap
classmethod subSkel(*name, fullClone=False, **kwargs)

Creates a new sub-skeleton as part of the current skeleton.

A sub-skeleton is a copy of the original skeleton, containing only a subset of its bones. To define sub-skeletons, use the subSkels property of the Skeleton object.

By passing multiple sub-skeleton names to this function, a sub-skeleton with the union of all bones of the specified sub-skeletons is returned.

If an entry called “*” exists in the subSkels-dictionary, the bones listed in this entry will always be part of the generated sub-skeleton.

Parameters:
  • name – Name of the sub-skeleton (that’s the key of the subSkels dictionary); Multiple names can be specified.

  • fullClone (bool) –

Returns:

The sub-skeleton of the specified type.

Return type:

SkeletonInstance

classmethod setSystemInitialized()
classmethod setBoneValue(skelValues, boneName, value, append=False, language=None)

Allows for setting a bones value without calling fromClient or assigning a value directly. Sanity-Checks are performed; if the value is invalid, that bone flips back to its original (default) value and false is returned.

Parameters:
  • boneName (str) – The name of the bone to be modified

  • value (Any) – The value that should be assigned. It’s type depends on the type of that bone

  • append (bool) – If True, the given value is appended to the values of that bone instead of replacing it. Only supported on bones with multiple=True

  • language (Optional[str]) – Language to set

  • skelValues (SkeletonInstance) –

Returns:

Wherever that operation succeeded or not.

Return type:

bool

classmethod fromClient(skelValues, data)

Load supplied data into Skeleton.

This function works similar to setValues(), except that the values retrieved from data are checked against the bones and their validity checks.

Even if this function returns False, all bones are guaranteed to be in a valid state. The ones which have been read correctly are set to their valid values; Bones with invalid values are set back to a safe default (None in most cases). So its possible to call toDB() afterwards even if reading data with this function failed (through this might violates the assumed consistency-model).

Parameters:
  • data (dict[str, list[str] | str]) – Dictionary from which the data is read.

  • skelValues (SkeletonInstance) –

Returns:

True if all data was successfully read and taken by the Skeleton’s bones. False otherwise (eg. some required fields where missing or invalid).

Return type:

bool

classmethod refresh(skel)

Refresh the bones current content.

This function causes a refresh of all relational bones and their associated information.

Parameters:

skel (SkeletonInstance) –

class core.skeleton.MetaSkel(name, bases, dct)

Bases: MetaBaseSkel

This is the metaclass for Skeletons. It is used to enforce several restrictions on bone names, etc.

class core.skeleton.CustomDatabaseAdapter
providesFulltextSearch: bool = False
fulltextSearchGuaranteesQueryConstrains = False
providesCustomQueries: bool = False
preprocessEntry(entry, skel, changeList, isAdd)

Can be overridden to add or alter the data of this entry before it’s written to firestore. Will always be called inside an transaction. :param entry: The entry containing the serialized data of that skeleton :param skel: The (complete) skeleton this skel.toDB() runs for :param changeList: List of boneNames that are changed by this skel.toDB() call :param isAdd: Is this an update or an add? :return: The (maybe modified) entity

Parameters:
  • entry (viur.core.db.Entity) –

  • skel (BaseSkeleton) –

  • changeList (list[str]) –

  • isAdd (bool) –

Return type:

viur.core.db.Entity

updateEntry(dbObj, skel, changeList, isAdd)

Like meth:preprocessEntry, but runs after the transaction had completed. Changes made to dbObj will be ignored. :param entry: The entry containing the serialized data of that skeleton :param skel: The (complete) skeleton this skel.toDB() runs for :param changeList: List of boneNames that are changed by this skel.toDB() call :param isAdd: Is this an update or an add?

Parameters:
  • dbObj (viur.core.db.Entity) –

  • skel (BaseSkeleton) –

  • changeList (list[str]) –

  • isAdd (bool) –

Return type:

None

deleteEntry(entry, skel)

Called, after an skeleton has been successfully deleted from firestore :param entry: The db.Entity object containing an snapshot of the data that has been deleted :param skel: The (complete) skeleton for which `meth:delete’ had been called

Parameters:
Return type:

None

abstract fulltextSearch(queryString, databaseQuery)

If this database supports fulltext searches, this method has to implement them. If it’s a plain fulltext search engine, leave ‘prop:fulltextSearchGuaranteesQueryConstrains’ set to False, then the server will post-process the list of entries returned from this function and drop any entry that cannot be returned due to other constrains set in ‘param:databaseQuery’. If you can obey every constrain set in that Query, we can skip this post-processing and save some CPU-cycles. :param queryString: the string as received from the user (no quotation or other safety checks applied!) :param databaseQuery: The query containing any constrains that returned entries must also match :return:

Parameters:
  • queryString (str) –

  • databaseQuery (viur.core.db.Query) –

Return type:

list[viur.core.db.Entity]

class core.skeleton.ViurTagsSearchAdapter(min_length=2, max_length=50, substring_matching=False)

Bases: CustomDatabaseAdapter

This Adapter implements a simple fulltext search on top of the datastore.

On skel.toDB(), all words from String-/TextBones are collected with all min_length postfixes and dumped into the property viurTags. When queried, we’ll run a prefix-match against this property - thus returning entities with either an exact match or a match within a word.

Example:

For the word “hello” we’ll write “hello”, “ello” and “llo” into viurTags. When queried with “hello” we’ll have an exact match. When queried with “hel” we’ll match the prefix for “hello” When queried with “ell” we’ll prefix-match “ello” - this is only enabled when substring_matching is True.

We’ll automatically add this adapter if a skeleton has no other database adapter defined.

Parameters:
  • min_length (int) –

  • max_length (int) –

  • substring_matching (bool) –

providesFulltextSearch = True
fulltextSearchGuaranteesQueryConstrains = True
_tagsFromString(value)

Extract all words including all min_length postfixes from given string

Parameters:

value (str) –

Return type:

set[str]

preprocessEntry(entry, skel, changeList, isAdd)

Collect searchTags from skeleton and build viurTags

Parameters:
  • entry (viur.core.db.Entity) –

  • skel (Skeleton) –

  • changeList (list[str]) –

  • isAdd (bool) –

Return type:

viur.core.db.Entity

fulltextSearch(queryString, databaseQuery)

Run a fulltext search

Parameters:
  • queryString (str) –

  • databaseQuery (viur.core.db.Query) –

Return type:

list[viur.core.db.Entity]

class core.skeleton.SeoKeyBone(*, caseSensitive=True, max_length=254, min_length=None, natural_sorting=False, **kwargs)

Bases: viur.core.bones.StringBone

Special kind of StringBone saving its contents as viurCurrentSeoKeys into the entity’s viur dict.

Initializes a new StringBone.

Parameters:
  • caseSensitive (bool) – When filtering for values in this bone, should it be case-sensitive?

  • max_length (int | None) – The maximum length allowed for values of this bone. Set to None for no limitation.

  • min_length (int | None) – The minimum length allowed for values of this bone. Set to None for no limitation.

  • natural_sorting (bool | Callable) – Allows a more natural sorting than the default sorting on the plain values. This uses the .sort_idx property. True enables sorting according to DIN 5007 Variant 2. With passing a callable, a custom transformer method can be set that creates the value for the index property.

  • kwargs – Inherited arguments from the BaseBone.

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 (viur.core.skeleton.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

class core.skeleton.Skeleton(*args, **kwargs)

Bases: BaseSkeleton

This is a container-object holding information about one database entity.

It has to be sub-classed with individual information about the kindName of the entities and its specific data attributes, the so called bones. The Skeleton stores its bones in an OrderedDict-Instance, so the definition order of the contained bones remains constant.

Variables:
  • key (server.bones.BaseBone) – This bone stores the current database key of this entity. Assigning to this bones value is dangerous and does not affect the actual key its stored in.

  • creationdate (server.bones.DateBone) – The date and time where this entity has been created.

  • changedate (server.bones.DateBone) – The date and time of the last change to this entity.

kindName: str
customDatabaseAdapter: CustomDatabaseAdapter | None
subSkels
interBoneValidations: list[Callable[[Skeleton], list[viur.core.bones.base.ReadFromClientError]]] = []
__seo_key_trans
key
name
creationdate
changedate
viurCurrentSeoKeys
__repr__()

Return repr(self).

__str__()

Return str(self).

classmethod all(skelValues, **kwargs)

Create a query with the current Skeletons kindName.

Returns:

A db.Query object which allows for entity filtering and sorting.

Return type:

viur.core.db.Query

classmethod fromClient(skelValues, data)

This function works similar to setValues(), except that the values retrieved from data are checked against the bones and their validity checks.

Even if this function returns False, all bones are guaranteed to be in a valid state. The ones which have been read correctly are set to their valid values; Bones with invalid values are set back to a safe default (None in most cases). So its possible to call toDB() afterwards even if reading data with this function failed (through this might violates the assumed consistency-model).

Parameters:
  • data (dict[str, list[str] | str]) – Dictionary from which the data is read.

  • skelValues (SkeletonInstance) –

Returns:

True, if all values have been read correctly (without errors), False otherwise

Return type:

bool

classmethod fromDB(skel, key)

Load entity with key from the Datastore into the Skeleton.

Reads all available data of entity kind kindName and the key key from the Datastore into the Skeleton structure’s bones. Any previous data of the bones will discard.

To store a Skeleton object to the Datastore, see toDB().

Parameters:
  • key (viur.core.db.Key | int | str) – A viur.core.DB.Key, string, or int; from which the data shall be fetched.

  • skel (SkeletonInstance) –

Returns:

True on success; False if the given key could not be found or can not be parsed.

Return type:

bool

classmethod toDB(skel, update_relations=True, **kwargs)

Store current Skeleton entity to the Datastore.

Stores the current data of this instance into the database. If an key value is set to the object, this entity will ne updated; Otherwise a new entity will be created.

To read a Skeleton object from the data store, see fromDB().

Parameters:
  • update_relations (bool) – If False, this entity won’t be marked dirty; This avoids from being fetched by the background task updating relations.

  • skel (SkeletonInstance) –

Returns:

The datastore key of the entity.

Return type:

viur.core.db.Key

classmethod preProcessBlobLocks(skelValues, locks)

Can be overridden to modify the list of blobs referenced by this skeleton

classmethod preProcessSerializedData(skelValues, entity)

Can be overridden to modify the viur.core.db.Entity before its actually written to the data store.

classmethod postSavedHandler(skelValues, key, dbObj)

Can be overridden to perform further actions after the entity has been written to the data store.

classmethod postDeletedHandler(skelValues, key)

Can be overridden to perform further actions after the entity has been deleted from the data store.

classmethod getCurrentSEOKeys(skelValues)

Should be overridden to return a dictionary of language -> SEO-Friendly key this entry should be reachable under. How theses names are derived are entirely up to the application. If the name is already in use for this module, the server will automatically append some random string to make it unique. :return:

Return type:

None | dict[str, str]

classmethod delete(skelValues)

Deletes the entity associated with the current Skeleton from the data store.

class core.skeleton.RelSkel

Bases: BaseSkeleton

This is a Skeleton-like class that acts as a container for Skeletons used as a additional information data skeleton for extendedRelationalBone.

It needs to be sub-classed where information about the kindName and its attributes (bones) are specified.

The Skeleton stores its bones in an OrderedDict-Instance, so the definition order of the contained bones remains constant.

serialize(parentIndexed)
unserialize(values)

Loads ‘values’ into this skeleton.

Parameters:

values (viur.core.db.Entity | dict) – dict with values we’ll assign to our bones

class core.skeleton.RefSkel

Bases: RelSkel

This is a Skeleton-like class that acts as a container for Skeletons used as a additional information data skeleton for extendedRelationalBone.

It needs to be sub-classed where information about the kindName and its attributes (bones) are specified.

The Skeleton stores its bones in an OrderedDict-Instance, so the definition order of the contained bones remains constant.

classmethod fromSkel(kindName, *args)

Creates a relSkel from a skeleton-class using only the bones explicitly named in *args

Parameters:
  • args (list[str]) – List of bone names we’ll adapt

  • kindName (str) –

Returns:

A new instance of RefSkel

Return type:

Type[RefSkel]

class core.skeleton.SkelList(baseSkel=None)

Bases: list

This class is used to hold multiple skeletons together with other, commonly used information.

SkelLists are returned by Skel().all()…fetch()-constructs and provide additional information about the data base query, for fetching additional entries.

Variables:

cursor (str) – Holds the cursor within a query.

Parameters:

baseSkel – The baseclass for all entries in this list

__slots__ = ('baseSkel', 'customQueryInfo', 'getCursor', 'get_orders', 'renderPreparation')
core.skeleton.processRemovedRelations(removedKey, cursor=None)
core.skeleton.updateRelations(destKey, minChangeTime, changedBone, cursor=None)

This function updates Entities, which may have a copy of values from another entity which has been recently edited (updated). In ViUR, relations are implemented by copying the values from the referenced entity into the entity that’s referencing them. This allows ViUR to run queries over properties of referenced entities and prevents additional db.Get’s to these referenced entities if the main entity is read. However, this forces us to track changes made to entities as we might have to update these mirrored values. This is the deferred call from meth:viur.core.skeleton.Skeleton.toDB() after an update (edit) on one Entity to do exactly that.

Parameters:
  • destKey (viur.core.db.Key) – The database-key of the entity that has been edited

  • minChangeTime (int) – The timestamp on which the edit occurred. As we run deferred, and the entity might have been edited multiple times before we get acutally called, we can ignore entities that have been updated in the meantime as they’re already up2date

  • changedBone (Optional[str]) – If set, we’ll update only entites that have a copy of that bone. Relations mirror only key and name by default, so we don’t have to update these if only another bone has been changed.

  • cursor (Optional[str]) – The database cursor for the current request as we only process five entities at once and then defer again.

class core.skeleton.TaskUpdateSearchIndex

Bases: viur.core.tasks.CallableTaskBase

This tasks loads and saves every entity of the given module. This ensures an updated searchIndex and verifies consistency of this data.

key = 'rebuildSearchIndex'
name = 'Rebuild search index'
descr = 'This task can be called to update search indexes and relational information.'
canCall()

Checks wherever the current user can execute this task

Return type:

bool

dataSkel()

If additional data is needed, return a skeleton-instance here. These values are then passed to execute.

execute(module, *args, **kwargs)

The actual code that should be run goes here.

static _run(module, notify)
Parameters:
  • module (str) –

  • notify (str) –

class core.skeleton.RebuildSearchIndex

Bases: viur.core.tasks.QueryIter

BaseClass to run a database Query and process each entry matched. This will run each step deferred, so it is possible to process an arbitrary number of entries without being limited by time or memory.

To use this class create a subclass, override the classmethods handleEntry and handleFinish and then call startIterOnQuery with an instance of a database Query (and possible some custom data to pass along)

classmethod handleEntry(skel, customData)

Overridable hook to process one entry. “entry” will be either an db.Entity or an SkeletonInstance (if that query has been created by skel.all())

Warning: If your query has an sortOrder other than __key__ and you modify that property here it is possible to encounter that object later one again (as it may jump behind the current cursor).

Parameters:
classmethod handleFinish(totalCount, customData)

Overridable hook that indicates the current run has been finished.

Parameters:
  • totalCount (int) –

  • customData (dict[str, str]) –

class core.skeleton.TaskVacuumRelations

Bases: TaskUpdateSearchIndex

Checks entries in viur-relations and verifies that the src-kind and it’s RelationalBone still exists.

key = 'vacuumRelations'
name = 'Vacuum viur-relations (dangerous)'
descr = 'Drop stale inbound relations for the given kind'
execute(module, *args, **kwargs)

The actual code that should be run goes here.

Parameters:

module (str) –

core.skeleton.processVacuumRelationsChunk(module, cursor, count_total=0, count_removed=0, notify=None)

Processes 25 Entries and calls the next batch

Parameters:
  • module (str) –

  • count_total (int) –

  • count_removed (int) –

core.skeleton.__DEPRECATED_NAMES
core.skeleton.__getattr__(attr)
Parameters:

attr (str) –

Return type:

object