From fc123730f59b84fc01e4a922381d6c52f975ea86 Mon Sep 17 00:00:00 2001 From: Tanner Prestegard <tanner.prestegard@ligo.org> Date: Wed, 13 Mar 2019 09:17:16 -0500 Subject: [PATCH] Add function to settings for getting environment variables --- config/settings/base.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/config/settings/base.py b/config/settings/base.py index edf198be8..9fb7c97f5 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -3,6 +3,7 @@ from datetime import datetime, timedelta import os, time, logging from os.path import abspath, dirname, join import socket +from django.core.exceptions import ImproperlyConfigured # Set up path to root of project BASE_DIR = abspath(join(dirname(__file__), "..", "..")) @@ -12,6 +13,14 @@ PROJECT_ROOT = join(BASE_DIR, "gracedb") # Other useful paths PROJECT_DATA_DIR = join(BASE_DIR, "..", "project_data") +# Useful function for getting environment variables +def get_from_env(envvar, default_value=None, fail_if_not_found=True): + value = os.environ.get(envvar, default_value) + if (value == default_value and fail_if_not_found): + raise ImproperlyConfigured( + 'Could not get environment variable {0}'.format(envvar)) + return value + # Unauthenticated access ------------------------------------------------------ # This variable controls whether unauthenticated access is allowed *ANYWHERE* # on this service, except the home page, which is always public. -- GitLab