Managers¶
Managers are the persistence abstraction layer in ripozo. They are responsible for all session to session persistence in a ripozo based application. Typically, we can assume that the persistence layer is a database. However, the actual method of persistence is irrelevant to ripozo. There are ripozo extensions that are specifically for certain database types. At the most basic level, all ripozo ManagerBase subclasses must implement basic CRUD+L. That is they must implement a create, retrieve, update, delete an retrieve_list methods.
Managers in ripozo are not required and are more intended as a way to create a common interface for database interactions. Additionally, they are intended, to abstract away the database since individual database interactions should not be on the resource classes. The one caveat is that the restmixins will not work without a manager defined.
Creating a manager extensions¶
Manager extensions in ripozo can be somewhat tricky to implement correctly. Fortunately, it only needs to be done once (and hopefully shared in the community).
There is only one absolutely critical piece that needs to be implements for a manager to minimally work. The get_field_type method helps the translation and validation work. If not implemented, your manager will not work in any case. This method determines what the python type is for the specified field.
note get_field_type is a classmethod. That means you need to decorate
it with the builtin @classmethod
decorator.
-
classmethod
BaseManager.
get_field_type
(name)[source]¶ Returns the BaseField instance (or subclass) instance that corresponds to the named attribute on the model. For example, if you the column “name” on the model for this manager is a String column, then this should return an instance of StringField with the appropriate requirements.
Parameters: name (unicode) – The name of the field on the model for which you are getting the field name Returns: The BaseField class (or subclass) instance to handle the field specified by the name Return type: ripozo.viewsets.fields.base.BaseField
Additionally, there are 5 basic CRUD+L operations. Not all
of them need to be implemented (with some databases it may not
make sense). However, the method should still be overridden
and a NotImplementedError
exception should be raised if
it is called. Also, keep in mind that if any methods are
not implemented then at least some of the restmixins will
not be available.
-
BaseManager.
create
(values, *args, **kwargs)[source]¶ Create a model with the values according to the values dictionary
Parameters: values (dict) – A dictionary of values to create the model according to Returns: The dictionary of arguments that should be returned by the serializer Return type: dict
-
BaseManager.
retrieve
(lookup_keys, *args, **kwargs)[source]¶ Retrieve a single model and nothing more as a python dictionary
Parameters: lookup_keys (dict) – The lookup keys for the model and the associated values Returns: The dictionary of arguments that should be returned by the serializer Returns: A tuple with the first object being a dictionary of key value pairs according to the fields list and the second object is the meta data (such as the next url for a paginated list) Return type: tuple
-
BaseManager.
retrieve_list
(filters, *args, **kwargs)[source]¶ Retrieves a list of dictionaries containing the fields for the associated model
Parameters: dictlookup_keys – The lookup keys for the model and the associated values Returns: The dictionary of arguments that should be returned by the serializer Returns: A a list of dictionaries of key value pairs according to the fields list Return type: list
-
BaseManager.
update
(lookup_keys, updates, *args, **kwargs)[source]¶ Updates the model found with the lookup keys according to the updates dictionary where the keys are the fields to update and the values are the new values for the field
Parameters: Returns: A dictionary of the full updated model according to the fields class attribute
Return type:
Base Manager API¶
-
class
ripozo.manager_base.
BaseManager
[source]¶ The BaseManager implements some common methods that are valuable across all databases as well as expose an interface that must be implemented by any specific implementation. This needs to be extended in order to implement a new database type. This should handle all direct interactions with a database or ORM. The extended classes are injected into viewsets in order to specify how to get the data from the database.
Parameters: - pagination_pk_query_arg (unicode) – The name of the query parameter that specifies the page or pk for the next set page to return when paginating over a list
- pagination_count_query_arg (unicode) – The name of the query parameter that specifies the maximum number of results to return in a list retrieval
- pagination_next (unicode) – The meta parameter to return that specifies the next query parameters
- paginate_by (int) – The number of results to return by default. This gets overridden by pagination_count_query_arg
- order_by (list) – A list of the fields to order the results by. This may be restricted in certain databases
- _fields (list) – A list of the fields that are able to be manipulated or retrieved by the manager. These are the default fields if _create_fields, _list_fields, or _update_fields are not defined.
- _create_fields (list) – The fields to use if a model is being created. Fields not in this list will not be applied
- _list_fields (list) – The fields to use if a list of models are being retrieved. If not defined, cls.fields will be used instead.
- _update_fields (list) – The fields to use if the model is being updated. Fields not in this list will not be used.
- model (type) – The model that is being managed. This is the individual model that is set by the user. For any type of base class this should be None. However, it is required for actual implementations
-
__weakref__
¶ list of weak references to the object (if defined)
-
create
(values, *args, **kwargs)[source] Create a model with the values according to the values dictionary
Parameters: values (dict) – A dictionary of values to create the model according to Returns: The dictionary of arguments that should be returned by the serializer Return type: dict
-
delete
(lookup_keys, *args, **kwargs)[source] Deletes the model found with the lookup keys
Parameters: lookup_keys (dict) – The keys with which to find the model to delete Returns: nothing. Return type: NoneType
-
dot_field_list_to_dict
(fields=None)[source]¶ Converts a list of dot delimited fields (and related fields) and turns it into a dictionary for example, it would transform
>>> dot_field_list_to_dict(['id', 'value', 'related.id', 'related.related_value']) { 'id': None, 'value': None, 'related': { 'id': None, 'related_value': None } }
Parameters: fields (list) – Returns: A dictionary of the fields layered as to indicate relationships. Return type: dict
-
classmethod
get_field_type
(name)[source] Returns the BaseField instance (or subclass) instance that corresponds to the named attribute on the model. For example, if you the column “name” on the model for this manager is a String column, then this should return an instance of StringField with the appropriate requirements.
Parameters: name (unicode) – The name of the field on the model for which you are getting the field name Returns: The BaseField class (or subclass) instance to handle the field specified by the name Return type: ripozo.viewsets.fields.base.BaseField
-
get_pagination_count
(filters)[source]¶ Get the pagination count from the args
Parameters: filters (dict) – All of the args Returns: tuple of (pagination_count, updated_filters Return type: tuple
-
get_pagination_pks
(filters)[source]¶ Get the pagination pks from the args
Parameters: filters (dict) – All of the args Returns: tuple of (pagination_pks, updated_filters Return type: tuple
-
retrieve
(lookup_keys, *args, **kwargs)[source] Retrieve a single model and nothing more as a python dictionary
Parameters: lookup_keys (dict) – The lookup keys for the model and the associated values Returns: The dictionary of arguments that should be returned by the serializer Returns: A tuple with the first object being a dictionary of key value pairs according to the fields list and the second object is the meta data (such as the next url for a paginated list) Return type: tuple
-
retrieve_list
(filters, *args, **kwargs)[source] Retrieves a list of dictionaries containing the fields for the associated model
Parameters: dictlookup_keys – The lookup keys for the model and the associated values Returns: The dictionary of arguments that should be returned by the serializer Returns: A a list of dictionaries of key value pairs according to the fields list Return type: list
-
update
(lookup_keys, updates, *args, **kwargs)[source] Updates the model found with the lookup keys according to the updates dictionary where the keys are the fields to update and the values are the new values for the field
Parameters: Returns: A dictionary of the full updated model according to the fields class attribute
Return type: