Skip to content

Commit

Permalink
Merge branch 'R5180_default_values'
Browse files Browse the repository at this point in the history
  • Loading branch information
playpauseandstop committed Nov 13, 2011
2 parents b2edd5f + 4a5d9eb commit 8ca768e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``.
Expand Down
21 changes: 19 additions & 2 deletions setman/lazy.py
Original file line number Diff line number Diff line change
@@ -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', )
Expand All @@ -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):
Expand All @@ -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()
Expand Down
16 changes: 14 additions & 2 deletions testproject/core/tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down

0 comments on commit 8ca768e

Please sign in to comment.