cubeserver_common.models.utils package
Submodules
cubeserver_common.models.utils.complexdictcodec module
For encoding/decoding “complex dictionaries” into bson
“complex dictionary” meaning a dictionary with non-bson-compatible types
- class cubeserver_common.models.utils.complexdictcodec.ComplexDictCodec(key_codec: ~bson.codec_options.TypeCodec = <class 'cubeserver_common.models.utils.dummycodec.DummyCodec'>, value_codec: ~bson.codec_options.TypeCodec = <class 'cubeserver_common.models.utils.dummycodec.DummyCodec'>)
Bases:
TypeCodecCodec for encoding a complex dictionary into bson
- property bson_type
The BSON type to be converted into our own type.
- property python_type
The Python type to be converted into something serializable.
cubeserver_common.models.utils.dummycodec module
A dummy, fallback codec that effectively does nothing.
- class cubeserver_common.models.utils.dummycodec.DummyCodec(type_class: ~typing.Type[~typing.Any] = <class 'str'>)
Bases:
TypeCodecDummy Codec. Does nothing as far as encoding/decoding. Returns what was given. No questions asked.
- property bson_type
The BSON type to be converted into our own type.
- property python_type
The Python type to be converted into something serializable.
- transform_bson(value)
Convert the given BSON value into our own type.
- transform_python(value)
Convert the given Python object into something serializable.
cubeserver_common.models.utils.enumcodec module
For encoding/decoding Enums into bson
Limitations: Only works with Enums that have bson-compatible typed values Enum values must all be of the same type
- class cubeserver_common.models.utils.enumcodec.EnumCodec(enum_class: Type[Enum], value_class: type = Ellipsis)
Bases:
TypeCodecCodec for encoding a generic Enum into bson This assumes the Enum only contains primitive/built-in/bson-compatible types
- property bson_type
The BSON type to be converted into our own type.
- property python_type
The Python type to be converted into something serializable.
cubeserver_common.models.utils.listcodec module
For encoding/decoding lists
Limitations: Assumes the type of the list’s elements is uniform
cubeserver_common.models.utils.modelutils module
Some utility classes to help with object mapping
- class cubeserver_common.models.utils.modelutils.Encodable
Bases:
_EncoderAn abstract class for classes that contain codec data
- class cubeserver_common.models.utils.modelutils.EncodableCodec(encodable: Type[_Encoder])
Bases:
TypeCodecA TypeCodec for PyMongoModel objects
- class cubeserver_common.models.utils.modelutils.PyMongoModel
Bases:
EncodableA class for easy object-mapping to bson. Extend this class for any classes that describe a type of document.
- property collection
Define the Mongodb collection in your class. Use the PyMongoModel.model_type_registry as the type registry.
- classmethod decode(value: Optional[Mapping[str, Any]]) Optional[Encodable]
Populates an object from a dictionary of the document This returns None only if the bson value given is None
- classmethod find(*args, **kwargs)
Finds documents from the collection Arguments are the same as those for PyMongo.collection’s find().
- classmethod find_by_id(identifier)
Finds a document from the collection, given the id
- classmethod find_codec(field_name: str, field_type_name: str) TypeCodec
Finds a codec for a given field (w/ name and type name specified)
- classmethod find_one(*args, **kwargs)
Finds a document from the collection Arguments are the same as those for PyMongo’s find_one().
- classmethod find_safe(*args, **kwargs)
- find_self()
Returns the database’s version of self
- classmethod find_sorted(*args, key: str = Ellipsis, order=1, **kwargs)
Same a find(), but with sorting!
- property id
The internal document identifier
- property id_secondary
A dummy property; always equal to id Used to have multiple id-driven columns in a Flask-tables table
- locate_codec(data_type: type) Optional[TypeCodec]
Tries to find a TypeCodec for the specified type if possible.
- property model_type_registry
A TypeRegistry to be used when getting the collection from the database
- register_codec(type_codec: TypeCodec, replace=False)
Register a TypeCodec for use in the PyMongoModelCodec Specify whether to replace an existing one if applicable, with the default being False.
- register_field(attr_name: str, value: Optional[Any] = None, custom_codec: Optional[TypeCodec] = None)
Register each attribute of the model for the database. Optionally specify with a custom codec prior to setting the attribute. If a codec is specified optionally here, it will not be automatically registered for use in encoding/decoding attributes of the same type unless register_codec() is used also. The value does not need to be specified ONLY IF a custom_codec is provided. This method returns immediately if a field with the given name is already registered. The force parameter allows you to force the registration of a field despite checks failing or the type appearing to be bson-compatible
- remove()
Removes this document from the collection
- save()
Saves this document to the collection
- set_attr_from_string(field_name: str, value: str)
Decodes and updates a single string value to the document object
Module contents
Classes to allow for easy object-mapping to bson for MongoDb