Python django.conf.settings._setup() Examples

The following are 9 code examples for showing how to use django.conf.settings._setup(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.conf.settings , or try the search function .

Example 1
Project: GTDWeb   Author: lanbing510   File: diffsettings.py    License: GNU General Public License v2.0 6 votes vote down vote up
def handle(self, **options):
        # Inspired by Postfix's "postconf -n".
        from django.conf import settings, global_settings

        # Because settings are imported lazily, we need to explicitly load them.
        settings._setup()

        user_settings = module_to_dict(settings._wrapped)
        default_settings = module_to_dict(global_settings)

        output = []
        for key in sorted(user_settings):
            if key not in default_settings:
                output.append("%s = %s  ###" % (key, user_settings[key]))
            elif user_settings[key] != default_settings[key]:
                output.append("%s = %s" % (key, user_settings[key]))
            elif options['all']:
                output.append("### %s = %s" % (key, user_settings[key]))
        return '\n'.join(output) 
Example 2
Project: luscan-devel   Author: blackye   File: diffsettings.py    License: GNU General Public License v2.0 6 votes vote down vote up
def handle_noargs(self, **options):
        # Inspired by Postfix's "postconf -n".
        from django.conf import settings, global_settings

        # Because settings are imported lazily, we need to explicitly load them.
        settings._setup()

        user_settings = module_to_dict(settings._wrapped)
        default_settings = module_to_dict(global_settings)

        output = []
        for key in sorted(user_settings.keys()):
            if key not in default_settings:
                output.append("%s = %s  ###" % (key, user_settings[key]))
            elif user_settings[key] != default_settings[key]:
                output.append("%s = %s" % (key, user_settings[key]))
        return '\n'.join(output) 
Example 3
Project: uwsgi_tasks   Author: Bahus   File: utils.py    License: MIT License 6 votes vote down vote up
def django_setup(settings_module=None):
    """Initialize Django if required, must be run before performing
    any task on spooler or mule.
    """
    from django.conf import settings, ENVIRONMENT_VARIABLE

    if settings.configured:
        return

    if settings_module:
        import os
        os.environ[ENVIRONMENT_VARIABLE] = settings_module

    try:
        # django > 1.7
        from django import setup
    except ImportError:
        # django < 1.7
        def setup():
            settings._setup()

    setup() 
Example 4
Project: pytest-services   Author: pytest-dev   File: django_settings.py    License: MIT License 5 votes vote down vote up
def setup_django_settings(test_settings):
    """Override the enviroment variable and call the _setup method of the settings object to reload them."""
    os.environ['DJANGO_SETTINGS_MODULE'] = test_settings

    from django.conf import settings as django_settings

    # (re)setup django settings
    django_settings._setup()

    # reload settings
    reload_settings(django_settings) 
Example 5
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_no_settings_module(self):
        msg = (
            'Requested setting%s, but settings are not configured. You '
            'must either define the environment variable DJANGO_SETTINGS_MODULE '
            'or call settings.configure() before accessing settings.'
        )
        orig_settings = os.environ[ENVIRONMENT_VARIABLE]
        os.environ[ENVIRONMENT_VARIABLE] = ''
        try:
            with self.assertRaisesMessage(ImproperlyConfigured, msg % 's'):
                settings._setup()
            with self.assertRaisesMessage(ImproperlyConfigured, msg % ' TEST'):
                settings._setup('TEST')
        finally:
            os.environ[ENVIRONMENT_VARIABLE] = orig_settings 
Example 6
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_incorrect_timezone(self):
        with self.assertRaisesMessage(ValueError, 'Incorrect timezone setting: test'):
            settings._setup() 
Example 7
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_no_settings_module(self):
        msg = (
            'Requested setting%s, but settings are not configured. You '
            'must either define the environment variable DJANGO_SETTINGS_MODULE '
            'or call settings.configure() before accessing settings.'
        )
        orig_settings = os.environ[ENVIRONMENT_VARIABLE]
        os.environ[ENVIRONMENT_VARIABLE] = ''
        try:
            with self.assertRaisesMessage(ImproperlyConfigured, msg % 's'):
                settings._setup()
            with self.assertRaisesMessage(ImproperlyConfigured, msg % ' TEST'):
                settings._setup('TEST')
        finally:
            os.environ[ENVIRONMENT_VARIABLE] = orig_settings 
Example 8
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_incorrect_timezone(self):
        with self.assertRaisesMessage(ValueError, 'Incorrect timezone setting: test'):
            settings._setup() 
Example 9
Project: django-stubs   Author: typeddjango   File: context.py    License: MIT License 5 votes vote down vote up
def initialize_django(settings_module: str) -> Tuple['Apps', 'LazySettings']:
    with temp_environ():
        os.environ['DJANGO_SETTINGS_MODULE'] = settings_module

        # add current directory to sys.path
        sys.path.append(os.getcwd())

        def noop_class_getitem(cls, key):
            return cls

        from django.db import models

        models.QuerySet.__class_getitem__ = classmethod(noop_class_getitem)  # type: ignore
        models.Manager.__class_getitem__ = classmethod(noop_class_getitem)  # type: ignore

        from django.conf import settings
        from django.apps import apps

        apps.get_models.cache_clear()  # type: ignore
        apps.get_swappable_settings_name.cache_clear()  # type: ignore

        if not settings.configured:
            settings._setup()

        apps.populate(settings.INSTALLED_APPS)

    assert apps.apps_ready
    assert settings.configured

    return apps, settings