Export the docstring of the method in the Link description

For viewset, it exports the corresponding action (list, retrieve,
update, etc.). For simple APIView, it returns the http handler (get,
post, put, etc.).
This commit is contained in:
Grégoire ROCHER 2016-07-28 15:39:49 +02:00
parent 99aa001f10
commit 38b953bb7e
2 changed files with 11 additions and 1 deletions

View File

@ -6,7 +6,7 @@ from django.core.urlresolvers import RegexURLPattern, RegexURLResolver
from django.utils import six
from django.utils.encoding import force_text
from rest_framework import exceptions, serializers
from rest_framework import exceptions, serializers, viewsets
from rest_framework.compat import coreapi, uritemplate, urlparse
from rest_framework.request import clone_request
from rest_framework.views import APIView
@ -207,10 +207,18 @@ class SchemaGenerator(object):
else:
encoding = None
if isinstance(view, viewsets.GenericViewSet):
actions = getattr(callback, 'actions', self.default_mapping)
action = actions[method.lower()]
view_fn = getattr(callback.cls, action, None)
else:
view_fn = getattr(callback.cls, method.lower(), None)
return coreapi.Link(
url=urlparse.urljoin(self.url, path),
action=method.lower(),
encoding=encoding,
description=view_fn.__doc__ if view_fn else '',
fields=fields
)

View File

@ -38,6 +38,7 @@ class ExampleView(APIView):
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
def get(self, request, *args, **kwargs):
"""get documentation"""
return Response()
def post(self, request, *args, **kwargs):
@ -171,6 +172,7 @@ class TestSchemaGenerator(TestCase):
'read': coreapi.Link(
url='/example-view/',
action='get',
description='get documentation',
fields=[]
)
}