Skip to content

Commit 0c9d3c4

Browse files
Merge pull request #20 from ekonstantinidis/get-docstrings
Get docstrings of endpoints
2 parents fdecdfb + d455475 commit 0c9d3c4

File tree

8 files changed

+37
-4
lines changed

8 files changed

+37
-4
lines changed

demo/project/accounts/views.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class TestView(TemplateView):
2020

2121

2222
class LoginView(APIView):
23+
"""
24+
A view that allows users to login providing their username and password.
25+
"""
2326

2427
throttle_classes = ()
2528
permission_classes = ()
@@ -42,6 +45,9 @@ class UserRegistrationView(generics.CreateAPIView):
4245

4346

4447
class UserProfileView(generics.RetrieveUpdateAPIView):
48+
"""
49+
An endpoint for users to view and update their profile information.
50+
"""
4551

4652
serializer_class = UserProfileSerializer
4753

rest_framework_docs/api_endpoint.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import inspect
12
from django.contrib.admindocs.views import simplify_regex
23

34

@@ -7,6 +8,7 @@ def __init__(self, pattern, parent_pattern=None):
78
self.pattern = pattern
89
self.callback = pattern.callback
910
# self.name = pattern.name
11+
self.docstring = self.__get_docstring__()
1012
self.name_parent = simplify_regex(parent_pattern.regex.pattern).replace('/', '') if parent_pattern else None
1113
self.path = self.__get_path__(parent_pattern)
1214
self.allowed_methods = self.__get_allowed_methods__()
@@ -22,6 +24,9 @@ def __get_path__(self, parent_pattern):
2224
def __get_allowed_methods__(self):
2325
return [m.upper() for m in self.callback.cls.http_method_names if hasattr(self.callback.cls, m)]
2426

27+
def __get_docstring__(self):
28+
return inspect.getdoc(self.callback)
29+
2530
def __get_serializer_fields__(self):
2631
fields = []
2732

rest_framework_docs/static/css/style.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rest_framework_docs/static/less/style.less

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,22 @@ body {
9292
.panel-body {
9393
padding: 0;
9494

95+
.lead {
96+
margin-bottom: 5px;
97+
}
98+
9599
.alert {
96100
padding: 5px 10px;
97101
margin-top: 10px;
98102
}
103+
104+
.fields-desc {
105+
margin-bottom: 5px;
106+
}
107+
108+
.fields {
109+
list-style-type: square;
110+
}
99111
}
100112

101113
> .panel-heading +.panel-collapse > .panel-body { border: 0; }

rest_framework_docs/static/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
"django",
2121
"rest",
2222
"framework",
23-
"docs",
24-
"jekyll"
23+
"docs"
2524
],
2625
"author": "Emmanouil Konstantinidis",
2726
"license": "SEE LICENSE IN LICENSE",

rest_framework_docs/templates/rest_framework_docs/home.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,16 @@ <h4 class="panel-title title">
4848

4949
<div id="{{ endpoint.path|slugify }}" class="panel-collapse collapse" role="tabpanel">
5050
<div class="panel-body">
51+
{% if endpoint.docstring %}
52+
<p class="lead">{{ endpoint.docstring }}</p>
53+
{% endif %}
54+
5155
{% if endpoint.errors %}
5256
<div class="alert alert-danger" role="alert">Oops! There was something wrong with {{ endpoint.errors }}. Please check your code.</div>
5357
{% endif %}
5458

5559
{% if endpoint.fields %}
56-
<p>Fields:</p>
60+
<p class="fields-desc">Fields:</p>
5761
<ul class="list fields">
5862
{% for field in endpoint.fields %}
5963
<li class="field">{{ field.name }}: {{ field.type }} {% if field.required %}<span class="label label-primary label-required" title="Required">R</span>{% endif %}</li>

tests/tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def test_index_view_with_endpoints(self):
3333
self.assertEqual(response.context["endpoints"][0].name_parent, "accounts")
3434
self.assertEqual(response.context["endpoints"][0].allowed_methods, ['POST', 'OPTIONS'])
3535
self.assertEqual(response.context["endpoints"][0].path, "/accounts/login/")
36+
self.assertEqual(response.context["endpoints"][0].docstring, "A view that allows users to login providing their username and password.")
3637
self.assertEqual(len(response.context["endpoints"][0].fields), 2)
3738
self.assertEqual(response.context["endpoints"][0].fields[0]["type"], "CharField")
3839
self.assertTrue(response.context["endpoints"][0].fields[0]["required"])

tests/views.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class TestView(TemplateView):
2020

2121

2222
class LoginView(APIView):
23+
"""
24+
A view that allows users to login providing their username and password.
25+
"""
2326

2427
throttle_classes = ()
2528
permission_classes = ()
@@ -42,6 +45,9 @@ class UserRegistrationView(generics.CreateAPIView):
4245

4346

4447
class UserProfileView(generics.RetrieveUpdateAPIView):
48+
"""
49+
An endpoint for users to view and update their profile information.
50+
"""
4551

4652
serializer_class = serializers.UserProfileSerializer
4753

0 commit comments

Comments
 (0)