documentation.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from django.urls import include, path
  2. from rest_framework.renderers import (
  3. CoreJSONRenderer, DocumentationRenderer, SchemaJSRenderer
  4. )
  5. from rest_framework.schemas import SchemaGenerator, get_schema_view
  6. from rest_framework.settings import api_settings
  7. def get_docs_view(
  8. title=None, description=None, schema_url=None, urlconf=None,
  9. public=True, patterns=None, generator_class=SchemaGenerator,
  10. authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
  11. permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES,
  12. renderer_classes=None):
  13. if renderer_classes is None:
  14. renderer_classes = [DocumentationRenderer, CoreJSONRenderer]
  15. return get_schema_view(
  16. title=title,
  17. url=schema_url,
  18. urlconf=urlconf,
  19. description=description,
  20. renderer_classes=renderer_classes,
  21. public=public,
  22. patterns=patterns,
  23. generator_class=generator_class,
  24. authentication_classes=authentication_classes,
  25. permission_classes=permission_classes,
  26. )
  27. def get_schemajs_view(
  28. title=None, description=None, schema_url=None, urlconf=None,
  29. public=True, patterns=None, generator_class=SchemaGenerator,
  30. authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
  31. permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES):
  32. renderer_classes = [SchemaJSRenderer]
  33. return get_schema_view(
  34. title=title,
  35. url=schema_url,
  36. urlconf=urlconf,
  37. description=description,
  38. renderer_classes=renderer_classes,
  39. public=public,
  40. patterns=patterns,
  41. generator_class=generator_class,
  42. authentication_classes=authentication_classes,
  43. permission_classes=permission_classes,
  44. )
  45. def include_docs_urls(
  46. title=None, description=None, schema_url=None, urlconf=None,
  47. public=True, patterns=None, generator_class=SchemaGenerator,
  48. authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
  49. permission_classes=api_settings.DEFAULT_PERMISSION_CLASSES,
  50. renderer_classes=None):
  51. docs_view = get_docs_view(
  52. title=title,
  53. description=description,
  54. schema_url=schema_url,
  55. urlconf=urlconf,
  56. public=public,
  57. patterns=patterns,
  58. generator_class=generator_class,
  59. authentication_classes=authentication_classes,
  60. renderer_classes=renderer_classes,
  61. permission_classes=permission_classes,
  62. )
  63. schema_js_view = get_schemajs_view(
  64. title=title,
  65. description=description,
  66. schema_url=schema_url,
  67. urlconf=urlconf,
  68. public=public,
  69. patterns=patterns,
  70. generator_class=generator_class,
  71. authentication_classes=authentication_classes,
  72. permission_classes=permission_classes,
  73. )
  74. urls = [
  75. path('', docs_view, name='docs-index'),
  76. path('schema.js', schema_js_view, name='schema-js')
  77. ]
  78. return include((urls, 'api-docs'), namespace='api-docs')