2017-09-14 11:46:34 +03:00
|
|
|
"""
|
|
|
|
views.py # Houses `SchemaView`, `APIView` subclass.
|
|
|
|
|
|
|
|
See schemas.__init__.py for package overview.
|
|
|
|
"""
|
|
|
|
from rest_framework import exceptions, renderers
|
|
|
|
from rest_framework.response import Response
|
2019-05-13 17:07:03 +03:00
|
|
|
from rest_framework.schemas import coreapi
|
2017-09-14 11:46:34 +03:00
|
|
|
from rest_framework.settings import api_settings
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
|
|
|
|
|
|
|
class SchemaView(APIView):
|
|
|
|
_ignore_model_permissions = True
|
2017-09-20 12:29:47 +03:00
|
|
|
schema = None # exclude from schema
|
2017-09-14 11:46:34 +03:00
|
|
|
renderer_classes = None
|
|
|
|
schema_generator = None
|
|
|
|
public = False
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2019-04-30 18:53:44 +03:00
|
|
|
super().__init__(*args, **kwargs)
|
2017-09-14 11:46:34 +03:00
|
|
|
if self.renderer_classes is None:
|
2019-05-13 17:07:03 +03:00
|
|
|
if coreapi.is_enabled():
|
|
|
|
self.renderer_classes = [
|
|
|
|
renderers.CoreAPIOpenAPIRenderer,
|
|
|
|
renderers.CoreJSONRenderer
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
self.renderer_classes = [
|
|
|
|
renderers.OpenAPIRenderer,
|
|
|
|
renderers.JSONOpenAPIRenderer,
|
|
|
|
]
|
2017-09-14 11:46:34 +03:00
|
|
|
if renderers.BrowsableAPIRenderer in api_settings.DEFAULT_RENDERER_CLASSES:
|
2018-10-04 16:05:55 +03:00
|
|
|
self.renderer_classes += [renderers.BrowsableAPIRenderer]
|
2017-09-14 11:46:34 +03:00
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
schema = self.schema_generator.get_schema(request, self.public)
|
|
|
|
if schema is None:
|
|
|
|
raise exceptions.PermissionDenied()
|
|
|
|
return Response(schema)
|
2019-01-31 17:28:01 +03:00
|
|
|
|
|
|
|
def handle_exception(self, exc):
|
|
|
|
# Schema renderers do not render exceptions, so re-perform content
|
|
|
|
# negotiation with default renderers.
|
|
|
|
self.renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES
|
|
|
|
neg = self.perform_content_negotiation(self.request, force=True)
|
|
|
|
self.request.accepted_renderer, self.request.accepted_media_type = neg
|
2019-04-30 18:53:44 +03:00
|
|
|
return super().handle_exception(exc)
|