environment_settings.py 2.89 KB
Newer Older
Evili del Rio's avatar
Evili del Rio committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#
# Django Settings Module entirely based on defaults and environment
# First we import the default settings for the project that
# should reside in <projectname>.settings
# then we import the global_settings from django.conf
# superseeded by the default <projectname>.settings
# For each setting SETTING_NAME test if the corresponding
# environment variable DJANGO_SETTING_NAME is defined.
# If it is, we incorporate as a true Django Setting
# By default, we check if the global or default setting is a string, 
# if not we evaluate directly the value as a Python expression.
#
# Special Cases:
#   If DJANGO_DATABASES is not defined we look for alternate definition 
#   via DJANGO_DATABASE_ENGINE, DJANGO_DATABASE_HOST, DJANGO_DATABASE_NAME,
#   DJANGO_DATABASE_USER, and DJANGO_DATABASE_PASSWORD; defaulting to postgres
#   environment variables POSTGRES_HOST, POSTGRES_DB, etc.
#

#
# DEBUG is disabled if it is not explicitly enabled via environment
#
DEBUG = False 

import os
26
import dj_database_url
Evili del Rio's avatar
Evili del Rio committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

from django.conf import global_settings as __global_settings

#
# Dictionary to hold global and default settings
#
__settings = {}
# Django global settings dict
__settings.update(vars(__global_settings))
# default settings as a dict
__settings.update(vars(__default_settings))

for st,val in __settings.items():
    # skip private items
    if st.startswith('_'):
        continue
    else:
        # look for the corresponding 'DJANGO_..' environment
        env_setting = os.getenv('DJANGO_'+st, None)
        if env_setting is not None:
            # if the former setting was a string evaluate as such
            if isinstance(val, str):
                exec(st+" = '"+env_setting+"'")
            else:
                exec(st+" = "+env_setting)

53 54


Evili del Rio's avatar
Evili del Rio committed
55
if os.getenv('DJANGO_DATABASES', None) is None:
56 57 58 59 60 61 62 63 64 65 66 67 68
    if os.getenv('DATABASE_URL', None) is not None:
        DATABASES['default'] = dj_database_url.config(conn_max_age=600)
    else:
        __PROJECT_NAME = os.getenv('DJANGO__PROJECT_NAME',
                                   __default_settings.__package__.split('.')[0])
        DATABASES = {
            'default': {
                'ENGINE': os.getenv('DJANGO_DATABASE_ENGINE','django.db.backends.postgresql'),
                'HOST': os.getenv('DJANGO_DATABASE_HOST', os.getenv('POSTGRES_HOST', 'postgres')),
                'NAME': os.getenv('DJANGO_DATABASE_NAME', os.getenv('POSTGRES_DB', __PROJECT_NAME)),
                'USER': os.getenv('DJANGO_DATABASE_USER', os.getenv('POSTGRES_USER', __PROJECT_NAME)),
                'PASSWORD' : os.getenv('DJANGO_DATABASE_PASSWORD', os.getenv('POSTGRES_PASSWORD', __PROJECT_NAME)),
            }
Evili del Rio's avatar
Evili del Rio committed
69
        }
Evili del Rio's avatar
Evili del Rio committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85


#
# Add django-health-check to INSTALLED_APPS
#
INSTALLED_APPS.extend([
    'health_check',
    'health_check.db',
    'health_check.cache',
    'health_check.storage',
])
#
# Substitue ROOT_URLCONF to include health checks
#
PROJECT_ROOT_URLCONF = ROOT_URLCONF
ROOT_URLCONF = 'health_urls'