Fixes #21833 -- Fix UserSettingsHolder.is_overridden() and add tests.

This commit is contained in:
Carl Meyer 2014-01-20 20:25:33 -07:00
parent 56516ade5e
commit 6070a5d6a6
2 changed files with 35 additions and 6 deletions

View File

@ -108,7 +108,6 @@ class Settings(BaseSettings):
isinstance(setting_value, six.string_types)):
raise ImproperlyConfigured("The %s setting must be a tuple. "
"Please fix your settings." % setting)
setattr(self, setting, setting_value)
self._explicit_settings.add(setting)
@ -164,9 +163,9 @@ class UserSettingsHolder(BaseSettings):
return list(self.__dict__) + dir(self.default_settings)
def is_overridden(self, setting):
if setting in self._deleted:
return False
else:
return self.default_settings.is_overridden(setting)
deleted = (setting in self._deleted)
set_locally = (setting in self.__dict__)
set_on_default = getattr(self.default_settings, 'is_overridden', lambda s: False)(setting)
return (deleted or set_locally or set_on_default)
settings = LazySettings()

View File

@ -1,8 +1,10 @@
import os
import sys
from types import ModuleType
import unittest
import warnings
from django.conf import settings
from django.conf import LazySettings, Settings, settings
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpRequest
from django.test import (SimpleTestCase, TransactionTestCase, TestCase,
@ -404,3 +406,31 @@ class SecureProxySslHeaderTest(TestCase):
req = HttpRequest()
req.META['HTTP_X_FORWARDED_PROTOCOL'] = 'https'
self.assertEqual(req.is_secure(), True)
class IsOverriddenTest(TestCase):
def test_configure(self):
s = LazySettings()
s.configure(SECRET_KEY='foo')
self.assertTrue(s.is_overridden('SECRET_KEY'))
def test_module(self):
settings_module = ModuleType('fake_settings_module')
settings_module.SECRET_KEY = 'foo'
sys.modules['fake_settings_module'] = settings_module
try:
s = Settings('fake_settings_module')
self.assertTrue(s.is_overridden('SECRET_KEY'))
self.assertFalse(s.is_overridden('TEMPLATE_LOADERS'))
finally:
del sys.modules['fake_settings_module']
def test_override(self):
self.assertFalse(settings.is_overridden('TEMPLATE_LOADERS'))
with override_settings(TEMPLATE_LOADERS=[]):
self.assertTrue(settings.is_overridden('TEMPLATE_LOADERS'))