import copy

from django.utils.functional import LazyObject, empty

from .default import HORIZON_CONFIG as DEFAULT_CONFIG


class LazySettings(LazyObject):
    def _setup(self, name=None):
        from django.conf import settings
        HORIZON_CONFIG = copy.copy(DEFAULT_CONFIG)
        HORIZON_CONFIG.update(settings.HORIZON_CONFIG)

        # Ensure we always have our exception configuration...
        for exc_category in ['unauthorized', 'not_found', 'recoverable']:
            if exc_category not in HORIZON_CONFIG['exceptions']:
                default_exc_config = DEFAULT_CONFIG['exceptions'][exc_category]
                HORIZON_CONFIG['exceptions'][exc_category] = default_exc_config

        # Ensure our password validator always exists...
        if 'regex' not in HORIZON_CONFIG['password_validator']:
            default_pw_regex = DEFAULT_CONFIG['password_validator']['regex']
            HORIZON_CONFIG['password_validator']['regex'] = default_pw_regex
        if 'help_text' not in HORIZON_CONFIG['password_validator']:
            default_pw_help = DEFAULT_CONFIG['password_validator']['help_text']
            HORIZON_CONFIG['password_validator']['help_text'] = default_pw_help

        self._wrapped = HORIZON_CONFIG

    def __getitem__(self, name, fallback=None):
        if self._wrapped is empty:
            self._setup(name)
        return self._wrapped.get(name, fallback)

HORIZON_CONFIG = LazySettings()

import copy

from django.utils.functional import LazyObject, empty


class LazySettings(LazyObject):
    def _setup(self, name=None):
        from django.conf import settings
        from .default import HORIZON_CONFIG as DEFAULT_CONFIG
        HORIZON_CONFIG = copy.copy(DEFAULT_CONFIG)
        HORIZON_CONFIG.update(settings.HORIZON_CONFIG)

        # Ensure we always have our exception configuration...
        for exc_category in ['unauthorized', 'not_found', 'recoverable']:
            if exc_category not in HORIZON_CONFIG['exceptions']:
                default_exc_config = DEFAULT_CONFIG['exceptions'][exc_category]
                HORIZON_CONFIG['exceptions'][exc_category] = default_exc_config

        # Ensure our password validator always exists...
        if 'regex' not in HORIZON_CONFIG['password_validator']:
            default_pw_regex = DEFAULT_CONFIG['password_validator']['regex']
            HORIZON_CONFIG['password_validator']['regex'] = default_pw_regex
        if 'help_text' not in HORIZON_CONFIG['password_validator']:
            default_pw_help = DEFAULT_CONFIG['password_validator']['help_text']
            HORIZON_CONFIG['password_validator']['help_text'] = default_pw_help

        self._wrapped = HORIZON_CONFIG

    def __getitem__(self, name, fallback=None):
        if self._wrapped is empty:
            self._setup(name)
        return self._wrapped.get(name, fallback)

HORIZON_CONFIG = LazySettings()

Fix circular dependencies in dashboard settings
Fix circular dependencies in dashboard settings

Importing horizon.utils from dashboard local_settings.py to generate
SECRET_KEY results in a sequence of imports, and horizon.conf.default
module gets imported at some point. During initialization of default
HORIZON_CONFIG this module uses settings.LOGIN_REDIRECT_URL and
ugettext() call. Both of them need django settings to be ready to use,
therefore settings initialization starts again before it could finish.

Since Python processes module only when it is imported the first time,
this process stops, but the 'inner' settings object contains only
parameters that were set above the point of import of local_settings.
Therefore Django complains about missing SECRET_KEY when it processes
'inner' settings.

The fix moves the import of horizon.conf.default to
LazySetting._setup(). If keys of HORIZON_CONFIG obtained from
horizon.conf are not used within openstask_dashboard settings.py
or local_settings.py, the circular import won't happen.

Fixes bug #1154564

Change-Id: If63ab1920ecc8e646fd5b6cc52c106ae0876fa2d
