Debian Codegraph @

src/whitenoise/runserver_nostatic/management/commands/runserver.py

raw
1 """
2 Subclass the existing 'runserver' command and change the default options
3 to disable static file serving, allowing WhiteNoise to handle static files.
4
5 There is some unpleasant hackery here because we don't know which command class
6 to subclass until runtime as it depends on which INSTALLED_APPS we have, so we
7 have to determine this dynamically.
8 """
9
10 from __future__ import annotations
11
12 from importlib import import_module
13
14 from django.apps import apps
15
16
17 def get_next_runserver_command():
18 """
19 Return the next highest priority "runserver" command class
20 """
21 for app_name in get_lower_priority_apps():
22 module_path = "%s.management.commands.runserver" % app_name
23 try:
24 return import_module(module_path).Command
25 except (ImportError, AttributeError):
26 pass
27
28
29 def get_lower_priority_apps():
30 """
31 Yield all app module names below the current app in the INSTALLED_APPS list
32 """
33 self_app_name = ".".join(__name__.split(".")[:-3])
34 reached_self = False
35 for app_config in apps.get_app_configs():
36 if app_config.name == self_app_name:
37 reached_self = True
38 elif reached_self:
39 yield app_config.name
40 yield "django.core"
41
42
43 RunserverCommand = get_next_runserver_command()
44
45
46 class Command(RunserverCommand):
47 def add_arguments(self, parser):
48 super().add_arguments(parser)
49 if parser.get_default("use_static_handler") is True:
50 parser.set_defaults(use_static_handler=False)
51 parser.description += (
52 "\n(Wrapped by 'whitenoise.runserver_nostatic' to always"
53 " enable '--nostatic')"
54 )