forked from miracle2k/webassets
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expanded on our settings wrapper to allow setting values as well; it …
…should now transparently wrap the Django settings object. Added tests for this behavior.
- Loading branch information
Showing
2 changed files
with
38 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |