Skip to content

Commit

Permalink
Added strict mode to utils.setting() (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZuluPro authored and jschneier committed Aug 2, 2016
1 parent 7e5e58f commit c8e902b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
19 changes: 16 additions & 3 deletions storages/utils.py
@@ -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)
16 changes: 16 additions & 0 deletions tests/test_utils.py
@@ -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)

0 comments on commit c8e902b

Please sign in to comment.