Viewsets

Cliquet 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 Cliquet 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 cliquet.resource.register() class decorator:

from cliquet import resource


@resource.register(collection_methods=('GET',))
class Resource(resource.UserResource):
    mapping = BookmarkSchema()

Subclassing

In case this isn’t enough, the cliquet.resource.viewset.ViewSet class can be subclassed and specified during registration:

from cliquet 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.UserResource):
    mapping = BookmarkSchema()

ViewSet class

class cliquet.resource.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 “collection” or “record”.
  • resource_cls – the resource class.
  • method (str) – the HTTP method.
get_record_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 collections, this will be “collection_{method|lower}
  • For records, 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.