"""
Django settings for okthess project.

Generated by 'django-admin startproject' using Django 1.11.3.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'yh(su1dt&mt=vqcwonwa*zq(r$e$up*4(cuqy-br@6=85#4@mu'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['
    'localhost',
    '127.0.0.1',
    'okthess.absk3drrdz.eu-central-1.elasticbeanstalk.com',
']


# Application definition

INSTALLED_APPS = [
    'main.apps.MainConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'okthess.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'okthess.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'okthess',
        'USER': 'okthess',
        'PASSWORD': 'okthess',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

# AWS production
if 'RDS_HOSTNAME' in os.environ:
    # Shame on AWS, ding ding
    hostname = os.environ['RDS_HOSTNAME'].split(':')[0]
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': os.environ['RDS_DB_NAME'],
            'USER': os.environ['RDS_USERNAME'],
            'PASSWORD': os.environ['RDS_PASSWORD'],
            'HOST': hostname,
            'PORT': os.environ['RDS_PORT'],
        }
    }

# TravisCI testing
if 'TRAVIS' in os.environ:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'okthess',
            'USER': 'postgres',
            'PASSWORD': '',
            'HOST': '127.0.0.1',
            'PORT': '5432',
        }
    }


# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = 'static'

@@ -928,16 +928,1159 @@
 m',%0A'%5D%0A%0A
+def is_ec2_linux():%0A    %22%22%22%0A    Detect if we are running on an EC2 Linux Instance%0A    See http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html%0A    %22%22%22%0A    if os.path.isfile(%22/sys/hypervisor/uuid%22):%0A        with open(%22/sys/hypervisor/uuid%22) as f:%0A            uuid = f.read()%0A            return uuid.startswith(%22ec2%22)%0A    return False%0A%0Adef get_linux_ec2_private_ip():%0A    %22%22%22%0A    Get the private IP Address of the machine if running on an EC2 linux server%0A    See http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html#instancedata-data-retrieval%0A    %22%22%22%0A    import urllib2%0A    if not is_ec2_linux():%0A        return None%0A    try:%0A        response = urllib2.urlopen('http://169.254.169.254/latest/meta-data/local-ipv4')%0A        return response.read()%0A    except:%0A        return None%0A    finally:%0A        if response:%0A    response.close()%0A%0A# ElasticBeanstalk healthcheck sends requests with host header = internal ip%0A# So we detect if we are in elastic beanstalk, and add the instances private ip address%0Aprivate_ip = get_linux_ec2_private_ip()%0Aif private_ip:%0A    ALLOWED_HOSTS.append(private_ip)%0A%0A
 %0A# Appli

add dynamic private ip loading on allowed hosts