From 6070a5d6a6950bdbd93853c4b1636c7eec820b20 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Mon, 20 Jan 2014 20:25:33 -0700 Subject: [PATCH] Fixes #21833 -- Fix UserSettingsHolder.is_overridden() and add tests. --- django/conf/__init__.py | 9 ++++----- tests/settings_tests/tests.py | 32 +++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/django/conf/__init__.py b/django/conf/__init__.py index aad67bd7f5..012df218b8 100644 --- a/django/conf/__init__.py +++ b/django/conf/__init__.py @@ -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() diff --git a/tests/settings_tests/tests.py b/tests/settings_tests/tests.py index 9b2f5c79fa..4ff6938f58 100644 --- a/tests/settings_tests/tests.py +++ b/tests/settings_tests/tests.py @@ -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'))