djblets.webapi.resources.registry¶
Resource registration and lookup.
- class ResourcesRegistry[source]¶
Manages a registry of instances of API resources.
This handles dynamically loading API resource instances upon request, and registering those resources with models.
When accessing a resource through this class for the first time, it will be imported from the proper file and cached. Subsequent requests will be returned from the cache.
While an optional class, consumers are encouraged to create a subclass of this that they use for all resource instance references and for registering model to resource mappings.
- register_resource_for_model(model, resource)[source]¶
Register a resource as the official location for a model.
- Parameters
model (djagno.db.models.Model) – The model associated with the resource.
resource (djblets.webapi.resources.base.WebAPIResource or callable) – Either a WebAPIResource, or a function that takes an instance of
model
and returns a WebAPIResource.
- unregister_resource_for_model(model)[source]¶
Remove the official location for a model.
- Parameters
model (django.db.models.Model) – The model associated with the resource to remove.
- get_resource_for_object(obj)[source]¶
Return the resource for an object.
- Parameters
obj (object) – The object whose model has a resource associated.
- Returns
The resource associated with the object, or
None
if not found.- Return type
- get_resource_from_name(name)[source]¶
Return the resource of the specified name.
- Parameters
name (unicode) – The name of the resource.
- Returns
The resource instance, or
None
if not found.- Return type
- get_resource_from_class(klass)[source]¶
Return the resource with the specified resource class.
- Parameters
klass (type) – The
WebAPIResource
subclass.- Returns
The resource instance, or
None
if not found.- Return type
- unregister_resource(resource)[source]¶
Unregister a resource from the caches.
- Parameters
resource (djblets.webapi.resources.base.WebAPIResource) – The resource instance to unregister.