Last active
March 8, 2017 01:21
-
-
Save aubricus/33d3b9e75a176d5a65a57f8f7731143f to your computer and use it in GitHub Desktop.
Revisions
-
aubricus revised this gist
Mar 8, 2017 . 1 changed file with 12 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ # ... Other django settings above FOOBAR_SETTINGS = { "FOO": "http://example.com", "BAR": "animals.kittens", } # How to override ... FOOBAR_SETTINGS.update({ "FOO": "http://google.com", }) -
aubricus created this gist
Mar 8, 2017 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ """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