Skip to content

Update the management command to use argparse for Django >= 1.8 #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 58 additions & 22 deletions devserver/management/commands/runserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,44 @@ def __init__(self, *args, **kwargs):
httpd.serve_forever()


ADDITIONAL_ARGUMENTS = {
'--werkzeug': dict(
action='store_true',
dest='use_werkzeug',
default=False,
help='Tells Django to use the Werkzeug interactive debugger.'),
'--forked': dict(
action='store_true',
dest='use_forked',
default=False,
help='Use forking instead of threading for multiple web requests.'),
'--dozer': dict(
action='store_true',
dest='use_dozer',
default=False,
help='Enable the Dozer memory debugging middleware.'),
'--wsgi-app': dict(
dest='wsgi_app',
default=None,
help='Load the specified WSGI app as the server endpoint.'),
}

if any(map(lambda app: app in settings.INSTALLED_APPS, STATICFILES_APPS)):
ADDITIONAL_ARGUMENTS.update({
'--nostatic': dict(
dest='use_static_files',
action='store_false',
default=True,
help='Tells Django to NOT automatically serve static files at STATIC_URL.')
})

class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option(
'--werkzeug', action='store_true', dest='use_werkzeug', default=False,
help='Tells Django to use the Werkzeug interactive debugger.'),
make_option(
'--forked', action='store_true', dest='use_forked', default=False,
help='Use forking instead of threading for multiple web requests.'),
make_option(
'--dozer', action='store_true', dest='use_dozer', default=False,
help='Enable the Dozer memory debugging middleware.'),
make_option(
'--wsgi-app', dest='wsgi_app', default=None,
help='Load the specified WSGI app as the server endpoint.'),
)
if any(map(lambda app: app in settings.INSTALLED_APPS, STATICFILES_APPS)):
option_list += make_option(
'--nostatic', dest='use_static_files', action='store_false', default=True,
help='Tells Django to NOT automatically serve static files at STATIC_URL.'),
if BaseCommand.option_list:
# Handle Django < 1.8
option_list = BaseCommand.option_list + (
make_option(name, **kwargs)
for name, kwargs in ADDITIONAL_ARGUMENTS.items()
)

help = "Starts a lightweight Web server for development which outputs additional debug information."
args = '[optional port number, or ipaddr:port]'
Expand All @@ -72,11 +91,16 @@ def __init__(self):
# `requires_system_checks`. If both options are present, an error is
# raised. BaseCommand sets requires_system_checks in >= Django 1.7.
if hasattr(self, 'requires_system_checks'):
requires_system_checks = False
self.requires_system_checks = False
else:
requires_model_validation = False # Django < 1.7
self.requires_model_validation = False # Django < 1.7
super(Command, self).__init__()

def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
for name, kwargs in ADDITIONAL_ARGUMENTS.items():
parser.add_argument(name, **kwargs)

def run_from_argv(self, argv):
parser = self.create_parser(argv[0], argv[1])
default_args = getattr(settings, 'DEVSERVER_ARGS', None)
Expand All @@ -85,12 +109,24 @@ def run_from_argv(self, argv):
else:
options = None

options, args = parser.parse_args(argv[2:], options)
if getattr(self, 'use_argparse', False):
options = parser.parse_args(argv[2:], options)
cmd_options = vars(options)
args = cmd_options.pop('args', ())
else:
options, args = parser.parse_args(argv[2:], options)
cmd_options = vars(options)

handle_default_options(options)
self.execute(*args, **options.__dict__)

def handle(self, addrport='', *args, **options):
def handle(self, *args, **options):
options.pop('addrport', None)
if args:
addrport, args = args[0], args[1:]
else:
addrport, args = '', args

if args:
raise CommandError('Usage is runserver %s' % self.args)

Expand Down