2017-09-14 11:46:34 +03:00
|
|
|
"""
|
|
|
|
generators.py # Top-down schema generation
|
|
|
|
|
|
|
|
See schemas.__init__.py for package overview.
|
|
|
|
"""
|
2017-12-20 11:10:28 +03:00
|
|
|
import re
|
2017-10-16 12:32:48 +03:00
|
|
|
from collections import Counter, OrderedDict
|
2016-07-04 18:38:17 +03:00
|
|
|
from importlib import import_module
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.admindocs.views import simplify_regex
|
2016-11-02 12:04:01 +03:00
|
|
|
from django.core.exceptions import PermissionDenied
|
|
|
|
from django.http import Http404
|
2016-07-04 18:38:17 +03:00
|
|
|
from django.utils import six
|
|
|
|
|
2017-09-14 11:46:34 +03:00
|
|
|
from rest_framework import exceptions
|
2016-10-10 15:03:46 +03:00
|
|
|
from rest_framework.compat import (
|
2017-12-20 15:17:54 +03:00
|
|
|
URLPattern, URLResolver, coreapi, coreschema, get_original_route
|
2016-10-10 15:03:46 +03:00
|
|
|
)
|
2016-07-04 18:38:17 +03:00
|
|
|
from rest_framework.request import clone_request
|
2016-10-10 15:03:46 +03:00
|
|
|
from rest_framework.settings import api_settings
|
|
|
|
from rest_framework.utils.model_meta import _get_pk
|
2017-03-03 18:24:37 +03:00
|
|
|
|
2017-09-14 11:46:34 +03:00
|
|
|
from .utils import is_list_view
|
2016-10-10 15:03:46 +03:00
|
|
|
|
|
|
|
|
2016-10-21 18:59:34 +03:00
|
|
|
def common_path(paths):
|
|
|
|
split_paths = [path.strip('/').split('/') for path in paths]
|
|
|
|
s1 = min(split_paths)
|
|
|
|
s2 = max(split_paths)
|
|
|
|
common = s1
|
|
|
|
for i, c in enumerate(s1):
|
|
|
|
if c != s2[i]:
|
|
|
|
common = s1[:i]
|
|
|
|
break
|
|
|
|
return '/' + '/'.join(common)
|
|
|
|
|
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
def get_pk_name(model):
|
|
|
|
meta = model._meta.concrete_model._meta
|
|
|
|
return _get_pk(meta).name
|
2016-07-04 18:38:17 +03:00
|
|
|
|
|
|
|
|
|
|
|
def is_api_view(callback):
|
|
|
|
"""
|
|
|
|
Return `True` if the given view callback is a REST framework view/viewset.
|
|
|
|
"""
|
2017-09-14 11:46:34 +03:00
|
|
|
# Avoid import cycle on APIView
|
|
|
|
from rest_framework.views import APIView
|
2016-07-04 18:38:17 +03:00
|
|
|
cls = getattr(callback, 'cls', None)
|
|
|
|
return (cls is not None) and issubclass(cls, APIView)
|
|
|
|
|
|
|
|
|
2017-10-05 12:06:09 +03:00
|
|
|
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}.
|
|
|
|
|
|
|
|
Attemped to insert link with keys: {keys}.
|
|
|
|
|
|
|
|
Adjust URLs to avoid naming collision or override `SchemaGenerator.get_keys()`
|
|
|
|
to customise schema structure.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2017-10-16 12:32:48 +03:00
|
|
|
class LinkNode(OrderedDict):
|
|
|
|
def __init__(self):
|
|
|
|
self.links = []
|
|
|
|
self.methods_counter = Counter()
|
|
|
|
super(LinkNode, self).__init__()
|
|
|
|
|
|
|
|
def get_available_key(self, preferred_key):
|
|
|
|
if preferred_key not in self:
|
|
|
|
return preferred_key
|
|
|
|
|
|
|
|
while True:
|
|
|
|
current_val = self.methods_counter[preferred_key]
|
|
|
|
self.methods_counter[preferred_key] += 1
|
|
|
|
|
|
|
|
key = '{}_{}'.format(preferred_key, current_val)
|
|
|
|
if key not in self:
|
|
|
|
return key
|
|
|
|
|
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
def insert_into(target, keys, value):
|
|
|
|
"""
|
|
|
|
Nested dictionary insertion.
|
2016-07-04 18:38:17 +03:00
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
>>> example = {}
|
|
|
|
>>> insert_into(example, ['a', 'b', 'c'], 123)
|
|
|
|
>>> example
|
2017-10-16 12:32:48 +03:00
|
|
|
LinkNode({'a': LinkNode({'b': LinkNode({'c': LinkNode(links=[123])}}})))
|
2016-10-10 15:03:46 +03:00
|
|
|
"""
|
|
|
|
for key in keys[:-1]:
|
|
|
|
if key not in target:
|
2017-10-16 12:32:48 +03:00
|
|
|
target[key] = LinkNode()
|
2016-10-10 15:03:46 +03:00
|
|
|
target = target[key]
|
2017-10-16 12:32:48 +03:00
|
|
|
|
2017-10-05 12:06:09 +03:00
|
|
|
try:
|
2017-10-16 12:32:48 +03:00
|
|
|
target.links.append((keys[-1], value))
|
2017-10-05 12:06:09 +03:00
|
|
|
except TypeError:
|
|
|
|
msg = INSERT_INTO_COLLISION_FMT.format(
|
|
|
|
value_url=value.url,
|
|
|
|
target_url=target.url,
|
|
|
|
keys=keys
|
|
|
|
)
|
|
|
|
raise ValueError(msg)
|
2016-07-28 14:08:34 +03:00
|
|
|
|
2016-07-04 18:38:17 +03:00
|
|
|
|
2017-10-16 12:32:48 +03:00
|
|
|
def distribute_links(obj):
|
|
|
|
for key, value in obj.items():
|
|
|
|
distribute_links(value)
|
|
|
|
|
|
|
|
for preferred_key, link in obj.links:
|
|
|
|
key = obj.get_available_key(preferred_key)
|
|
|
|
obj[key] = link
|
|
|
|
|
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
def is_custom_action(action):
|
2017-11-06 12:03:01 +03:00
|
|
|
return action not in {
|
2016-10-10 15:03:46 +03:00
|
|
|
'retrieve', 'list', 'create', 'update', 'partial_update', 'destroy'
|
2017-11-06 12:03:01 +03:00
|
|
|
}
|
2016-08-11 13:27:28 +03:00
|
|
|
|
2016-07-04 18:38:17 +03:00
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
def endpoint_ordering(endpoint):
|
|
|
|
path, method, callback = endpoint
|
|
|
|
method_priority = {
|
|
|
|
'GET': 0,
|
|
|
|
'POST': 1,
|
|
|
|
'PUT': 2,
|
|
|
|
'PATCH': 3,
|
|
|
|
'DELETE': 4
|
|
|
|
}.get(method, 5)
|
|
|
|
return (path, method_priority)
|
|
|
|
|
|
|
|
|
2017-12-20 11:10:28 +03:00
|
|
|
_PATH_PARAMETER_COMPONENT_RE = re.compile(
|
|
|
|
r'<(?:(?P<converter>[^>:]+):)?(?P<parameter>\w+)>'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-09-14 11:46:34 +03:00
|
|
|
class EndpointEnumerator(object):
|
2016-10-10 15:03:46 +03:00
|
|
|
"""
|
|
|
|
A class to determine the available API endpoints that a project exposes.
|
|
|
|
"""
|
|
|
|
def __init__(self, patterns=None, urlconf=None):
|
|
|
|
if patterns is None:
|
|
|
|
if urlconf is None:
|
|
|
|
# Use the default Django URL conf
|
|
|
|
urlconf = settings.ROOT_URLCONF
|
2016-07-04 18:38:17 +03:00
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
# Load the given URLconf module
|
|
|
|
if isinstance(urlconf, six.string_types):
|
|
|
|
urls = import_module(urlconf)
|
2016-08-11 16:07:40 +03:00
|
|
|
else:
|
2016-10-10 15:03:46 +03:00
|
|
|
urls = urlconf
|
|
|
|
patterns = urls.urlpatterns
|
2016-07-04 18:38:17 +03:00
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
self.patterns = patterns
|
2016-07-04 18:38:17 +03:00
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
def get_api_endpoints(self, patterns=None, prefix=''):
|
2016-07-04 18:38:17 +03:00
|
|
|
"""
|
|
|
|
Return a list of all available API endpoints by inspecting the URL conf.
|
|
|
|
"""
|
2016-10-10 15:03:46 +03:00
|
|
|
if patterns is None:
|
|
|
|
patterns = self.patterns
|
|
|
|
|
2016-07-04 18:38:17 +03:00
|
|
|
api_endpoints = []
|
|
|
|
|
|
|
|
for pattern in patterns:
|
2017-12-20 15:17:54 +03:00
|
|
|
path_regex = prefix + get_original_route(pattern)
|
2017-11-10 11:41:03 +03:00
|
|
|
if isinstance(pattern, URLPattern):
|
2016-10-10 15:03:46 +03:00
|
|
|
path = self.get_path_from_regex(path_regex)
|
2016-07-04 18:38:17 +03:00
|
|
|
callback = pattern.callback
|
|
|
|
if self.should_include_endpoint(path, callback):
|
|
|
|
for method in self.get_allowed_methods(callback):
|
2016-10-10 15:03:46 +03:00
|
|
|
endpoint = (path, method, callback)
|
2016-07-04 18:38:17 +03:00
|
|
|
api_endpoints.append(endpoint)
|
|
|
|
|
2017-11-10 11:41:03 +03:00
|
|
|
elif isinstance(pattern, URLResolver):
|
2016-07-04 18:38:17 +03:00
|
|
|
nested_endpoints = self.get_api_endpoints(
|
|
|
|
patterns=pattern.url_patterns,
|
|
|
|
prefix=path_regex
|
|
|
|
)
|
|
|
|
api_endpoints.extend(nested_endpoints)
|
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
api_endpoints = sorted(api_endpoints, key=endpoint_ordering)
|
|
|
|
|
2016-08-12 12:58:47 +03:00
|
|
|
return api_endpoints
|
2016-07-04 18:38:17 +03:00
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
def get_path_from_regex(self, path_regex):
|
2016-07-04 18:38:17 +03:00
|
|
|
"""
|
|
|
|
Given a URL conf regex, return a URI template string.
|
|
|
|
"""
|
|
|
|
path = simplify_regex(path_regex)
|
2017-12-20 11:10:28 +03:00
|
|
|
|
|
|
|
# Strip Django 2.0 convertors as they are incompatible with uritemplate format
|
|
|
|
path = re.sub(_PATH_PARAMETER_COMPONENT_RE, r'{\g<parameter>}', path)
|
2016-07-04 18:38:17 +03:00
|
|
|
return path
|
|
|
|
|
|
|
|
def should_include_endpoint(self, path, callback):
|
|
|
|
"""
|
|
|
|
Return `True` if the given endpoint should be included.
|
|
|
|
"""
|
|
|
|
if not is_api_view(callback):
|
|
|
|
return False # Ignore anything except REST framework views.
|
|
|
|
|
2017-09-20 12:29:47 +03:00
|
|
|
if callback.cls.schema is None:
|
|
|
|
return False
|
|
|
|
|
2018-07-06 11:35:36 +03:00
|
|
|
if 'schema' in callback.initkwargs:
|
|
|
|
if callback.initkwargs['schema'] is None:
|
|
|
|
return False
|
|
|
|
|
2016-07-04 18:38:17 +03:00
|
|
|
if path.endswith('.{format}') or path.endswith('.{format}/'):
|
|
|
|
return False # Ignore .json style URLs.
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_allowed_methods(self, callback):
|
|
|
|
"""
|
|
|
|
Return a list of the valid HTTP methods for this endpoint.
|
|
|
|
"""
|
|
|
|
if hasattr(callback, 'actions'):
|
2018-01-08 12:49:46 +03:00
|
|
|
actions = set(callback.actions)
|
2017-05-03 10:49:04 +03:00
|
|
|
http_method_names = set(callback.cls.http_method_names)
|
2017-10-25 11:56:40 +03:00
|
|
|
methods = [method.upper() for method in actions & http_method_names]
|
|
|
|
else:
|
|
|
|
methods = callback.cls().allowed_methods
|
2016-07-04 18:38:17 +03:00
|
|
|
|
2017-10-25 11:56:40 +03:00
|
|
|
return [method for method in methods if method not in ('OPTIONS', 'HEAD')]
|
2016-07-04 18:38:17 +03:00
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
|
|
|
|
class SchemaGenerator(object):
|
|
|
|
# Map HTTP methods onto actions.
|
|
|
|
default_mapping = {
|
|
|
|
'get': 'retrieve',
|
|
|
|
'post': 'create',
|
|
|
|
'put': 'update',
|
|
|
|
'patch': 'partial_update',
|
|
|
|
'delete': 'destroy',
|
|
|
|
}
|
2017-09-14 11:46:34 +03:00
|
|
|
endpoint_inspector_cls = EndpointEnumerator
|
2016-10-10 15:03:46 +03:00
|
|
|
|
|
|
|
# Map the method names we use for viewset actions onto external schema names.
|
|
|
|
# These give us names that are more suitable for the external representation.
|
|
|
|
# Set by 'SCHEMA_COERCE_METHOD_NAMES'.
|
|
|
|
coerce_method_names = None
|
|
|
|
|
|
|
|
# 'pk' isn't great as an externally exposed name for an identifier,
|
|
|
|
# so by default we prefer to use the actual model field name for schemas.
|
|
|
|
# Set by 'SCHEMA_COERCE_PATH_PK'.
|
|
|
|
coerce_path_pk = None
|
|
|
|
|
2017-03-03 18:24:37 +03:00
|
|
|
def __init__(self, title=None, url=None, description=None, patterns=None, urlconf=None):
|
2016-10-10 15:03:46 +03:00
|
|
|
assert coreapi, '`coreapi` must be installed for schema support.'
|
2017-03-03 18:24:37 +03:00
|
|
|
assert coreschema, '`coreschema` must be installed for schema support.'
|
2016-10-10 15:03:46 +03:00
|
|
|
|
|
|
|
if url and not url.endswith('/'):
|
|
|
|
url += '/'
|
|
|
|
|
|
|
|
self.coerce_method_names = api_settings.SCHEMA_COERCE_METHOD_NAMES
|
|
|
|
self.coerce_path_pk = api_settings.SCHEMA_COERCE_PATH_PK
|
|
|
|
|
|
|
|
self.patterns = patterns
|
|
|
|
self.urlconf = urlconf
|
|
|
|
self.title = title
|
2017-03-03 18:24:37 +03:00
|
|
|
self.description = description
|
2016-10-10 15:03:46 +03:00
|
|
|
self.url = url
|
|
|
|
self.endpoints = None
|
|
|
|
|
2017-03-03 18:24:37 +03:00
|
|
|
def get_schema(self, request=None, public=False):
|
2016-07-04 18:38:17 +03:00
|
|
|
"""
|
2016-10-10 15:03:46 +03:00
|
|
|
Generate a `coreapi.Document` representing the API schema.
|
2016-07-04 18:38:17 +03:00
|
|
|
"""
|
2016-10-10 15:03:46 +03:00
|
|
|
if self.endpoints is None:
|
|
|
|
inspector = self.endpoint_inspector_cls(self.patterns, self.urlconf)
|
|
|
|
self.endpoints = inspector.get_api_endpoints()
|
|
|
|
|
2017-03-03 18:24:37 +03:00
|
|
|
links = self.get_links(None if public else request)
|
2016-10-10 15:03:46 +03:00
|
|
|
if not links:
|
|
|
|
return None
|
2017-03-03 18:24:37 +03:00
|
|
|
|
|
|
|
url = self.url
|
|
|
|
if not url and request is not None:
|
|
|
|
url = request.build_absolute_uri()
|
|
|
|
|
2017-10-16 12:32:48 +03:00
|
|
|
distribute_links(links)
|
2017-03-03 18:24:37 +03:00
|
|
|
return coreapi.Document(
|
|
|
|
title=self.title, description=self.description,
|
|
|
|
url=url, content=links
|
|
|
|
)
|
2016-08-11 16:07:40 +03:00
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
def get_links(self, request=None):
|
|
|
|
"""
|
|
|
|
Return a dictionary containing all the links that should be
|
|
|
|
included in the API schema.
|
2016-08-12 12:58:47 +03:00
|
|
|
"""
|
2017-10-16 12:32:48 +03:00
|
|
|
links = LinkNode()
|
2016-10-10 15:03:46 +03:00
|
|
|
|
|
|
|
# Generate (path, method, view) given (path, method, callback).
|
|
|
|
paths = []
|
|
|
|
view_endpoints = []
|
|
|
|
for path, method, callback in self.endpoints:
|
|
|
|
view = self.create_view(callback, method, request)
|
|
|
|
path = self.coerce_path(path, method, view)
|
|
|
|
paths.append(path)
|
|
|
|
view_endpoints.append((path, method, view))
|
|
|
|
|
|
|
|
# Only generate the path prefix for paths that will be included
|
2016-11-07 14:41:10 +03:00
|
|
|
if not paths:
|
|
|
|
return None
|
2016-10-10 15:03:46 +03:00
|
|
|
prefix = self.determine_path_prefix(paths)
|
2016-08-12 12:58:47 +03:00
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
for path, method, view in view_endpoints:
|
|
|
|
if not self.has_view_permissions(path, method, view):
|
|
|
|
continue
|
2017-09-14 11:46:34 +03:00
|
|
|
link = view.schema.get_link(path, method, base_url=self.url)
|
2016-10-10 15:03:46 +03:00
|
|
|
subpath = path[len(prefix):]
|
|
|
|
keys = self.get_keys(subpath, method, view)
|
|
|
|
insert_into(links, keys, link)
|
2017-10-16 12:32:48 +03:00
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
return links
|
|
|
|
|
|
|
|
# Methods used when we generate a view instance from the raw callback...
|
2016-08-12 12:58:47 +03:00
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
def determine_path_prefix(self, paths):
|
2016-08-12 12:58:47 +03:00
|
|
|
"""
|
2016-10-10 15:03:46 +03:00
|
|
|
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/'
|
|
|
|
"""
|
|
|
|
prefixes = []
|
|
|
|
for path in paths:
|
|
|
|
components = path.strip('/').split('/')
|
|
|
|
initial_components = []
|
|
|
|
for component in components:
|
|
|
|
if '{' in component:
|
|
|
|
break
|
|
|
|
initial_components.append(component)
|
|
|
|
prefix = '/'.join(initial_components[:-1])
|
|
|
|
if not prefix:
|
|
|
|
# We can just break early in the case that there's at least
|
|
|
|
# one URL that doesn't have a path prefix.
|
|
|
|
return '/'
|
|
|
|
prefixes.append('/' + prefix + '/')
|
2016-10-21 18:59:34 +03:00
|
|
|
return common_path(prefixes)
|
2016-10-10 15:03:46 +03:00
|
|
|
|
|
|
|
def create_view(self, callback, method, request=None):
|
|
|
|
"""
|
|
|
|
Given a callback, return an actual view instance.
|
|
|
|
"""
|
2018-07-06 11:35:36 +03:00
|
|
|
view = callback.cls(**getattr(callback, 'initkwargs', {}))
|
2016-10-10 15:03:46 +03:00
|
|
|
view.args = ()
|
|
|
|
view.kwargs = {}
|
|
|
|
view.format_kwarg = None
|
|
|
|
view.request = None
|
|
|
|
view.action_map = getattr(callback, 'actions', None)
|
|
|
|
|
|
|
|
actions = getattr(callback, 'actions', None)
|
|
|
|
if actions is not None:
|
|
|
|
if method == 'OPTIONS':
|
|
|
|
view.action = 'metadata'
|
|
|
|
else:
|
|
|
|
view.action = actions.get(method.lower())
|
|
|
|
|
|
|
|
if request is not None:
|
|
|
|
view.request = clone_request(request, method)
|
|
|
|
|
|
|
|
return view
|
|
|
|
|
|
|
|
def has_view_permissions(self, path, method, view):
|
|
|
|
"""
|
|
|
|
Return `True` if the incoming request has the correct view permissions.
|
|
|
|
"""
|
|
|
|
if view.request is None:
|
|
|
|
return True
|
2016-08-12 12:58:47 +03:00
|
|
|
|
|
|
|
try:
|
2016-10-10 15:03:46 +03:00
|
|
|
view.check_permissions(view.request)
|
2016-11-02 12:04:01 +03:00
|
|
|
except (exceptions.APIException, Http404, PermissionDenied):
|
2016-10-10 15:03:46 +03:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def 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")
|
|
|
|
"""
|
|
|
|
if not self.coerce_path_pk or '{pk}' not in path:
|
|
|
|
return path
|
|
|
|
model = getattr(getattr(view, 'queryset', None), 'model', None)
|
|
|
|
if model:
|
|
|
|
field_name = get_pk_name(model)
|
|
|
|
else:
|
|
|
|
field_name = 'id'
|
|
|
|
return path.replace('{pk}', '{%s}' % field_name)
|
2016-07-04 18:38:17 +03:00
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
# Method for generating the link layout....
|
|
|
|
|
|
|
|
def 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")
|
|
|
|
"""
|
|
|
|
if hasattr(view, 'action'):
|
|
|
|
# Viewsets have explicitly named actions.
|
|
|
|
action = view.action
|
|
|
|
else:
|
|
|
|
# Views have no associated action, so we determine one from the method.
|
|
|
|
if is_list_view(subpath, method, view):
|
|
|
|
action = 'list'
|
|
|
|
else:
|
|
|
|
action = self.default_mapping[method.lower()]
|
|
|
|
|
|
|
|
named_path_components = [
|
|
|
|
component for component
|
|
|
|
in subpath.strip('/').split('/')
|
|
|
|
if '{' not in component
|
|
|
|
]
|
|
|
|
|
|
|
|
if is_custom_action(action):
|
|
|
|
# Custom action, eg "/users/{pk}/activate/", "/users/active/"
|
|
|
|
if len(view.action_map) > 1:
|
|
|
|
action = self.default_mapping[method.lower()]
|
|
|
|
if action in self.coerce_method_names:
|
|
|
|
action = self.coerce_method_names[action]
|
|
|
|
return named_path_components + [action]
|
|
|
|
else:
|
|
|
|
return named_path_components[:-1] + [action]
|
|
|
|
|
|
|
|
if action in self.coerce_method_names:
|
|
|
|
action = self.coerce_method_names[action]
|
|
|
|
|
|
|
|
# Default action, eg "/users/", "/users/{pk}/"
|
|
|
|
return named_path_components + [action]
|