Last active
March 8, 2017 01:21
-
-
Save aubricus/33d3b9e75a176d5a65a57f8f7731143f to your computer and use it in GitHub Desktop.
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
"""Collect settings for this app from django settings.""" | |
from django.core.exceptions import ImproperlyConfigured | |
class NotSetButRequired(object): | |
def __repr__(self): | |
return '<{0}>'.format(self.__class__.__name__) | |
NOT_SET_BUT_REQUIRED = NotSetButRequired() | |
SETTINGS = { | |
"FOO": NOT_SET_BUT_REQUIRED, | |
"BAR": "qux.baz", | |
} | |
def get_setting(setting_name): | |
from django.conf import settings | |
SETTINGS_USER = getattr(settings, "FOOBAR_SETTINGS", {}) | |
SETTING_NAME = setting_name.upper() | |
SETTINGS.update(SETTINGS_USER) | |
try: | |
value = SETTINGS[SETTING_NAME] | |
if value is NOT_SET_BUT_REQUIRED: | |
error_msg = "FOOBAR_SETTINGS['{}'] is a required setting but is unset".format(SETTING_NAME) | |
raise ImproperlyConfigured(error_msg) | |
else: | |
return value | |
except KeyError: | |
raise KeyError("Got unsupported settings key {}".format(SETTING_NAME)) | |
except Exception: | |
print("Caught unhandled exception while accessing FOOBAR_SETTINGS") | |
raise |
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
# ... Other django settings above | |
FOOBAR_SETTINGS = { | |
"FOO": "http://example.com", | |
"BAR": "animals.kittens", | |
} | |
# How to override ... | |
FOOBAR_SETTINGS.update({ | |
"FOO": "http://google.com", | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just a quick gist based on some generic django app settings:
get_setting
FOOBAR_SETTINGS