Skip to content

Commit 1913850

Browse files
author
Emmanouil Konstantinidis
committed
Split to Api Docs & Api Endpoint
1 parent 55770e9 commit 1913850

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

drfdocs/api_docs.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
from django.conf import settings
22
from django.core.urlresolvers import RegexURLResolver, RegexURLPattern
3+
from drfdocs.api_endpoint import ApiEndpoint
34

45

56
class ApiDocumentation(object):
67
excluded_apps = ["admin", "drfdocs"]
78
excluded_endpoints = ["serve"]
8-
root_urlconf = __import__(settings.ROOT_URLCONF)
99

1010
def __init__(self):
11-
self.view_names = []
12-
self.get_all_view_names(self.root_urlconf.urls.urlpatterns)
11+
self.endpoints = []
12+
root_urlconf = __import__(settings.ROOT_URLCONF)
13+
self.get_all_view_names(root_urlconf.urls.urlpatterns)
1314

1415
def get_all_view_names(self, urlpatterns):
1516
for pattern in urlpatterns:
1617
if isinstance(pattern, RegexURLResolver) and (pattern.app_name not in self.excluded_apps):
1718
self.get_all_view_names(pattern.url_patterns)
1819
elif isinstance(pattern, RegexURLPattern) and (pattern.callback.__name__ not in self.excluded_endpoints):
19-
self.view_names.append(pattern.callback.__name__)
20+
api_endpoint = ApiEndpoint(pattern)
21+
self.endpoints.append(api_endpoint)
2022

21-
def get_views(self):
22-
return self.view_names
23+
def get_endpoints(self):
24+
return self.endpoints

drfdocs/api_endpoint.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from django.contrib.admindocs.views import simplify_regex
2+
from rest_framework.views import APIView
3+
4+
5+
class ApiEndpoint(object):
6+
7+
def __init__(self, pattern):
8+
self.pattern = pattern
9+
self.regex = simplify_regex(pattern._regex)
10+
self.view_name = pattern.callback.__name__
11+
# self._get_api_callback(pattern)
12+
13+
def _get_api_callback(self, pattern):
14+
"""
15+
Verifies that pattern callback is a subclass of APIView, and returns the class
16+
"""
17+
if (hasattr(pattern.callback, 'cls') and issubclass(pattern.callback.cls, APIView)):
18+
return pattern.callback.cls

drfdocs/templates/drfdocs/home.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
{% block content %}
44
<ul>
5-
{% for view in views %}
6-
<li>{{ view }}</li>
5+
{% for endpoint in endpoints %}
6+
<li>
7+
<pre>{{ endpoint.regex }}</pre>
8+
<p>{{ endpoint.view_name }}</p>
79
{% endfor %}
810
</ul>
911
{% endblock %}

drfdocs/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ class DRFDocsView(TemplateView):
99
def get_context_data(self, **kwargs):
1010
context = super(DRFDocsView, self).get_context_data(**kwargs)
1111
docs = ApiDocumentation()
12-
context['views'] = docs.get_views()
12+
context['endpoints'] = docs.get_endpoints()
1313
return context

0 commit comments

Comments
 (0)