Skip to content

Commit

Permalink
Merge pull request #5448 from carltongibson/pr/5309-docs-view-custom-…
Browse files Browse the repository at this point in the history
…auth

Allow setting custom authentication and permissions on docs view.
  • Loading branch information
Carlton Gibson authored Sep 25, 2017
2 parents 107e8b3 + 1bcee8c commit 8812e6c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
9 changes: 9 additions & 0 deletions docs/api-guide/schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,15 @@ to be exposed in the schema:
May be used to specify a `SchemaGenerator` subclass to be passed to the
`SchemaView`.

#### `authentication_classes`

May be used to specify the list of authentication classes that will apply to the schema endpoint.
Defaults to `settings.DEFAULT_AUTHENTICATION_CLASSES`

#### `permission_classes`

May be used to specify the list of permission classes that will apply to the schema endpoint.
Defaults to `settings.DEFAULT_PERMISSION_CLASSES`


## Using an explicit schema view
Expand Down
21 changes: 18 additions & 3 deletions rest_framework/documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
CoreJSONRenderer, DocumentationRenderer, SchemaJSRenderer
)
from rest_framework.schemas import SchemaGenerator, get_schema_view
from rest_framework.settings import api_settings


def get_docs_view(
title=None, description=None, schema_url=None, public=True,
patterns=None, generator_class=SchemaGenerator):
patterns=None, generator_class=SchemaGenerator,
authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES):
renderer_classes = [DocumentationRenderer, CoreJSONRenderer]

return get_schema_view(
Expand All @@ -19,12 +22,16 @@ def get_docs_view(
public=public,
patterns=patterns,
generator_class=generator_class,
authentication_classes=authentication_classes,
permission_classes=permission_classes,
)


def get_schemajs_view(
title=None, description=None, schema_url=None, public=True,
patterns=None, generator_class=SchemaGenerator):
patterns=None, generator_class=SchemaGenerator,
authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES):
renderer_classes = [SchemaJSRenderer]

return get_schema_view(
Expand All @@ -35,19 +42,25 @@ def get_schemajs_view(
public=public,
patterns=patterns,
generator_class=generator_class,
authentication_classes=authentication_classes,
permission_classes=permission_classes,
)


def include_docs_urls(
title=None, description=None, schema_url=None, public=True,
patterns=None, generator_class=SchemaGenerator):
patterns=None, generator_class=SchemaGenerator,
authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES):
docs_view = get_docs_view(
title=title,
description=description,
schema_url=schema_url,
public=public,
patterns=patterns,
generator_class=generator_class,
authentication_classes=authentication_classes,
permission_classes=permission_classes,
)
schema_js_view = get_schemajs_view(
title=title,
Expand All @@ -56,6 +69,8 @@ def include_docs_urls(
public=public,
patterns=patterns,
generator_class=generator_class,
authentication_classes=authentication_classes,
permission_classes=permission_classes,
)
urls = [
url(r'^$', docs_view, name='docs-index'),
Expand Down
8 changes: 7 additions & 1 deletion rest_framework/schemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
Other access should target the submodules directly
"""
from rest_framework.settings import api_settings

from .generators import SchemaGenerator
from .inspectors import AutoSchema, ManualSchema # noqa


def get_schema_view(
title=None, url=None, description=None, urlconf=None, renderer_classes=None,
public=False, patterns=None, generator_class=SchemaGenerator):
public=False, patterns=None, generator_class=SchemaGenerator,
authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES):
"""
Return a schema view.
"""
Expand All @@ -40,4 +44,6 @@ def get_schema_view(
renderer_classes=renderer_classes,
schema_generator=generator,
public=public,
authentication_classes=authentication_classes,
permission_classes=permission_classes,
)

0 comments on commit 8812e6c

Please sign in to comment.