Viewsets¶
Kinto-Core maps URLs, endpoints and permissions to resources using ViewSets.
Since a resource defines two URLs with several HTTP methods, a view set can be considered as a set of rules for registring the resource views into the routing mechanism of Pyramid.
To use Kinto-Core in a basic fashion, there is no need to understand how viewsets work in full detail.
Override defaults¶
Viewsets defaults can be overriden by passing arguments to the
kinto.core.resource.register()
class decorator:
from kinto.core import resource
@resource.register(collection_methods=('GET',))
class Bookmark(resource.Resource):
schema = BookmarkSchema
Subclassing¶
In case this isn’t enough, the kinto.core.resource.viewset.ViewSet
class
can be subclassed and specified during registration:
from kinto.core import resource
class NoSchemaViewSet(resource.ViewSet):
def get_record_schema(self, resource_cls, method):
simple_mapping = colander.MappingSchema(unknown='preserve')
return simple_mapping
@resource.register(viewset=NoSchemaViewSet())
class Resource(resource.Resource):
schema = BookmarkSchema
ViewSet class¶
- class kinto.core.resource.viewset.ViewSet(**kwargs)¶
The default ViewSet object.
A viewset contains all the information needed to register any resource in the Cornice registry.
It provides the same features as
cornice.resource()
, except that it is much more flexible and extensible.- update(**kwargs)¶
Update viewset attributes with provided values.
- get_view_arguments(endpoint_type, resource_cls, method)¶
Return the Pyramid/Cornice view arguments for the given endpoint type and method.
- Parameters
endpoint_type (str) – either “plural” or “object”.
resource_cls – the resource class.
method (str) – the HTTP method.
- get_object_schema(resource_cls, method)¶
Return the Cornice schema for the given method.
- get_view(endpoint_type, method)¶
Return the view method name located on the resource object, for the given type and method.
For plural, this will be “plural_{method|lower}
For objects, this will be “{method|lower}.
- get_name(resource_cls)¶
Returns the name of the resource.
- get_service_name(endpoint_type, resource_cls)¶
Returns the name of the service, depending a given type and resource.
- is_endpoint_enabled(endpoint_type, resource_name, method, settings)¶
Returns if the given endpoint is enabled or not.
Uses the settings to tell so.