# Do not edit this file.
# Customizations should be made in one of the three modules this one will call:
#  production.py   -- used if a URL is defined and it says ?production=true
#  staging.py      -- used if a URL is defined (but no production=true switch is present)
#  local_preset.py  -- used if no URL is defined
#   (but, it's your computer, so you can mess up whatever you want.)
"""
this preset uses a url-type string to implement 12-factor configuration with fewer environment variables.

DATABASE_URL = '<engine>://<user>:<password>@<host>:<port>/<database>'? <switches>
       <engine> can be one of: 'postgresql', 'postgis', 'mysql', 'sqlite'
       (default: sqlite3 database file)
   (the appropriate database modules must be loaded, of course.)

if <switches> are defined, keywerds "production" and "secrect_key" have effect as follows

SECRET_KEY  will be set from ?secret_key=<key of your invention>

if ?production=true
  then this module will import production.py
  otherwise, it will import staging.py
"""
import os
import sys
##  may also do "lazy" imports of:
# import django.utitls.six
# import _database_url # local module
# import local_preset
#
# and will try:
#   import ..local_settings  # failure is silent (and expected)

# This module is compatible with ideas from:
#   dj_database_url by Kenneth Reitz
#

DATABASES = NotImplemented   # before leaving this module, we will test to ensure that this has been defined

_url = os.getenv('DATABASE_URL', '')

if _url:
    import _database_url
    _dbd, DATABASE_URL_QUERIES = _database_url.config(_url)
    _production = DATABASE_URL_QUERIES.get('production', ['No'])[0].lower() == 'true'
    DATABASES = {'default': {}}

try:  # we must trap ImportError to get a quality traceback in case of errors in our parent settings files
    if _url:
        if _production:
            from production import *
        else:
            from staging import *

    # database definition from URL overrides others
    DATABASES['default'].update(_dbd)
    try:
        SECRET_KEY = DATABASE_URL_QUERIES['secret_key'][0]
    except KeyError:
        pass

#   if _url was not defined, so we will use the local preset as a default
    else:
        from local_preset import *   # is expected to import either staging or production itself
except ImportError:
    import django.utils.six
    django.utils.six.reraise(RuntimeError, *sys.exc_info()[1:])  # use RuntimeError to extend the traceback
except:
    raise

# almost there..  someone should have imported ..settings.py by now
#   in order to emulate the legacy action of settings.py importing local_settings.py
#   we will perform that import here, after settings.py and everyone else....
if 'local_settings' in sys.modules:
    print('Initialization Note: preset/default_settings.py is skipping import of ..local_settings.py')
    print('   it has already been imported (by settings.py?) so some of its settings may have been overridden.')
else:
    try:
        from ..local_settings import *
        # import warnings
        # warnings.warn('Legacy local_settings.py was imported.', PendingDeprecationWarning)
    except ImportError:
        pass

# last of all, if nothing has defined a database, use sqlite
if DATABASES is NotImplemented:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': 'db.sqlite',
        }
    }
    print('Using default database {}'.format(os.path.abspath(DATABASES['default']['NAME'])))
