From 5f66bfe8d95173d1259bad141f4434ad9b4daf2c Mon Sep 17 00:00:00 2001 From: Ivan Anishchuk Date: Fri, 14 Jul 2017 02:49:33 +0800 Subject: [PATCH] Add schema generator with automatic bulk action support Based entirely on DRF's default generator with expanded actions list. Requires yet to be released changes in DRF. Add an example on how to use it with interactive docs. --- README.rst | 16 ++++++++++++++++ rest_framework_bulk/drf3/schemas.py | 14 ++++++++++++++ rest_framework_bulk/schemas.py | 7 +++++++ 3 files changed, 37 insertions(+) create mode 100644 rest_framework_bulk/drf3/schemas.py create mode 100644 rest_framework_bulk/schemas.py diff --git a/README.rst b/README.rst index af70822..d621332 100644 --- a/README.rst +++ b/README.rst @@ -182,3 +182,19 @@ please refer to Django REST `docs `_. Either way, please use bulk deletes with extreme caution since they can be dangerous. + +Schemas and interactive documentation +------------------------------------- + +You can use BulkSchemaGenerator for your interactive docs needs:: + + from rest_framework.documentation import include_docs_urls + from rest_framework_bulk.schemas import SchemaGenerator + + urlpatterns = [ + ... + url(r'^docs/', include_docs_urls(title='My API title', generator_class=SchemaGenerator)) + ] + +This will include bulk actions in docs for appropriate viewsets. +Refer to DRF docs for other details on interactive documentation and schemas. diff --git a/rest_framework_bulk/drf3/schemas.py b/rest_framework_bulk/drf3/schemas.py new file mode 100644 index 0000000..a99b8b6 --- /dev/null +++ b/rest_framework_bulk/drf3/schemas.py @@ -0,0 +1,14 @@ +import rest_framework + + +class BulkSchemaGenerator(rest_framework.schemas.SchemaGenerator): + """ + Customized schema generator with bulk actions included + """ + + default_list_mapping = { + 'get': 'list', + 'put': 'bulk_update', + 'patch': 'bulk_partial_update', + 'delete': 'bulk_destroy', + } diff --git a/rest_framework_bulk/schemas.py b/rest_framework_bulk/schemas.py new file mode 100644 index 0000000..2ccb0f7 --- /dev/null +++ b/rest_framework_bulk/schemas.py @@ -0,0 +1,7 @@ +from __future__ import print_function, unicode_literals +import rest_framework + + +# import schema generator only for DRF 3+ +if not str(rest_framework.__version__).startswith('2'): + from .drf3.schemas import * # noqa