2013-04-25 01:40:24 +04:00
|
|
|
"""
|
|
|
|
Routers provide a convenient and consistent way of automatically
|
|
|
|
determining the URL conf for your API.
|
|
|
|
|
|
|
|
They are used by simply instantiating a Router class, and then registering
|
|
|
|
all the required ViewSets with that router.
|
|
|
|
|
|
|
|
For example, you might have a `urls.py` that looks something like this:
|
|
|
|
|
|
|
|
router = routers.DefaultRouter()
|
|
|
|
router.register('users', UserViewSet, 'user')
|
|
|
|
router.register('accounts', AccountViewSet, 'account')
|
|
|
|
|
|
|
|
urlpatterns = router.urls
|
|
|
|
"""
|
2013-04-29 15:45:00 +04:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-06-27 02:00:42 +04:00
|
|
|
import itertools
|
2016-10-10 15:03:46 +03:00
|
|
|
import warnings
|
2015-09-22 17:35:38 +03:00
|
|
|
from collections import OrderedDict, namedtuple
|
2015-06-18 16:38:29 +03:00
|
|
|
|
2015-06-11 01:45:23 +03:00
|
|
|
from django.conf.urls import url
|
2015-06-18 16:38:29 +03:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
|
2016-07-04 18:38:17 +03:00
|
|
|
from rest_framework import exceptions, renderers, views
|
2016-10-10 15:03:46 +03:00
|
|
|
from rest_framework.compat import NoReverseMatch
|
|
|
|
from rest_framework.renderers import BrowsableAPIRenderer
|
2015-06-18 16:38:29 +03:00
|
|
|
from rest_framework.response import Response
|
2015-06-25 23:55:51 +03:00
|
|
|
from rest_framework.reverse import reverse
|
2016-07-04 18:38:17 +03:00
|
|
|
from rest_framework.schemas import SchemaGenerator
|
|
|
|
from rest_framework.settings import api_settings
|
2013-04-25 01:40:24 +04:00
|
|
|
from rest_framework.urlpatterns import format_suffix_patterns
|
2013-04-04 23:00:44 +04:00
|
|
|
|
2013-07-13 19:12:59 +04:00
|
|
|
Route = namedtuple('Route', ['url', 'mapping', 'name', 'initkwargs'])
|
|
|
|
DynamicDetailRoute = namedtuple('DynamicDetailRoute', ['url', 'name', 'initkwargs'])
|
|
|
|
DynamicListRoute = namedtuple('DynamicListRoute', ['url', 'name', 'initkwargs'])
|
2013-04-26 17:59:21 +04:00
|
|
|
|
|
|
|
|
2013-04-25 20:41:47 +04:00
|
|
|
def replace_methodname(format_string, methodname):
|
|
|
|
"""
|
|
|
|
Partially format a format_string, swapping out any
|
2013-04-26 18:09:55 +04:00
|
|
|
'{methodname}' or '{methodnamehyphen}' components.
|
2013-04-25 20:41:47 +04:00
|
|
|
"""
|
|
|
|
methodnamehyphen = methodname.replace('_', '-')
|
|
|
|
ret = format_string
|
|
|
|
ret = ret.replace('{methodname}', methodname)
|
|
|
|
ret = ret.replace('{methodnamehyphen}', methodnamehyphen)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2013-06-27 02:00:42 +04:00
|
|
|
def flatten(list_of_lists):
|
|
|
|
"""
|
|
|
|
Takes an iterable of iterables, returns a single iterable containing all items
|
|
|
|
"""
|
|
|
|
return itertools.chain(*list_of_lists)
|
|
|
|
|
|
|
|
|
2013-04-04 23:00:44 +04:00
|
|
|
class BaseRouter(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.registry = []
|
|
|
|
|
2013-05-02 15:07:37 +04:00
|
|
|
def register(self, prefix, viewset, base_name=None):
|
|
|
|
if base_name is None:
|
|
|
|
base_name = self.get_default_base_name(viewset)
|
|
|
|
self.registry.append((prefix, viewset, base_name))
|
|
|
|
|
|
|
|
def get_default_base_name(self, viewset):
|
|
|
|
"""
|
|
|
|
If `base_name` is not specified, attempt to automatically determine
|
|
|
|
it from the viewset.
|
|
|
|
"""
|
2015-01-19 17:48:13 +03:00
|
|
|
raise NotImplementedError('get_default_base_name must be overridden')
|
2013-04-04 23:00:44 +04:00
|
|
|
|
2013-04-25 01:40:24 +04:00
|
|
|
def get_urls(self):
|
2013-05-02 15:07:37 +04:00
|
|
|
"""
|
|
|
|
Return a list of URL patterns, given the registered viewsets.
|
|
|
|
"""
|
2015-01-19 17:48:13 +03:00
|
|
|
raise NotImplementedError('get_urls must be overridden')
|
2013-04-04 23:00:44 +04:00
|
|
|
|
|
|
|
@property
|
2013-04-25 01:40:24 +04:00
|
|
|
def urls(self):
|
|
|
|
if not hasattr(self, '_urls'):
|
2015-06-11 01:45:23 +03:00
|
|
|
self._urls = self.get_urls()
|
2013-04-25 01:40:24 +04:00
|
|
|
return self._urls
|
|
|
|
|
|
|
|
|
|
|
|
class SimpleRouter(BaseRouter):
|
2016-10-10 15:03:46 +03:00
|
|
|
|
2013-04-25 01:40:24 +04:00
|
|
|
routes = [
|
|
|
|
# List route.
|
2013-04-26 17:59:21 +04:00
|
|
|
Route(
|
2013-06-04 23:59:12 +04:00
|
|
|
url=r'^{prefix}{trailing_slash}$',
|
2013-04-26 17:59:21 +04:00
|
|
|
mapping={
|
2013-04-25 01:40:24 +04:00
|
|
|
'get': 'list',
|
|
|
|
'post': 'create'
|
|
|
|
},
|
2013-04-26 17:59:21 +04:00
|
|
|
name='{basename}-list',
|
|
|
|
initkwargs={'suffix': 'List'}
|
2013-04-25 01:40:24 +04:00
|
|
|
),
|
2013-07-13 19:11:53 +04:00
|
|
|
# Dynamically generated list routes.
|
2013-07-16 02:35:13 +04:00
|
|
|
# Generated using @list_route decorator
|
2013-06-06 01:39:14 +04:00
|
|
|
# on methods of the viewset.
|
2013-07-13 19:12:59 +04:00
|
|
|
DynamicListRoute(
|
2013-06-06 01:39:14 +04:00
|
|
|
url=r'^{prefix}/{methodname}{trailing_slash}$',
|
2013-06-16 20:43:59 +04:00
|
|
|
name='{basename}-{methodnamehyphen}',
|
2013-06-06 01:39:14 +04:00
|
|
|
initkwargs={}
|
|
|
|
),
|
2013-04-25 01:40:24 +04:00
|
|
|
# Detail route.
|
2013-04-26 17:59:21 +04:00
|
|
|
Route(
|
2013-06-04 23:59:12 +04:00
|
|
|
url=r'^{prefix}/{lookup}{trailing_slash}$',
|
2013-04-26 17:59:21 +04:00
|
|
|
mapping={
|
2013-04-25 01:40:24 +04:00
|
|
|
'get': 'retrieve',
|
|
|
|
'put': 'update',
|
|
|
|
'patch': 'partial_update',
|
|
|
|
'delete': 'destroy'
|
|
|
|
},
|
2013-04-26 17:59:21 +04:00
|
|
|
name='{basename}-detail',
|
|
|
|
initkwargs={'suffix': 'Instance'}
|
2013-04-25 01:40:24 +04:00
|
|
|
),
|
2013-07-13 19:11:53 +04:00
|
|
|
# Dynamically generated detail routes.
|
2013-07-16 02:35:13 +04:00
|
|
|
# Generated using @detail_route decorator on methods of the viewset.
|
2013-07-13 19:12:59 +04:00
|
|
|
DynamicDetailRoute(
|
2013-06-04 23:59:12 +04:00
|
|
|
url=r'^{prefix}/{lookup}/{methodname}{trailing_slash}$',
|
2013-06-16 20:43:59 +04:00
|
|
|
name='{basename}-{methodnamehyphen}',
|
2013-04-26 17:59:21 +04:00
|
|
|
initkwargs={}
|
2013-04-25 01:40:24 +04:00
|
|
|
),
|
2013-04-04 23:00:44 +04:00
|
|
|
]
|
|
|
|
|
2013-06-04 23:59:12 +04:00
|
|
|
def __init__(self, trailing_slash=True):
|
|
|
|
self.trailing_slash = trailing_slash and '/' or ''
|
|
|
|
super(SimpleRouter, self).__init__()
|
|
|
|
|
2013-05-02 15:07:37 +04:00
|
|
|
def get_default_base_name(self, viewset):
|
|
|
|
"""
|
|
|
|
If `base_name` is not specified, attempt to automatically determine
|
|
|
|
it from the viewset.
|
|
|
|
"""
|
|
|
|
queryset = getattr(viewset, 'queryset', None)
|
|
|
|
|
2015-01-29 19:28:03 +03:00
|
|
|
assert queryset is not None, '`base_name` argument not specified, and could ' \
|
2013-05-02 15:07:37 +04:00
|
|
|
'not automatically determine the name from the viewset, as ' \
|
2014-08-20 20:15:46 +04:00
|
|
|
'it does not have a `.queryset` attribute.'
|
2013-05-02 15:07:37 +04:00
|
|
|
|
2015-01-29 19:28:03 +03:00
|
|
|
return queryset.model._meta.object_name.lower()
|
2013-05-02 15:07:37 +04:00
|
|
|
|
2013-04-25 01:40:24 +04:00
|
|
|
def get_routes(self, viewset):
|
|
|
|
"""
|
|
|
|
Augment `self.routes` with any dynamically generated routes.
|
|
|
|
|
2013-04-26 17:59:21 +04:00
|
|
|
Returns a list of the Route namedtuple.
|
2013-04-25 01:40:24 +04:00
|
|
|
"""
|
2016-06-29 19:27:17 +03:00
|
|
|
# converting to list as iterables are good for one pass, known host needs to be checked again and again for
|
|
|
|
# different functions.
|
|
|
|
known_actions = list(flatten([route.mapping.values() for route in self.routes if isinstance(route, Route)]))
|
2013-06-27 02:00:42 +04:00
|
|
|
|
2013-07-16 02:35:13 +04:00
|
|
|
# Determine any `@detail_route` or `@list_route` decorated methods on the viewset
|
2013-07-13 19:11:53 +04:00
|
|
|
detail_routes = []
|
|
|
|
list_routes = []
|
2013-04-25 01:40:24 +04:00
|
|
|
for methodname in dir(viewset):
|
|
|
|
attr = getattr(viewset, methodname)
|
2013-05-25 02:28:47 +04:00
|
|
|
httpmethods = getattr(attr, 'bind_to_methods', None)
|
2013-07-13 19:11:53 +04:00
|
|
|
detail = getattr(attr, 'detail', True)
|
2013-05-25 02:28:47 +04:00
|
|
|
if httpmethods:
|
2016-06-29 19:27:17 +03:00
|
|
|
# checking method names against the known actions list
|
2013-06-27 02:00:42 +04:00
|
|
|
if methodname in known_actions:
|
2013-07-16 02:35:13 +04:00
|
|
|
raise ImproperlyConfigured('Cannot use @detail_route or @list_route '
|
|
|
|
'decorators on method "%s" '
|
2013-07-13 19:11:53 +04:00
|
|
|
'as it is an existing route' % methodname)
|
2013-06-21 19:28:17 +04:00
|
|
|
httpmethods = [method.lower() for method in httpmethods]
|
2013-07-13 19:11:53 +04:00
|
|
|
if detail:
|
|
|
|
detail_routes.append((httpmethods, methodname))
|
2013-06-06 01:39:14 +04:00
|
|
|
else:
|
2013-07-13 19:11:53 +04:00
|
|
|
list_routes.append((httpmethods, methodname))
|
2013-04-25 01:40:24 +04:00
|
|
|
|
2015-02-25 15:29:07 +03:00
|
|
|
def _get_dynamic_routes(route, dynamic_routes):
|
|
|
|
ret = []
|
|
|
|
for httpmethods, methodname in dynamic_routes:
|
|
|
|
method_kwargs = getattr(viewset, methodname).kwargs
|
|
|
|
initkwargs = route.initkwargs.copy()
|
|
|
|
initkwargs.update(method_kwargs)
|
|
|
|
url_path = initkwargs.pop("url_path", None) or methodname
|
|
|
|
ret.append(Route(
|
|
|
|
url=replace_methodname(route.url, url_path),
|
2015-10-17 12:00:11 +03:00
|
|
|
mapping={httpmethod: methodname for httpmethod in httpmethods},
|
2015-02-25 15:29:07 +03:00
|
|
|
name=replace_methodname(route.name, url_path),
|
|
|
|
initkwargs=initkwargs,
|
|
|
|
))
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
2013-04-04 23:00:44 +04:00
|
|
|
ret = []
|
2013-04-26 17:59:21 +04:00
|
|
|
for route in self.routes:
|
2013-07-13 19:12:59 +04:00
|
|
|
if isinstance(route, DynamicDetailRoute):
|
2013-07-16 02:35:13 +04:00
|
|
|
# Dynamic detail routes (@detail_route decorator)
|
2015-02-25 15:29:07 +03:00
|
|
|
ret += _get_dynamic_routes(route, detail_routes)
|
2013-07-13 19:12:59 +04:00
|
|
|
elif isinstance(route, DynamicListRoute):
|
2013-07-16 02:35:13 +04:00
|
|
|
# Dynamic list routes (@list_route decorator)
|
2015-02-25 15:29:07 +03:00
|
|
|
ret += _get_dynamic_routes(route, list_routes)
|
2013-04-25 01:40:24 +04:00
|
|
|
else:
|
|
|
|
# Standard route
|
2013-04-26 17:59:21 +04:00
|
|
|
ret.append(route)
|
2013-04-05 00:42:26 +04:00
|
|
|
|
2013-04-25 01:40:24 +04:00
|
|
|
return ret
|
2013-04-04 23:00:44 +04:00
|
|
|
|
2013-04-25 01:40:24 +04:00
|
|
|
def get_method_map(self, viewset, method_map):
|
|
|
|
"""
|
|
|
|
Given a viewset, and a mapping of http methods to actions,
|
|
|
|
return a new mapping which only includes any mappings that
|
|
|
|
are actually implemented by the viewset.
|
|
|
|
"""
|
|
|
|
bound_methods = {}
|
|
|
|
for method, action in method_map.items():
|
|
|
|
if hasattr(viewset, action):
|
|
|
|
bound_methods[method] = action
|
|
|
|
return bound_methods
|
|
|
|
|
2013-12-10 23:14:17 +04:00
|
|
|
def get_lookup_regex(self, viewset, lookup_prefix=''):
|
2013-04-25 01:40:24 +04:00
|
|
|
"""
|
|
|
|
Given a viewset, return the portion of URL regex that is used
|
|
|
|
to match against a single instance.
|
2013-12-14 00:22:56 +04:00
|
|
|
|
|
|
|
Note that lookup_prefix is not used directly inside REST rest_framework
|
|
|
|
itself, but is required in order to nicely support nested router
|
|
|
|
implementations, such as drf-nested-routers.
|
|
|
|
|
|
|
|
https://github.com/alanjds/drf-nested-routers
|
2013-04-25 01:40:24 +04:00
|
|
|
"""
|
2015-03-13 03:07:20 +03:00
|
|
|
base_regex = '(?P<{lookup_prefix}{lookup_url_kwarg}>{lookup_value})'
|
2014-01-13 19:37:52 +04:00
|
|
|
# Use `pk` as default field, unset set. Default regex should not
|
|
|
|
# consume `.json` style suffixes and should break at '/' boundaries.
|
2013-04-25 01:40:24 +04:00
|
|
|
lookup_field = getattr(viewset, 'lookup_field', 'pk')
|
2015-03-13 03:07:20 +03:00
|
|
|
lookup_url_kwarg = getattr(viewset, 'lookup_url_kwarg', None) or lookup_field
|
2014-01-13 19:37:52 +04:00
|
|
|
lookup_value = getattr(viewset, 'lookup_value_regex', '[^/.]+')
|
2014-01-03 02:44:47 +04:00
|
|
|
return base_regex.format(
|
|
|
|
lookup_prefix=lookup_prefix,
|
2015-03-13 03:07:20 +03:00
|
|
|
lookup_url_kwarg=lookup_url_kwarg,
|
2014-01-03 02:44:47 +04:00
|
|
|
lookup_value=lookup_value
|
|
|
|
)
|
2013-04-25 01:40:24 +04:00
|
|
|
|
|
|
|
def get_urls(self):
|
|
|
|
"""
|
|
|
|
Use the registered viewsets to generate a list of URL patterns.
|
|
|
|
"""
|
|
|
|
ret = []
|
2013-04-04 23:00:44 +04:00
|
|
|
|
2013-04-25 01:40:24 +04:00
|
|
|
for prefix, viewset, basename in self.registry:
|
|
|
|
lookup = self.get_lookup_regex(viewset)
|
|
|
|
routes = self.get_routes(viewset)
|
2013-04-04 23:35:40 +04:00
|
|
|
|
2013-04-26 17:59:21 +04:00
|
|
|
for route in routes:
|
2013-04-04 23:38:42 +04:00
|
|
|
|
2013-04-25 01:40:24 +04:00
|
|
|
# Only actions which actually exist on the viewset will be bound
|
2013-04-26 17:59:21 +04:00
|
|
|
mapping = self.get_method_map(viewset, route.mapping)
|
|
|
|
if not mapping:
|
2013-04-04 23:35:40 +04:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Build the url pattern
|
2013-06-04 23:59:12 +04:00
|
|
|
regex = route.url.format(
|
|
|
|
prefix=prefix,
|
|
|
|
lookup=lookup,
|
|
|
|
trailing_slash=self.trailing_slash
|
|
|
|
)
|
2016-07-04 18:38:17 +03:00
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
# If there is no prefix, the first part of the url is probably
|
|
|
|
# controlled by project's urls.py and the router is in an app,
|
|
|
|
# so a slash in the beginning will (A) cause Django to give
|
|
|
|
# warnings and (B) generate URLS that will require using '//'.
|
|
|
|
if not prefix and regex[:2] == '^/':
|
|
|
|
regex = '^' + regex[2:]
|
|
|
|
|
2013-04-26 17:59:21 +04:00
|
|
|
view = viewset.as_view(mapping, **route.initkwargs)
|
|
|
|
name = route.name.format(basename=basename)
|
2013-04-04 23:35:40 +04:00
|
|
|
ret.append(url(regex, view, name=name))
|
|
|
|
|
2013-04-04 23:00:44 +04:00
|
|
|
return ret
|
2013-04-25 01:40:24 +04:00
|
|
|
|
|
|
|
|
|
|
|
class DefaultRouter(SimpleRouter):
|
|
|
|
"""
|
|
|
|
The default router extends the SimpleRouter, but also adds in a default
|
|
|
|
API root view, and adds format suffix patterns to the URLs.
|
|
|
|
"""
|
|
|
|
include_root_view = True
|
|
|
|
include_format_suffixes = True
|
2013-06-08 06:49:18 +04:00
|
|
|
root_view_name = 'api-root'
|
2016-10-10 15:03:46 +03:00
|
|
|
default_schema_renderers = [renderers.CoreJSONRenderer, BrowsableAPIRenderer]
|
2016-07-04 18:38:17 +03:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2016-10-10 15:03:46 +03:00
|
|
|
if 'schema_title' in kwargs:
|
|
|
|
warnings.warn(
|
|
|
|
"Including a schema directly via a router is now pending "
|
|
|
|
"deprecation. Use `get_schema_view()` instead.",
|
|
|
|
PendingDeprecationWarning
|
|
|
|
)
|
2016-07-27 13:49:01 +03:00
|
|
|
if 'schema_renderers' in kwargs:
|
|
|
|
assert 'schema_title' in kwargs, 'Missing "schema_title" argument.'
|
2016-07-28 14:08:34 +03:00
|
|
|
if 'schema_url' in kwargs:
|
|
|
|
assert 'schema_title' in kwargs, 'Missing "schema_title" argument.'
|
2016-07-04 18:38:17 +03:00
|
|
|
self.schema_title = kwargs.pop('schema_title', None)
|
2016-07-28 14:08:34 +03:00
|
|
|
self.schema_url = kwargs.pop('schema_url', None)
|
2016-07-13 18:30:41 +03:00
|
|
|
self.schema_renderers = kwargs.pop('schema_renderers', self.default_schema_renderers)
|
2016-07-28 14:50:51 +03:00
|
|
|
if 'root_renderers' in kwargs:
|
|
|
|
self.root_renderers = kwargs.pop('root_renderers')
|
|
|
|
else:
|
|
|
|
self.root_renderers = list(api_settings.DEFAULT_RENDERER_CLASSES)
|
2016-07-04 18:38:17 +03:00
|
|
|
super(DefaultRouter, self).__init__(*args, **kwargs)
|
2013-04-25 01:40:24 +04:00
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
def get_schema_root_view(self, api_urls=None):
|
|
|
|
"""
|
|
|
|
Return a schema root view.
|
|
|
|
"""
|
|
|
|
schema_renderers = self.schema_renderers
|
|
|
|
schema_generator = SchemaGenerator(
|
|
|
|
title=self.schema_title,
|
|
|
|
url=self.schema_url,
|
|
|
|
patterns=api_urls
|
|
|
|
)
|
|
|
|
|
|
|
|
class APISchemaView(views.APIView):
|
|
|
|
_ignore_model_permissions = True
|
|
|
|
exclude_from_schema = True
|
|
|
|
renderer_classes = schema_renderers
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
schema = schema_generator.get_schema(request)
|
|
|
|
if schema is None:
|
|
|
|
raise exceptions.PermissionDenied()
|
|
|
|
return Response(schema)
|
|
|
|
|
|
|
|
return APISchemaView.as_view()
|
|
|
|
|
2016-07-28 14:08:34 +03:00
|
|
|
def get_api_root_view(self, api_urls=None):
|
2013-04-25 01:40:24 +04:00
|
|
|
"""
|
2016-10-10 15:03:46 +03:00
|
|
|
Return a basic root view.
|
2013-04-25 01:40:24 +04:00
|
|
|
"""
|
2014-11-06 15:00:30 +03:00
|
|
|
api_root_dict = OrderedDict()
|
2013-04-26 17:59:21 +04:00
|
|
|
list_name = self.routes[0].name
|
2013-04-25 01:40:24 +04:00
|
|
|
for prefix, viewset, basename in self.registry:
|
|
|
|
api_root_dict[prefix] = list_name.format(basename=basename)
|
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
class APIRootView(views.APIView):
|
2013-05-16 18:08:12 +04:00
|
|
|
_ignore_model_permissions = True
|
2016-10-10 15:03:46 +03:00
|
|
|
exclude_from_schema = True
|
2013-04-25 01:40:24 +04:00
|
|
|
|
2014-09-09 04:12:52 +04:00
|
|
|
def get(self, request, *args, **kwargs):
|
2016-07-04 18:38:17 +03:00
|
|
|
# Return a plain {"name": "hyperlink"} response.
|
2014-11-06 15:00:30 +03:00
|
|
|
ret = OrderedDict()
|
2015-08-07 00:51:35 +03:00
|
|
|
namespace = request.resolver_match.namespace
|
2013-05-16 18:08:12 +04:00
|
|
|
for key, url_name in api_root_dict.items():
|
2014-12-28 15:02:52 +03:00
|
|
|
if namespace:
|
|
|
|
url_name = namespace + ':' + url_name
|
2014-09-11 14:42:36 +04:00
|
|
|
try:
|
|
|
|
ret[key] = reverse(
|
|
|
|
url_name,
|
2015-07-29 15:43:25 +03:00
|
|
|
args=args,
|
2015-07-28 16:55:49 +03:00
|
|
|
kwargs=kwargs,
|
2014-09-11 14:42:36 +04:00
|
|
|
request=request,
|
|
|
|
format=kwargs.get('format', None)
|
|
|
|
)
|
|
|
|
except NoReverseMatch:
|
2014-09-17 18:51:17 +04:00
|
|
|
# Don't bail out if eg. no list routes exist, only detail routes.
|
2014-09-11 14:42:36 +04:00
|
|
|
continue
|
|
|
|
|
2013-05-16 18:08:12 +04:00
|
|
|
return Response(ret)
|
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
return APIRootView.as_view()
|
2013-04-25 01:40:24 +04:00
|
|
|
|
|
|
|
def get_urls(self):
|
|
|
|
"""
|
|
|
|
Generate the list of URL patterns, including a default root view
|
|
|
|
for the API, and appending `.json` style format suffixes.
|
|
|
|
"""
|
2016-07-04 18:38:17 +03:00
|
|
|
urls = super(DefaultRouter, self).get_urls()
|
2013-04-25 01:40:24 +04:00
|
|
|
|
|
|
|
if self.include_root_view:
|
2016-10-10 15:03:46 +03:00
|
|
|
if self.schema_title:
|
|
|
|
view = self.get_schema_root_view(api_urls=urls)
|
|
|
|
else:
|
|
|
|
view = self.get_api_root_view(api_urls=urls)
|
2016-07-04 18:38:17 +03:00
|
|
|
root_url = url(r'^$', view, name=self.root_view_name)
|
2013-04-25 01:40:24 +04:00
|
|
|
urls.append(root_url)
|
|
|
|
|
|
|
|
if self.include_format_suffixes:
|
|
|
|
urls = format_suffix_patterns(urls)
|
|
|
|
|
|
|
|
return urls
|