Skip to content

Commit 44580bd

Browse files
Merge pull request #5 from ekonstantinidis/allowed-methods
Allowed methods & Serializer Fields
2 parents 13443fb + 969a0f3 commit 44580bd

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

drfdocs/api_endpoint.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,28 @@ class ApiEndpoint(object):
55

66
def __init__(self, pattern, parent_pattern=None):
77
self.pattern = pattern
8+
self.callback = pattern.callback
89
self.name = pattern.name
9-
self.path = self._get_path(parent_pattern)
10+
self.name_parent = simplify_regex(parent_pattern.regex.pattern).replace('/', '') if parent_pattern else None
11+
self.path = self.__get_path__(parent_pattern)
12+
self.allowed_methods = self.__get_allowed_methods__()
1013
self.view_name = pattern.callback.__name__
14+
self.fields = self.__get_serializer_fields__()
1115

12-
def _get_path(self, parent_pattern):
16+
def __get_path__(self, parent_pattern):
1317
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))
18+
return "/{0}{1}".format(self.name_parent, simplify_regex(self.pattern.regex.pattern))
1619
return simplify_regex(self.pattern.regex.pattern)
20+
21+
def __get_allowed_methods__(self):
22+
return [m.upper() for m in self.callback.cls.http_method_names if hasattr(self.callback.cls, m)]
23+
24+
def __get_serializer_fields__(self):
25+
fields = []
26+
27+
if hasattr(self.callback.cls, 'serializer_class') and hasattr(self.callback.cls.serializer_class, 'get_fields'):
28+
serializer = self.callback.cls.serializer_class
29+
if hasattr(serializer, 'get_fields'):
30+
fields = [{"name": key, "type": str(value.__class__.__name__)} for key, value in serializer().get_fields().items()]
31+
32+
return fields

drfdocs/static/css/style.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ body {
2222

2323
.endpoint .title {
2424
font-family: Menlo,Monaco,Consolas,"Courier New",monospace;
25+
word-wrap: break-word;
26+
}
27+
28+
.endpoint .methods .method {
29+
border: 1px solid #2c3e50;
30+
padding: 3px 8px;
2531
}
2632

2733
/* @end Endpoint */

drfdocs/templates/drfdocs/home.html

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

33
{% block content %}
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>
4+
5+
<h1>API Endpoints</h1>
6+
{% regroup endpoints by name_parent as endpoints_grouped %}
7+
8+
{% for group in endpoints_grouped %}
9+
<h1>{{group.grouper}}</h1>
10+
11+
{% for endpoint in group.list %}
12+
<div class="endpoint">
13+
<h4 class="title">{{ endpoint.path }}</h4>
14+
15+
<ul class="list-inline methods">
16+
{% for method in endpoint.allowed_methods %}
17+
<li class="method">{{ method }}</li>
18+
{% endfor %}
19+
</ul>
20+
21+
<p>View Name: {{ endpoint.view_name }}</p>
22+
<p>URL Name: {{ endpoint.name }}</p>
23+
24+
{% if endpoint.fields %}
25+
<p>Fields:</p>
26+
<ul class="list fields">
27+
{% for field in endpoint.fields %}
28+
<li class="field">{{ field.name }}: {{ field.type }}</li>
29+
{% endfor %}
30+
</ul>
31+
{% endif %}
32+
33+
</div>
34+
{% endfor %}
35+
1236
{% endfor %}
37+
1338
{% endblock %}

0 commit comments

Comments
 (0)