Ticket #14297: ticket_14297.diff

File ticket_14297.diff, 5.9 KB (added by Luke Plant, 14 years ago)
  • django/conf/__init__.py

    diff -r 87cceb6af4c4 django/conf/__init__.py
    a b  
    1616
    1717ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE"
    1818
    19 class LazySettings(LazyObject):
     19
     20class LazySettings(object):
    2021    """
    2122    A lazy proxy for either global Django settings or a custom settings object.
    2223    The user can manually configure settings prior to using them. Otherwise,
    2324    Django uses the settings module pointed to by DJANGO_SETTINGS_MODULE.
    2425    """
     26    def __init__(self):
     27        self.configured = False
     28
     29    def __getattr__(self, name):
     30        if self.configured:
     31            raise AttributeError("Settings object has no attribute '%s'" % name)
     32        else:
     33            self._setup()
     34            return getattr(self, name)
     35
     36    def __dir__(self):
     37        if not self.configured:
     38            self._setup()
     39        return self.__dict__.keys()
     40
     41    # For Python < 2.6:
     42    __members__ = property(lambda self: self.__dir__())
     43
    2544    def _setup(self):
    2645        """
    2746        Load the settings module pointed to by the environment variable. This
     
    3756            # problems with Python's interactive help.
    3857            raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
    3958
    40         self._wrapped = Settings(settings_module)
     59        self._load(Settings(settings_module))
     60        self.configured = True
     61
     62    def _load(self, obj):
     63        for name in dir(obj):
     64            if not name.startswith('__'):
     65                setattr(self, name, getattr(obj, name))
    4166
    4267    def configure(self, default_settings=global_settings, **options):
    4368        """
     
    4570        parameter sets where to retrieve any unspecified values from (its
    4671        argument must support attribute access (__getattr__)).
    4772        """
    48         if self._wrapped != None:
     73        if self.configured:
    4974            raise RuntimeError('Settings already configured.')
    50         holder = UserSettingsHolder(default_settings)
    51         for name, value in options.items():
    52             setattr(holder, name, value)
    53         self._wrapped = holder
     75        self._load(default_settings)
     76        self.__dict__.update(options)
     77        self.configured = True
    5478
    55     def configured(self):
    56         """
    57         Returns True if the settings have already been configured.
    58         """
    59         return bool(self._wrapped)
    60     configured = property(configured)
    6179
    6280class Settings(object):
    6381    def __init__(self, settings_module):
     
    114132            os.environ['TZ'] = self.TIME_ZONE
    115133            time.tzset()
    116134
    117 class UserSettingsHolder(object):
    118     """
    119     Holder for user configured settings.
    120     """
    121     # SETTINGS_MODULE doesn't make much sense in the manually configured
    122     # (standalone) case.
    123     SETTINGS_MODULE = None
    124 
    125     def __init__(self, default_settings):
    126         """
    127         Requests for configuration variables not in this class are satisfied
    128         from the module specified in default_settings (if possible).
    129         """
    130         self.default_settings = default_settings
    131 
    132     def __getattr__(self, name):
    133         return getattr(self.default_settings, name)
    134 
    135     def __dir__(self):
    136         return self.__dict__.keys() + dir(self.default_settings)
    137 
    138     # For Python < 2.6:
    139     __members__ = property(lambda self: self.__dir__())
    140135
    141136settings = LazySettings()
    142137
  • django/contrib/messages/tests/base.py

    diff -r 87cceb6af4c4 django/contrib/messages/tests/base.py
    a b  
    4040        for setting in self.restore_settings:
    4141            if hasattr(settings, setting):
    4242                self._remembered_settings[setting] = getattr(settings, setting)
    43                 delattr(settings._wrapped, setting)
     43                delattr(settings, setting)
    4444        # Backup these manually because we do not want them deleted.
    4545        self._middleware_classes = settings.MIDDLEWARE_CLASSES
    4646        self._template_context_processors = \
     
    6565            value = self._remembered_settings.pop(setting)
    6666            setattr(settings, setting, value)
    6767        elif hasattr(settings, setting):
    68             delattr(settings._wrapped, setting)
     68            delattr(settings, setting)
    6969
    7070    def get_request(self):
    7171        return http.HttpRequest()
  • tests/regressiontests/comment_tests/tests/app_api_tests.py

    diff -r 87cceb6af4c4 tests/regressiontests/comment_tests/tests/app_api_tests.py
    a b  
    4141        del settings.INSTALLED_APPS[-1]
    4242        settings.COMMENTS_APP = self.old_comments_app
    4343        if settings.COMMENTS_APP is None:
    44             del settings._wrapped.COMMENTS_APP
     44            del settings.COMMENTS_APP
    4545
    4646    def testGetCommentApp(self):
    4747        from regressiontests.comment_tests import custom_comments
  • tests/regressiontests/settings_tests/tests.py

    diff -r 87cceb6af4c4 tests/regressiontests/settings_tests/tests.py
    a b  
    77    # Regression tests for #10130: deleting settings.
    88    #
    99
     10    def setUp(self):
     11        self.DEBUG = settings.DEBUG
     12
     13    def tearDown(self):
     14        settings.DEBUG = self.DEBUG
     15
    1016    def test_settings_delete(self):
    1117        settings.TEST = 'test'
    1218        self.assertEqual('test', settings.TEST)
    1319        del settings.TEST
    1420        self.assertRaises(AttributeError, getattr, settings, 'TEST')
    1521
    16     def test_settings_delete_wrapped(self):
    17         self.assertRaises(TypeError, delattr, settings, '_wrapped')
     22        # Check with something that will definitely already exist
     23        # in the settings module
     24        del settings.DEBUG
     25        self.assertRaises(AttributeError, getattr, settings, 'DEBUG')
Back to Top