Skip to content

Instantly share code, notes, and snippets.

@aubricus
Last active March 8, 2017 01:21
Show Gist options
  • Select an option

  • Save aubricus/33d3b9e75a176d5a65a57f8f7731143f to your computer and use it in GitHub Desktop.

Select an option

Save aubricus/33d3b9e75a176d5a65a57f8f7731143f to your computer and use it in GitHub Desktop.

Revisions

  1. aubricus revised this gist Mar 8, 2017. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions settings.py
    Original 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",
    })
  2. aubricus created this gist Mar 8, 2017.
    41 changes: 41 additions & 0 deletions django-app-settings.py
    Original 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