The Nursery

exception importer.nursery.HandlerDoesNotExistError[source]
class importer.nursery.TariffObjectNursery(cache: importer.cache.cache.ObjectCacheFacade)[source]

Provides an interface between raw data and the Django modelling system for the tariff.

The primary function is to take a raw python object (generally a dictionary) and convert it into a row in the database via the Django models.

This layer of separation comes from the fact that often, when receiving data, the system will receive incomplete data objects which depend on data received later. As a result this “Nursery” is designed to store and look after incomplete data objects until it can be matched with the rest of the data and then dispatched to the database.

cache_current_instances()[source]

Take all current instances of all TrackedModels in the data and cache them.

cache_object(obj: common.models.trackedmodel.TrackedModel)[source]

Caches an objects primary key and model name in the cache.

Key is generated based on the model name and the identifying fields used to find it.

clear_cache(repeats: int = 2)[source]

Iterate over the cache and handle any objects which can be resolved.

As the importer runs many objects are created before their proper relationships can be found. In some cases these will be cleared by interdependent handlers. However in some cases (foreign key links) the objects are left dangling in the cache.

This method runs through the cache and tries to resolve all objects that it can. As some of the objects often depend on other objects which are also in the cache, but appear later, this method runs recursively based on the repeats argument

classmethod generate_cache_key(model: Type[common.models.trackedmodel.TrackedModel], identifying_fields: Iterable, obj: dict) str[source]

Generate a cache key based on the model name and the identifying values used to find it.

get_handler(tag: str)[source]

Find a handler which matches the given tag.

If one is not found throw an error.

Find all the unique link fields for a given model.

This gives all the combinations of fields used to identify a model.

classmethod get_obj_from_cache(model: Type[common.models.trackedmodel.TrackedModel], identifying_fields: Iterable, obj: dict) Tuple[int, str][source]

Fetches an object PK and model name from the cache if it exists.

classmethod register_handler(handler: Type[BaseHandler])[source]

Registers a handler to the TariffObjectNursery class.

The nursery needs to be able to map all handlers against incoming data. To do this the nursery needs a map of handlers. This method allows handlers to register themselves so that they can be added to the internal nursery map - to be used when processing.

remove_object_from_cache(obj: common.models.trackedmodel.TrackedModel)[source]

Removes an object from the importer cache. If an object has to be deleted (generally done in dev only) then it is problematic to keep the ID in the cache as well.

Key is generated based on the model name and the identifying fields used to find it.

submit(obj: importer.utils.DispatchedObjectType)[source]

Entrypoint for the nursery.

Handles whether an object can be dispatched to the database or, if some pieces of data are missing, cached to await new data.

importer.nursery.get_nursery(object_cache=None) importer.nursery.TariffObjectNursery[source]

Convenience function for building a nursery object.