rest_framework.schemas.generators

generators.py # Top-down schema generation

See schemas.__init__.py for package overview.

Module Contents

common_path(paths)
get_pk_name(model)
is_api_view(callback)

Return True if the given view callback is a REST framework view/viewset.

INSERT_INTO_COLLISION_FMT =

Schema Naming Collision.

coreapi.Link for URL path {value_url} cannot be inserted into schema. Position conflicts with coreapi.Link for URL path {target_url}.

Attempted to insert link with keys: {keys}.

Adjust URLs to avoid naming collision or override SchemaGenerator.get_keys() to customise schema structure.

class LinkNode

Bases:collections.OrderedDict

get_available_key(self, preferred_key)
insert_into(target, keys, value)

Nested dictionary insertion.

>>> example = {}
>>> insert_into(example, ['a', 'b', 'c'], 123)
>>> example
LinkNode({'a': LinkNode({'b': LinkNode({'c': LinkNode(links=[123])}}})))
is_custom_action(action)
endpoint_ordering(endpoint)
_PATH_PARAMETER_COMPONENT_RE
class EndpointEnumerator(patterns=None, urlconf=None)

Bases:object

A class to determine the available API endpoints that a project exposes.

get_api_endpoints(self, patterns=None, prefix='')

Return a list of all available API endpoints by inspecting the URL conf.

get_path_from_regex(self, path_regex)

Given a URL conf regex, return a URI template string.

should_include_endpoint(self, path, callback)

Return True if the given endpoint should be included.

get_allowed_methods(self, callback)

Return a list of the valid HTTP methods for this endpoint.

class SchemaGenerator(title=None, url=None, description=None, patterns=None, urlconf=None)

Bases:object

default_mapping
endpoint_inspector_cls
coerce_method_names
coerce_path_pk
get_schema(self, request=None, public=False)

Generate a coreapi.Document representing the API schema.

Return a dictionary containing all the links that should be included in the API schema.

determine_path_prefix(self, paths)

Given a list of all paths, return the common prefix which should be discounted when generating a schema structure.

This will be the longest common string that does not include that last component of the URL, or the last component before a path parameter.

For example:

/api/v1/users/ /api/v1/users/{pk}/

The path prefix is ‘/api/v1/’

create_view(self, callback, method, request=None)

Given a callback, return an actual view instance.

has_view_permissions(self, path, method, view)

Return True if the incoming request has the correct view permissions.

coerce_path(self, path, method, view)

Coerce {pk} path arguments into the name of the model field, where possible. This is cleaner for an external representation. (Ie. “this is an identifier”, not “this is a database primary key”)

get_keys(self, subpath, method, view)

Return a list of keys that should be used to layout a link within the schema document.

/users/ (“users”, “list”), (“users”, “create”) /users/{pk}/ (“users”, “read”), (“users”, “update”), (“users”, “delete”) /users/enabled/ (“users”, “enabled”) # custom viewset list action /users/{pk}/star/ (“users”, “star”) # custom viewset detail action /users/{pk}/groups/ (“users”, “groups”, “list”), (“users”, “groups”, “create”) /users/{pk}/groups/{pk}/ (“users”, “groups”, “read”), (“users”, “groups”, “update”), (“users”, “groups”, “delete”)