2018-10-03 17:28:04 +03:00
|
|
|
from django.core.management.base import BaseCommand
|
2019-06-09 15:43:54 +03:00
|
|
|
from django.utils.module_loading import import_string
|
2018-10-03 17:28:04 +03:00
|
|
|
|
2019-05-13 17:07:03 +03:00
|
|
|
from rest_framework import renderers
|
|
|
|
from rest_framework.schemas import coreapi
|
|
|
|
from rest_framework.schemas.openapi import SchemaGenerator
|
|
|
|
|
|
|
|
OPENAPI_MODE = 'openapi'
|
|
|
|
COREAPI_MODE = 'coreapi'
|
2018-10-03 17:28:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = "Generates configured API schema for project."
|
|
|
|
|
2019-05-13 17:07:03 +03:00
|
|
|
def get_mode(self):
|
|
|
|
return COREAPI_MODE if coreapi.is_enabled() else OPENAPI_MODE
|
|
|
|
|
2018-10-03 17:28:04 +03:00
|
|
|
def add_arguments(self, parser):
|
2019-05-13 17:07:03 +03:00
|
|
|
parser.add_argument('--title', dest="title", default='', type=str)
|
2018-10-03 17:28:04 +03:00
|
|
|
parser.add_argument('--url', dest="url", default=None, type=str)
|
|
|
|
parser.add_argument('--description', dest="description", default=None, type=str)
|
2019-05-13 17:07:03 +03:00
|
|
|
if self.get_mode() == COREAPI_MODE:
|
|
|
|
parser.add_argument('--format', dest="format", choices=['openapi', 'openapi-json', 'corejson'], default='openapi', type=str)
|
|
|
|
else:
|
|
|
|
parser.add_argument('--format', dest="format", choices=['openapi', 'openapi-json'], default='openapi', type=str)
|
2019-06-09 16:23:52 +03:00
|
|
|
parser.add_argument('--urlconf', dest="urlconf", default=None, type=str)
|
2019-06-09 15:43:54 +03:00
|
|
|
parser.add_argument('--generator_class', dest="generator_class", default=None, type=str)
|
2020-02-12 22:35:54 +03:00
|
|
|
parser.add_argument('--file', dest="file", default=None, type=str)
|
2022-09-22 12:36:01 +03:00
|
|
|
parser.add_argument('--api_version', dest="api_version", default='', type=str)
|
2018-10-03 17:28:04 +03:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2019-06-09 15:43:54 +03:00
|
|
|
if options['generator_class']:
|
|
|
|
generator_class = import_string(options['generator_class'])
|
|
|
|
else:
|
|
|
|
generator_class = self.get_generator_class()
|
2019-05-13 17:07:03 +03:00
|
|
|
generator = generator_class(
|
2018-10-03 17:28:04 +03:00
|
|
|
url=options['url'],
|
|
|
|
title=options['title'],
|
2019-06-09 16:23:52 +03:00
|
|
|
description=options['description'],
|
|
|
|
urlconf=options['urlconf'],
|
2022-09-22 12:36:01 +03:00
|
|
|
version=options['api_version'],
|
2018-10-03 17:28:04 +03:00
|
|
|
)
|
|
|
|
schema = generator.get_schema(request=None, public=True)
|
|
|
|
renderer = self.get_renderer(options['format'])
|
|
|
|
output = renderer.render(schema, renderer_context={})
|
2020-02-12 22:35:54 +03:00
|
|
|
|
|
|
|
if options['file']:
|
|
|
|
with open(options['file'], 'wb') as f:
|
|
|
|
f.write(output)
|
|
|
|
else:
|
|
|
|
self.stdout.write(output.decode())
|
2018-10-03 17:28:04 +03:00
|
|
|
|
|
|
|
def get_renderer(self, format):
|
2019-05-13 17:07:03 +03:00
|
|
|
if self.get_mode() == COREAPI_MODE:
|
|
|
|
renderer_cls = {
|
|
|
|
'corejson': renderers.CoreJSONRenderer,
|
|
|
|
'openapi': renderers.CoreAPIOpenAPIRenderer,
|
|
|
|
'openapi-json': renderers.CoreAPIJSONOpenAPIRenderer,
|
|
|
|
}[format]
|
|
|
|
return renderer_cls()
|
|
|
|
|
2019-01-24 18:01:06 +03:00
|
|
|
renderer_cls = {
|
2019-05-13 17:07:03 +03:00
|
|
|
'openapi': renderers.OpenAPIRenderer,
|
|
|
|
'openapi-json': renderers.JSONOpenAPIRenderer,
|
2018-10-03 17:28:04 +03:00
|
|
|
}[format]
|
2019-01-24 18:01:06 +03:00
|
|
|
return renderer_cls()
|
2019-05-13 17:07:03 +03:00
|
|
|
|
|
|
|
def get_generator_class(self):
|
|
|
|
if self.get_mode() == COREAPI_MODE:
|
|
|
|
return coreapi.SchemaGenerator
|
|
|
|
return SchemaGenerator
|