Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting docstrings using DRF get_view_description function #140

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion rest_framework_docs/api_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
from django.contrib.admindocs.views import simplify_regex
from django.utils.encoding import force_str
from rest_framework.serializers import BaseSerializer
from rest_framework_docs.settings import DRFSettings


class ApiEndpoint(object):

def __init__(self, pattern, parent_pattern=None, drf_router=None):
self.settings = DRFSettings().settings
self.drf_router = drf_router
self.pattern = pattern
self.callback = pattern.callback
Expand Down Expand Up @@ -67,7 +69,7 @@ def __get_allowed_methods__(self):
return viewset_methods + view_methods

def __get_docstring__(self):
return inspect.getdoc(self.callback)
return self.settings["VIEW_DESCRIPTION_FUNCTION"](self.callback, html=True)

def __get_permissions_class__(self):
for perm_class in self.pattern.callback.cls.permission_classes:
Expand Down
4 changes: 3 additions & 1 deletion rest_framework_docs/settings.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from django.conf import settings
from rest_framework.views import get_view_description


class DRFSettings(object):

def __init__(self):
self.drf_settings = {
"HIDE_DOCS": self.get_setting("HIDE_DOCS") or False
"HIDE_DOCS": self.get_setting("HIDE_DOCS") or False,
"VIEW_DESCRIPTION_FUNCTION": self.get_setting("VIEW_DESCRIPTION_FUNCTION") or get_view_description
}

def get_setting(self, name):
Expand Down
4 changes: 2 additions & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ def test_index_view_with_endpoints(self):
self.assertEqual(response.context["endpoints"][0].name_parent, "accounts")
self.assertEqual(response.context["endpoints"][0].allowed_methods, ['POST', 'OPTIONS'])
self.assertEqual(response.context["endpoints"][0].path, "/accounts/login/")
self.assertEqual(response.context["endpoints"][0].docstring, "A view that allows users to login providing their username and password.")
self.assertEqual(response.context["endpoints"][0].docstring, "<p>A view that allows users to login providing their username and password.</p>")
self.assertEqual(len(response.context["endpoints"][0].fields), 2)
self.assertEqual(response.context["endpoints"][0].fields[0]["type"], "CharField")
self.assertTrue(response.context["endpoints"][0].fields[0]["required"])

self.assertEqual(response.context["endpoints"][1].name_parent, "accounts")
self.assertEqual(response.context["endpoints"][1].allowed_methods, ['POST', 'OPTIONS'])
self.assertEqual(response.context["endpoints"][1].path, "/accounts/login2/")
self.assertEqual(response.context["endpoints"][1].docstring, "A view that allows users to login providing their username and password. Without serializer_class")
self.assertEqual(response.context["endpoints"][1].docstring, "<p>A view that allows users to login providing their username and password. Without serializer_class</p>")
self.assertEqual(len(response.context["endpoints"][1].fields), 2)
self.assertEqual(response.context["endpoints"][1].fields[0]["type"], "CharField")
self.assertTrue(response.context["endpoints"][1].fields[0]["required"])
Expand Down