Skip to content

Commit 13443fb

Browse files
Merge pull request #4 from ekonstantinidis/init-api-endpoint-obj
Split to Api Docs & Api Endpoint
2 parents 55770e9 + 6f2f5c8 commit 13443fb

File tree

6 files changed

+173
-16
lines changed

6 files changed

+173
-16
lines changed

drfdocs/api_docs.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
from django.conf import settings
22
from django.core.urlresolvers import RegexURLResolver, RegexURLPattern
3+
from drfdocs.api_endpoint import ApiEndpoint
4+
from rest_framework.views import APIView
35

46

57
class ApiDocumentation(object):
68
excluded_apps = ["admin", "drfdocs"]
79
excluded_endpoints = ["serve"]
8-
root_urlconf = __import__(settings.ROOT_URLCONF)
910

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

14-
def get_all_view_names(self, urlpatterns):
16+
def get_all_view_names(self, urlpatterns, parent_pattern=None):
1517
for pattern in urlpatterns:
1618
if isinstance(pattern, RegexURLResolver) and (pattern.app_name not in self.excluded_apps):
17-
self.get_all_view_names(pattern.url_patterns)
18-
elif isinstance(pattern, RegexURLPattern) and (pattern.callback.__name__ not in self.excluded_endpoints):
19-
self.view_names.append(pattern.callback.__name__)
19+
self.get_all_view_names(urlpatterns=pattern.url_patterns, parent_pattern=pattern)
20+
elif isinstance(pattern, RegexURLPattern) and (pattern.callback.__name__ not in self.excluded_endpoints) and self._is_drf_view(pattern):
21+
api_endpoint = ApiEndpoint(pattern, parent_pattern)
22+
self.endpoints.append(api_endpoint)
2023

21-
def get_views(self):
22-
return self.view_names
24+
def _is_drf_view(self, pattern):
25+
# Should check whether a pattern inherits from DRF's APIView
26+
if (hasattr(pattern.callback, 'cls') and issubclass(pattern.callback.cls, APIView)):
27+
return True
28+
return False
29+
30+
def get_endpoints(self):
31+
return self.endpoints

drfdocs/api_endpoint.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from django.contrib.admindocs.views import simplify_regex
2+
3+
4+
class ApiEndpoint(object):
5+
6+
def __init__(self, pattern, parent_pattern=None):
7+
self.pattern = pattern
8+
self.name = pattern.name
9+
self.path = self._get_path(parent_pattern)
10+
self.view_name = pattern.callback.__name__
11+
12+
def _get_path(self, parent_pattern):
13+
if parent_pattern:
14+
parent_path = simplify_regex(parent_pattern.regex.pattern)[:-1]
15+
return "{0}{1}".format(parent_path, simplify_regex(self.pattern.regex.pattern))
16+
return simplify_regex(self.pattern.regex.pattern)

drfdocs/static/css/style.css

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* @group Misc */
2+
3+
body {
4+
background-color: #FEFEFE;
5+
}
6+
7+
.jumbotron {
8+
margin: 50px 0 40px;
9+
text-align: center;
10+
}
11+
12+
/* @end Misc */
13+
14+
15+
/* @group Endpoint */
16+
17+
.endpoint {
18+
padding: 5px 20px;
19+
margin: 20px 0;
20+
background-color: #ecf0f1;
21+
}
22+
23+
.endpoint .title {
24+
font-family: Menlo,Monaco,Consolas,"Courier New",monospace;
25+
}
26+
27+
/* @end Endpoint */
28+
29+
30+
/* @group Footer */
31+
32+
.footer {
33+
text-align: center;
34+
padding: 20px;
35+
margin-bottom: 20px;
36+
}
37+
38+
.footer .links {
39+
padding: 20px 0;
40+
}
41+
42+
.footer .links .fa {
43+
margin: 0 10px;
44+
font-size: 22px;
45+
}
46+
47+
/* @end Footer */
48+
49+
50+
/* @group Github Ribbon - SVG */
51+
52+
.github-corner:hover .octo-arm {
53+
animation: octocat-wave 560ms ease-in-out;
54+
}
55+
56+
@keyframes octocat-wave {
57+
0% {
58+
transform: rotate(0deg);
59+
}
60+
61+
20% {
62+
transform: rotate(-25deg);
63+
}
64+
65+
40% {
66+
transform: rotate(10deg);
67+
}
68+
69+
60% {
70+
transform: rotate(-25deg);
71+
}
72+
73+
80% {
74+
transform: rotate(10deg);
75+
}
76+
77+
100% {
78+
transform: rotate(0deg);
79+
}
80+
}
81+
82+
@media (max-width: 500px) {
83+
.github-corner:hover .octo-arm {
84+
animation: none;
85+
}
86+
87+
.github-corner .octo-arm {
88+
animation: octocat-wave 560ms ease-in-out;
89+
}
90+
}
91+
92+
/* @end Github Ribbon - SVG */

drfdocs/templates/drfdocs/base.html

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,49 @@
1+
{% load staticfiles %}
2+
13
<!DOCTYPE html>
24
<html>
35
<head>
46
<meta charset="utf-8">
7+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
8+
<meta name="viewport" content="width=device-width, initial-scale=1">
9+
510
<title>DRF Docs</title>
11+
12+
<!-- Bootstrap -->
13+
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
14+
<!-- Flatly -->
15+
<link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.5/flatly/bootstrap.min.css" rel="stylesheet">
16+
<!-- Font Awesome -->
17+
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
18+
<!-- Custom Style -->
19+
<link href="{% static "css/style.css" %}" rel="stylesheet">
620
</head>
21+
722
<body>
8-
<h1>Django Rest Frameworks Docs</h1>
23+
<a href="https://github.com/ekonstantinidis/drf-docs/" class="github-corner" target="_blank">
24+
<svg width="80" height="80" viewBox="0 0 250 250" style="fill:#18bc9c; color:#fff; position: absolute; top: 0; border: 0; right: 0;">
25+
<path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
26+
<path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
27+
<path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path>
28+
</svg>
29+
</a>
30+
31+
<div class="container">
32+
<div class="jumbotron">
33+
<h1>DRF Docs</h1>
34+
<h3>Documentation for Web APIs made with <a href="http://www.django-rest-framework.org/" target="_blank">Django Rest Framework</a>.</h3>
35+
</div>
936

10-
{% block content %}{% endblock %}
37+
{% block content %}{% endblock %}
1138

39+
<div class="footer">
40+
<div class="links">
41+
<a href="http://www.iamemmanouil.com"><i class="fa fa-link"></i></a>
42+
<a href="http://github.com/ekonstantinidis"><i class="fa fa-github"></i></a>
43+
<a href="http://www.twitter.com/iamemmanouil"><i class="fa fa-twitter"></i></a>
44+
</div>
45+
Copyright © 2015 Emmanouil Konstantinidis.
46+
</div>
47+
</div>
1248
</body>
1349
</html>

drfdocs/templates/drfdocs/home.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
{% extends "drfdocs/base.html" %}
22

33
{% block content %}
4-
<ul>
5-
{% for view in views %}
6-
<li>{{ view }}</li>
4+
<h2>API Endpoints</h2>
5+
6+
{% for endpoint in endpoints %}
7+
<div class="endpoint">
8+
<h3 class="title">{{ endpoint.path }}</h3>
9+
<p>View Name: {{ endpoint.view_name }}</p>
10+
<p>URL Name: {{ endpoint.name }}</p>
11+
</div>
712
{% endfor %}
8-
</ul>
913
{% 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)