Skip to content

Commit

Permalink
Add global --configuration option in Django >= 1.8.
Browse files Browse the repository at this point in the history
  • Loading branch information
jezdez committed Feb 13, 2015
1 parent 9be0c4f commit f35e7e5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
31 changes: 20 additions & 11 deletions configurations/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import logging
import os
import sys
from optparse import make_option
from optparse import OptionParser, make_option

from django import VERSION as DJ_VERSION
from django import VERSION as DJANGO_VERSION
from django.conf import ENVIRONMENT_VARIABLE as SETTINGS_ENVIRONMENT_VARIABLE
from django.core.exceptions import ImproperlyConfigured
from django.core.management import base
Expand All @@ -16,10 +16,10 @@

CONFIGURATION_ENVIRONMENT_VARIABLE = 'DJANGO_CONFIGURATION'
CONFIGURATION_ARGUMENT = '--configuration'
CONFIGURATION_ARGUMENT_HELP = ('The name of the configuration class to load, e.g. '
'"Development". If this isn\'t provided, the '
'DJANGO_CONFIGURATION environment variable will '
'be used.')
CONFIGURATION_ARGUMENT_HELP = ('The name of the configuration class to load, '
'e.g. "Development". If this isn\'t provided, '
'the DJANGO_CONFIGURATION environment '
'variable will be used.')


configuration_options = (make_option(CONFIGURATION_ARGUMENT,
Expand All @@ -29,8 +29,17 @@
def install(check_options=False):
global installed
if not installed:
if DJ_VERSION >= (1, 8):
pass
if DJANGO_VERSION >= (1, 8):
orig_create_parser = base.BaseCommand.create_parser

def create_parser(self, prog_name, subcommand):
parser = orig_create_parser(self, prog_name, subcommand)
if not isinstance(parser, OptionParser):
# probably argparse, let's not import argparse though
parser.add_argument(CONFIGURATION_ARGUMENT,
help=CONFIGURATION_ARGUMENT_HELP)
return parser
base.BaseCommand.create_parser = create_parser
else:
# add the configuration option to all management commands
base.BaseCommand.option_list += configuration_options
Expand Down Expand Up @@ -71,10 +80,10 @@ def name(self):

def check_options(self):
# django switched to argparse in version 1.8
if DJ_VERSION >= (1, 8):
if DJANGO_VERSION >= (1, 8):
parser = base.CommandParser(None,
usage="%(prog)s subcommand [options] [args]",
add_help=False)
usage="%(prog)s subcommand [options] [args]",
add_help=False)
parser.add_argument('--settings')
parser.add_argument('--pythonpath')
parser.add_argument(CONFIGURATION_ARGUMENT,
Expand Down
17 changes: 0 additions & 17 deletions tests/test_cli.py

This file was deleted.

17 changes: 17 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import subprocess
import sys

from django.conf import global_settings
Expand All @@ -9,6 +10,9 @@

from configurations.importer import ConfigurationImporter

ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
TEST_PROJECT_DIR = os.path.join(ROOT_DIR, 'test_project')


class MainTests(TestCase):

Expand Down Expand Up @@ -92,3 +96,16 @@ def test_configuration_option(self):
importer = ConfigurationImporter(check_options=True)
self.assertEqual(importer.module, 'tests.settings.main')
self.assertEqual(importer.name, 'Test')

def test_configuration_argument_in_cli(self):
"""
Verify that's configuration option has been added to managements
commands
"""
manage_args = ['python', os.path.join(ROOT_DIR, 'manage.py')]
proc = subprocess.Popen(manage_args + ['test', '--help'],
stdout=subprocess.PIPE)
self.assertIn('--configuration', proc.communicate()[0])
proc = subprocess.Popen(manage_args + ['runserver', '--help'],
stdout=subprocess.PIPE)
self.assertIn('--configuration', proc.communicate()[0])

0 comments on commit f35e7e5

Please sign in to comment.