From 8061c52c142326b2c9e6597e428a5705b90a5a71 Mon Sep 17 00:00:00 2001 From: Igor Davydenko Date: Sun, 13 Nov 2011 23:19:10 +0100 Subject: [PATCH 1/3] Feature #5180: Check that default value for setting should be returned even it not stored to database. --- testproject/core/tests/test_settings.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/testproject/core/tests/test_settings.py b/testproject/core/tests/test_settings.py index d6559bd..20d5fa2 100644 --- a/testproject/core/tests/test_settings.py +++ b/testproject/core/tests/test_settings.py @@ -1,11 +1,12 @@ from django.conf import settings as django_settings +from django.core.cache import cache from django.core.exceptions import ValidationError from django.test import TestCase from setman import settings from setman.models import Settings -from test_models import TEST_SETTINGS +from testproject.core.tests.test_models import TEST_SETTINGS __all__ = ('TestGlobalSettings', ) @@ -13,8 +14,19 @@ class TestGlobalSettings(TestCase): - def tearDown(self): + def setUp(self): settings._clear() + cache.clear() + + def tearDown(self): + self.setUp() + + def test_default_values(self): + self.assertEqual(Settings.objects.count(), 0) + + for name, value in TEST_SETTINGS.items(): + self.assertTrue(hasattr(settings, name)) + self.assertEqual(getattr(settings, name), value) def test_empty_settings(self): for name in dir(django_settings): From 40e1f3514a9215eb971d4164e1c1c7de6958f7cf Mon Sep 17 00:00:00 2001 From: Igor Davydenko Date: Sun, 13 Nov 2011 23:19:41 +0100 Subject: [PATCH 2/3] Feature #5180: Implement support of returning default values for setting not stored to database. --- setman/lazy.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/setman/lazy.py b/setman/lazy.py index 9d3c762..20ae943 100644 --- a/setman/lazy.py +++ b/setman/lazy.py @@ -1,6 +1,7 @@ from django.conf import settings as django_settings from setman.models import Settings +from setman.utils import AVAILABLE_SETTINGS __all__ = ('LazySettings', ) @@ -14,21 +15,34 @@ class LazySettings(object): _custom = None def __getattr__(self, name): - if not name.isupper(): + """ + Add support for getting settings keys as instance attribute. + + For first try, method tries to read settings from database, then from + Django settings and if all fails try to return default value of + available setting from configuration definition file if any. + """ + if name.startswith('_'): return super(LazySettings, self).__getattr__(name) if self._custom is None: self._custom = self._get_custom_settings() + # Read setting from database if name in self._custom.data: return self._custom.data.get(name) + # Or from Django settings elif hasattr(django_settings, name): return getattr(django_settings, name) + # Or read default value from available settings + elif hasattr(AVAILABLE_SETTINGS, name): + return getattr(AVAILABLE_SETTINGS, name).default + # If cannot read setting - raise error raise AttributeError('Settings has not attribute %r' % name) def __setattr__(self, name, value): - if not name.isupper(): + if name.startswith('_'): return super(LazySettings, self).__setattr__(name, value) if hasattr(django_settings, name): @@ -39,6 +53,9 @@ def __setattr__(self, name, value): setattr(self._custom, name, value) def save(self): + """ + Save customized settings to the database. + """ if self._custom is None: self._custom = self._get_custom_settings() self._custom.save() From 4a5d9eb8125049b39bf3c75e3b6f01b322784d5b Mon Sep 17 00:00:00 2001 From: Igor Davydenko Date: Sun, 13 Nov 2011 23:20:01 +0100 Subject: [PATCH 3/3] Feature #5180: Update change list. --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index 68e247b..b30b2e7 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,6 +5,7 @@ django-setman Changes 0.2 (current) ------------- ++ Return default value for setting even it not stored to database. + Lazy process ``validators`` and ``choices`` setting attributes, don't care about import ordering. + Added labels support for ``choices`` value in ``ChoiceSetting``.