Skip to content

Commit

Permalink
Add custom settings to CLI (view parity) #892
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Dec 10, 2022
1 parent 51e2905 commit 720f14c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
18 changes: 13 additions & 5 deletions drf_spectacular/management/commands/spectacular.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from drf_spectacular.drainage import GENERATOR_STATS
from drf_spectacular.renderers import OpenApiJsonRenderer, OpenApiYamlRenderer
from drf_spectacular.settings import spectacular_settings
from drf_spectacular.settings import patched_settings, spectacular_settings
from drf_spectacular.validation import validate_schema


Expand Down Expand Up @@ -41,6 +41,7 @@ def add_arguments(self, parser):
parser.add_argument('--api-version', dest="api_version", default=None, type=str)
parser.add_argument('--lang', dest="lang", default=None, type=str)
parser.add_argument('--color', dest="color", default=False, action='store_true')
parser.add_argument('--custom-settings', dest="custom_settings", default=None, type=str)

def handle(self, *args, **options):
if options['generator_class']:
Expand All @@ -55,11 +56,18 @@ def handle(self, *args, **options):
urlconf=options['urlconf'],
api_version=options['api_version'],
)
if options['lang']:
with translation.override(options['lang']):
schema = generator.get_schema(request=None, public=True)

if options['custom_settings']:
custom_settings = import_string(options['custom_settings'])
else:
schema = generator.get_schema(request=None, public=True)
custom_settings = None

with patched_settings(custom_settings):
if options['lang']:
with translation.override(options['lang']):
schema = generator.get_schema(request=None, public=True)
else:
schema = generator.get_schema(request=None, public=True)

GENERATOR_STATS.emit_summary()

Expand Down
8 changes: 8 additions & 0 deletions tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ def test_command_color(capsys):
GENERATOR_STATS._yellow = GENERATOR_STATS._clear = ''


CUSTOM = {'DESCRIPTION': 'custom setting'}


def test_command_custom_settings(capsys):
management.call_command('spectacular', '--custom-settings=tests.test_command.CUSTOM')
assert 'description: custom setting' in capsys.readouterr().out


def test_command_check(capsys):
management.call_command('check', '--deploy')
stderr = capsys.readouterr().err
Expand Down

0 comments on commit 720f14c

Please sign in to comment.