app default settings

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#conf/__init__.py:

import time
import os
import imp
from django.conf import global_settings, LazySettings, ENVIRONMENT_VARIABLE
from django.utils.importlib import import_module


class Settings(object):
    def __init__(self, settings_module):
        self.set_from_module(global_settings)

        # store the settings module in case someone later cares
        self.SETTINGS_MODULE = settings_module
        
        try:
            project_settings = import_module(self.SETTINGS_MODULE)
        except ImportError, e:
            raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)

        # automatically set defaults from settings in app dir
        for app in getattr(project_settings, 'INSTALLED_APPS', []):
            try:
                app_path = import_module(app).__path__
            except AttributeError:
                continue
            try:
                imp.find_module('settings', app_path)
            except ImportError:
                continue
            app_settings = import_module('%s.settings' % app)
            self.set_from_module(app_settings)
        
        self.set_from_module(project_settings)

        if hasattr(time, 'tzset'):
            # Move the time zone info into os.environ. See ticket #2315 for why
            # we don't do this unconditionally (breaks Windows).
            os.environ['TZ'] = self.TIME_ZONE
            time.tzset()

    def set_from_module(self, mod):
        for setting in dir(mod):
            if setting == setting.upper():
                setattr(self, setting, getattr(mod, setting))

    def get_all_members(self):
        return dir(self)


class LazySettings(LazySettings):
    def _setup(self):
        """
        Load the settings module pointed to by the environment variable. This
        is used the first time we need any settings at all, if the user has not
        previously configured the settings manually.
        """
        try:
            settings_module = os.environ[ENVIRONMENT_VARIABLE]
            if not settings_module: # If it's set but is an empty string.
                raise KeyError
        except KeyError:
            # NOTE: This is arguably an EnvironmentError, but that causes
            # problems with Python's interactive help.
            raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)

        self._wrapped = Settings(settings_module)

settings = LazySettings()

Comments

(Forgotten your password?)

You may use Markdown syntax here, but raw HTML will be removed.