Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added strict mode to utils.setting() (#176)
- Loading branch information
Showing
2 changed files
with
32 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,9 +1,22 @@ | ||
from django.conf import settings | ||
from django.core.exceptions import ImproperlyConfigured | ||
|
||
|
||
def setting(name, default=None): | ||
def setting(name, default=None, strict=False): | ||
""" | ||
Helper function to get a Django setting by name or (optionally) return | ||
a default (or else ``None``). | ||
Helper function to get a Django setting by name. If setting doesn't exists | ||
it can return a default or raise an error if in strict mode. | ||
:param name: Name of setting | ||
:type name: str | ||
:param default: Value if setting is unfound | ||
:param strict: Define if return default value or raise an error | ||
:type strict: bool | ||
:returns: Setting's value | ||
:raises: django.core.exceptions.ImproperlyConfigured if setting is unfound | ||
and strict mode | ||
""" | ||
if strict and not hasattr(settings, name): | ||
msg = "You must provide settings.%s" % name | ||
raise ImproperlyConfigured(msg) | ||
return getattr(settings, name, default) |
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,16 @@ | ||
from django.test import TestCase | ||
from django.conf import settings | ||
from django.core.exceptions import ImproperlyConfigured | ||
from storages import utils | ||
|
||
|
||
class SettingTest(TestCase): | ||
def test_get_setting(self): | ||
value = utils.setting('SECRET_KEY') | ||
self.assertEqual(settings.SECRET_KEY, value) | ||
|
||
def test_setting_unfound(self): | ||
self.assertIsNone(utils.setting('FOO')) | ||
self.assertEqual(utils.setting('FOO', 'bar'), 'bar') | ||
with self.assertRaises(ImproperlyConfigured): | ||
utils.setting('FOO', strict=True) |