Allow customizing the SchemaGenerator class for docs

This commit is contained in:
Aarni Koskela 2017-04-18 13:44:43 +03:00
parent bd768d6253
commit 51a6c076e2
2 changed files with 22 additions and 10 deletions

View File

@ -3,10 +3,10 @@ from django.conf.urls import include, url
from rest_framework.renderers import ( from rest_framework.renderers import (
CoreJSONRenderer, DocumentationRenderer, SchemaJSRenderer CoreJSONRenderer, DocumentationRenderer, SchemaJSRenderer
) )
from rest_framework.schemas import get_schema_view from rest_framework.schemas import SchemaGenerator, get_schema_view
def get_docs_view(title=None, description=None, schema_url=None, public=True): def get_docs_view(title=None, description=None, schema_url=None, public=True, generator_class=SchemaGenerator):
renderer_classes = [DocumentationRenderer, CoreJSONRenderer] renderer_classes = [DocumentationRenderer, CoreJSONRenderer]
return get_schema_view( return get_schema_view(
@ -14,11 +14,12 @@ def get_docs_view(title=None, description=None, schema_url=None, public=True):
url=schema_url, url=schema_url,
description=description, description=description,
renderer_classes=renderer_classes, renderer_classes=renderer_classes,
public=public public=public,
generator_class=generator_class,
) )
def get_schemajs_view(title=None, description=None, schema_url=None, public=True): def get_schemajs_view(title=None, description=None, schema_url=None, public=True, generator_class=SchemaGenerator):
renderer_classes = [SchemaJSRenderer] renderer_classes = [SchemaJSRenderer]
return get_schema_view( return get_schema_view(
@ -26,22 +27,25 @@ def get_schemajs_view(title=None, description=None, schema_url=None, public=True
url=schema_url, url=schema_url,
description=description, description=description,
renderer_classes=renderer_classes, renderer_classes=renderer_classes,
public=public public=public,
generator_class=generator_class,
) )
def include_docs_urls(title=None, description=None, schema_url=None, public=True): def include_docs_urls(title=None, description=None, schema_url=None, public=True, generator_class=SchemaGenerator):
docs_view = get_docs_view( docs_view = get_docs_view(
title=title, title=title,
description=description, description=description,
schema_url=schema_url, schema_url=schema_url,
public=public public=public,
generator_class=generator_class,
) )
schema_js_view = get_schemajs_view( schema_js_view = get_schemajs_view(
title=title, title=title,
description=description, description=description,
schema_url=schema_url, schema_url=schema_url,
public=public public=public,
generator_class=generator_class,
) )
urls = [ urls = [
url(r'^$', docs_view, name='docs-index'), url(r'^$', docs_view, name='docs-index'),

View File

@ -694,11 +694,19 @@ class SchemaView(APIView):
return Response(schema) return Response(schema)
def get_schema_view(title=None, url=None, description=None, urlconf=None, renderer_classes=None, public=False): def get_schema_view(
title=None,
url=None,
description=None,
urlconf=None,
renderer_classes=None,
public=False,
generator_class=SchemaGenerator,
):
""" """
Return a schema view. Return a schema view.
""" """
generator = SchemaGenerator(title=title, url=url, description=description, urlconf=urlconf) generator = generator_class(title=title, url=url, description=description, urlconf=urlconf)
return SchemaView.as_view( return SchemaView.as_view(
renderer_classes=renderer_classes, renderer_classes=renderer_classes,
schema_generator=generator, schema_generator=generator,