Commit 43ef6535 authored by M. de Verteuil's avatar M. de Verteuil
Browse files

Request user determines which serializer to include

parent e917f70a
......@@ -20,6 +20,9 @@ class DocumentationGenerator(object):
# Response classes defined in docstrings
explicit_response_types = dict()
def __init__(self, for_user):
self.user = for_user
def generate(self, apis):
Returns documentation for a list of APIs
......@@ -39,14 +42,13 @@ class DocumentationGenerator(object):
pattern = api['pattern']
callback = api['callback']
if callback.__module__ == 'rest_framework.decorators':
return WrappedAPIViewIntrospector(callback, path, pattern)
return WrappedAPIViewIntrospector(callback, path, pattern, self.user)
elif issubclass(callback, viewsets.ViewSetMixin):
patterns = [a['pattern'] for a in apis
if a['callback'] == callback]
return ViewSetIntrospector(callback, path, pattern,
return ViewSetIntrospector(callback, path, pattern, self.user, patterns=patterns)
return APIViewIntrospector(callback, path, pattern)
return APIViewIntrospector(callback, path, pattern, self.user)
def get_operations(self, api, apis=None):
......@@ -12,7 +12,6 @@ from .compat import OrderedDict, strip_tags, get_pagination_attribures
from abc import ABCMeta, abstractmethod
from django.http import HttpRequest
from django.contrib.auth.models import AnonymousUser
from django.contrib.admindocs.utils import trim_docstring
from django.utils.encoding import smart_text
......@@ -125,10 +124,11 @@ class IntrospectorHelper(object):
class BaseViewIntrospector(object):
__metaclass__ = ABCMeta
def __init__(self, callback, path, pattern):
def __init__(self, callback, path, pattern, user):
self.callback = callback
self.path = path
self.pattern = pattern
self.user = user
def get_yaml_parser(self):
parser = YAMLDocstringParser(self)
......@@ -166,6 +166,7 @@ class BaseMethodIntrospector(object):
self.parent = view_introspector
self.callback = view_introspector.callback
self.path = view_introspector.path
self.user = view_introspector.user
def get_module(self):
return self.callback.__module__
......@@ -210,7 +211,7 @@ class BaseMethodIntrospector(object):
if hasattr(self.parent.pattern, 'default_args'):
view.request = HttpRequest()
view.request.user = AnonymousUser()
view.request.user = self.user
view.request.method = self.method
return view
......@@ -574,8 +575,8 @@ class WrappedAPIViewMethodIntrospector(BaseMethodIntrospector):
class ViewSetIntrospector(BaseViewIntrospector):
"""Handle ViewSet introspection."""
def __init__(self, callback, path, pattern, patterns=None):
super(ViewSetIntrospector, self).__init__(callback, path, pattern)
def __init__(self, callback, path, pattern, user, patterns=None):
super(ViewSetIntrospector, self).__init__(callback, path, pattern, user)
if not issubclass(callback, viewsets.ViewSetMixin):
raise Exception("wrong callback passed to ViewSetIntrospector")
self.patterns = patterns or [pattern]
......@@ -153,7 +153,7 @@ class SwaggerApiView(APIDocView):
apis = [api for api in apis
if self.handle_resource_access(request, api['pattern'])]
generator = DocumentationGenerator()
generator = DocumentationGenerator(for_user=request.user)
return Response({
'apiVersion': rfs.SWAGGER_SETTINGS.get('api_version', ''),
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment