4

In an attempt to try to generalize a django app, so it can be run with different configurations defined in yaml files, I wanted to extend the runserver command to take the locations of these configuration files as arguments.

I followed a guide by Chase Seibert, but when I try to run the command with my "added" arguments, it doesn't seem to work.

I have a feeling it is because of the location of the management/commands directory, but moving that around (with the __init__.py isn't helping).

Any suggestions? Thanks!

My app structure

my_app/
    __init__.py
    static/
    templates/
    manage.py
    app1/
    app2/
    my_app/
        __init__.py
        management/
            __init__.py
            commands/
                __init__.py
                runserver.py
        models.py
        views.py
        settings.py
        urls.py

My extension of runserver

from django.core.management import call_command
from django.core.management.base import BaseCommand
from django.core.management.commands.runserver import BaseRunserverCommand

from .env import PATH_CFG_SIM, PATH_CFG_PARAM


class Command(BaseCommand):
    ''' Runs the built-in Django runserver with initial schema and fixtures
    Note: cannot use auto-reloading because that casues resetdb to be called
    twice.
    '''

    # remove this option from the --help for this command
    option_list = (o for o in BaseRunserverCommand.option_list if o.dest != 'use_reloader')
    help = 'Starts a lightweight web server testing.'
    args = '[optional port number, or ipaddr:port] appname appname'

    def add_arguments(self, parser):
        super(Command, self).add_arguments(parser)
        parser.add_argument('--cfg-sim', action='store',
                            dest='path_cfg_sim',
                            help='Set path to simulation.yml configurations')
        parser.add_argument('--cfg-param', action='store',
                            dest='path_cfg_param',
                            help='Set path to parameters.yml configurations')


    def handle(self, addrport='', *args, **options):

        # Set path parameters if passed
        if options['path_cfg_sim']:
            PATH_CFG_SIM = options['path_cfg_sim']
        if options['path_cfg_param']:
            PATH_CFG_PARAM = options['path_cfg_param']

        call_command('syncdb', *args, **options)
        # BUG: runserver gets called twice if --noreload is False
        # https://code.djangoproject.com/ticket/8085
        options['use_reloader'] = False
        call_command('runserver', addrport, *args, **options)

        return None
ryanjdillon
  • 17,658
  • 9
  • 85
  • 110

0 Answers0