Server IP : 85.214.239.14 / Your IP : 3.12.150.240 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /srv/modoboa/env/lib/python3.5/site-packages/rest_framework/management/commands/ |
Upload File : |
from django.core.management.base import BaseCommand from django.utils.module_loading import import_string from rest_framework import renderers from rest_framework.schemas import coreapi from rest_framework.schemas.openapi import SchemaGenerator OPENAPI_MODE = 'openapi' COREAPI_MODE = 'coreapi' class Command(BaseCommand): help = "Generates configured API schema for project." def get_mode(self): return COREAPI_MODE if coreapi.is_enabled() else OPENAPI_MODE def add_arguments(self, parser): parser.add_argument('--title', dest="title", default='', type=str) parser.add_argument('--url', dest="url", default=None, type=str) parser.add_argument('--description', dest="description", default=None, type=str) 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) parser.add_argument('--urlconf', dest="urlconf", default=None, type=str) parser.add_argument('--generator_class', dest="generator_class", default=None, type=str) def handle(self, *args, **options): if options['generator_class']: generator_class = import_string(options['generator_class']) else: generator_class = self.get_generator_class() generator = generator_class( url=options['url'], title=options['title'], description=options['description'], urlconf=options['urlconf'], ) schema = generator.get_schema(request=None, public=True) renderer = self.get_renderer(options['format']) output = renderer.render(schema, renderer_context={}) self.stdout.write(output.decode()) def get_renderer(self, format): if self.get_mode() == COREAPI_MODE: renderer_cls = { 'corejson': renderers.CoreJSONRenderer, 'openapi': renderers.CoreAPIOpenAPIRenderer, 'openapi-json': renderers.CoreAPIJSONOpenAPIRenderer, }[format] return renderer_cls() renderer_cls = { 'openapi': renderers.OpenAPIRenderer, 'openapi-json': renderers.JSONOpenAPIRenderer, }[format] return renderer_cls() def get_generator_class(self): if self.get_mode() == COREAPI_MODE: return coreapi.SchemaGenerator return SchemaGenerator