Skip to content

Commit

Permalink
Expanded on our settings wrapper to allow setting values as well; it …
Browse files Browse the repository at this point in the history
…should now transparently wrap the Django settings object. Added tests for this behavior.
  • Loading branch information
miracle2k committed Dec 8, 2009
1 parent ad5bf77 commit 7ff057e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
17 changes: 14 additions & 3 deletions django_assets/conf/__init__.py
@@ -1,18 +1,29 @@
from itertools import chain
import default as default_settings
from django.conf import settings as django_settings


class WrappedSettings(object):
"""Wraps around the Django settings, falling back to our own
default settings module.
"""
__slots__ = ('_live_settings', '_fallbacks',)

def __init__(self, *setting_objs):
self.setting_objs = setting_objs
def __init__(self, live_settings, *fallbacks):
self._live_settings = live_settings
self._fallbacks = fallbacks

def __getattr__(self, name):
for settings in self.setting_objs:
for settings in chain([self._live_settings], self._fallbacks):
if hasattr(settings, name):
return getattr(settings, name)
raise AttributeError()

def __setattr__(self, name, value):
if name in self.__slots__:
object.__setattr__(self, name, value)
else:
setattr(self._live_settings, name, value)


settings = WrappedSettings(django_settings, default_settings)
24 changes: 24 additions & 0 deletions test/test_conf.py
@@ -0,0 +1,24 @@
from django_assets import conf as assets_conf
from django import conf as django_conf


def test_read():
"""Test that we can read Django's own config values from our
own settings object.
"""
django_conf.settings.TOP_SECRET = 1234
assert assets_conf.settings.TOP_SECRET == 1234

# Test a again, to make sure that the settings object didn't
# just copy the data once on initialization.
django_conf.settings.TOP_SECRET = 'helloworld'
assert assets_conf.settings.TOP_SECRET == 'helloworld'


def test_write():
"""Test that changing a value to our configuration also updates
the original Django settings object.
"""
assert not getattr(django_conf.settings, 'FOOBAR', None) == 1234
assets_conf.settings.FOOBAR = 1234
assert django_conf.settings.FOOBAR == 1234

0 comments on commit 7ff057e

Please sign in to comment.