Permalink
Browse files

Merge branch 'R5180_default_values'

  • Loading branch information...
2 parents b2edd5f + 4a5d9eb commit 8ca768e295fa45309eb58909336c78260b633192 @playpauseandstop playpauseandstop committed Nov 13, 2011
Showing with 34 additions and 4 deletions.
  1. +1 −0 CHANGES.rst
  2. +19 −2 setman/lazy.py
  3. +14 −2 testproject/core/tests/test_settings.py
View
@@ -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``.
View
@@ -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()
@@ -1,20 +1,32 @@
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', )
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):

0 comments on commit 8ca768e

Please sign in to comment.