From a665f875ad64e2fa9d9166ce0155a9eac3dc2b21 Mon Sep 17 00:00:00 2001 From: lukjarzsolwit Date: Mon, 4 Jul 2016 11:48:43 +0200 Subject: [PATCH 1/2] Support to augmented API paths. --- rest_framework_docs/api_docs.py | 12 ++++++++---- rest_framework_docs/api_endpoint.py | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/rest_framework_docs/api_docs.py b/rest_framework_docs/api_docs.py index d22dd4c..36f4c12 100644 --- a/rest_framework_docs/api_docs.py +++ b/rest_framework_docs/api_docs.py @@ -21,13 +21,17 @@ def __init__(self, drf_router=None): else: self.get_all_view_names(root_urlconf.urlpatterns) - def get_all_view_names(self, urlpatterns, parent_pattern=None): + def get_all_view_names(self, urlpatterns, ancestors_patterns=None): + if ancestors_patterns is None: + ancestors_patterns = [] for pattern in urlpatterns: if isinstance(pattern, RegexURLResolver): - parent_pattern = None if pattern._regex == "^" else pattern - self.get_all_view_names(urlpatterns=pattern.url_patterns, parent_pattern=parent_pattern) + if pattern._regex != "^": + ancestors_patterns.append(pattern) + self.get_all_view_names(urlpatterns=pattern.url_patterns, ancestors_patterns=ancestors_patterns) + ancestors_patterns.pop() elif isinstance(pattern, RegexURLPattern) and self._is_drf_view(pattern) and not self._is_format_endpoint(pattern): - api_endpoint = ApiEndpoint(pattern, parent_pattern, self.drf_router) + api_endpoint = ApiEndpoint(pattern, ancestors_patterns, self.drf_router) self.endpoints.append(api_endpoint) def _is_drf_view(self, pattern): diff --git a/rest_framework_docs/api_endpoint.py b/rest_framework_docs/api_endpoint.py index 89a33f8..9135f4d 100644 --- a/rest_framework_docs/api_endpoint.py +++ b/rest_framework_docs/api_endpoint.py @@ -7,13 +7,13 @@ class ApiEndpoint(object): - def __init__(self, pattern, parent_pattern=None, drf_router=None): + def __init__(self, pattern, ancestors_patterns=[], drf_router=None): self.drf_router = drf_router self.pattern = pattern self.callback = pattern.callback # self.name = pattern.name self.docstring = self.__get_docstring__() - self.name_parent = simplify_regex(parent_pattern.regex.pattern).strip('/') if parent_pattern else None + self.name_parent = ''.join([simplify_regex(parent_pattern.regex.pattern).rstrip('/') for parent_pattern in ancestors_patterns]).lstrip('/') if ancestors_patterns else None self.path = self.__get_path__(parent_pattern) self.allowed_methods = self.__get_allowed_methods__() # self.view_name = pattern.callback.__name__ From 15cda4074b47f392674a77809ca876a471c9f9c3 Mon Sep 17 00:00:00 2001 From: Lukasz Jarzembowski Date: Wed, 20 Jul 2016 15:03:32 +0200 Subject: [PATCH 2/2] Fix for top level space --- rest_framework_docs/api_docs.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rest_framework_docs/api_docs.py b/rest_framework_docs/api_docs.py index 36f4c12..88bd710 100644 --- a/rest_framework_docs/api_docs.py +++ b/rest_framework_docs/api_docs.py @@ -29,7 +29,8 @@ def get_all_view_names(self, urlpatterns, ancestors_patterns=None): if pattern._regex != "^": ancestors_patterns.append(pattern) self.get_all_view_names(urlpatterns=pattern.url_patterns, ancestors_patterns=ancestors_patterns) - ancestors_patterns.pop() + if ancestors_patterns: + ancestors_patterns.pop() elif isinstance(pattern, RegexURLPattern) and self._is_drf_view(pattern) and not self._is_format_endpoint(pattern): api_endpoint = ApiEndpoint(pattern, ancestors_patterns, self.drf_router) self.endpoints.append(api_endpoint)