# Django settings for qc project.

import os
import dj_database_url
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse_lazy


def get_env_variable(var_name):
    """ Get the environment variable or return exception """
    try:
        return os.environ[var_name]
    except KeyError:
        error_msg = "Set the %s environment variable" % var_name
        raise ImproperlyConfigured(error_msg)


# Get ENV VARIABLES key
ENV_ROLE = get_env_variable('ENV_ROLE')

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

SECRET_KEY = get_env_variable('QC_SECRET_KEY')

if ENV_ROLE == 'dev':
    DEBUG = True
    ALLOWED_HOSTS = []
    EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

if ENV_ROLE == 'prod':
    DEBUG = False
    ALLOWED_HOSTS = [
        '.quect.ru',
    ]

ADMINS = (
    ('Phobos', 'dev@quect.ru'),
)

MANAGERS = ADMINS

EMAIL_HOST = 'smtp.yandex.ru'
EMAIL_HOST_USER = 'dev@quect.ru'
EMAIL_HOST_PASSWORD = get_env_variable('EMAIL_HOST_PASSWORD')
EMAIL_PORT = 587
EMAIL_USE_TLS = True

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'crispy_forms',
    'main',
    'coordination',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'qc.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates'), ],
        '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',
                'main.context_processors.global_settings',
            ],
        },
    },
]

WSGI_APPLICATION = 'qc.wsgi.application'

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

LANGUAGE_CODE = 'ru-RU'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

# Parse database configuration from $DATABASE_URL
DATABASES = {'default': dj_database_url.config()}

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
MEDIA_ROOT = 'mediafiles'
MEDIA_URL = '/media/'

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

STATICFILES_DIRS = (
    ('assets', os.path.join(BASE_DIR, 'templates/assets')),
)

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

LOGOUT_URL = reverse_lazy('auth_logout')
LOGIN_URL = reverse_lazy('auth_login')
LOGIN_REDIRECT_URL = reverse_lazy('home')

PROJECT_NAME = 'QC'
FULL_PROJECT_NAME = 'QuestCoordination'
PROJECT_VERSION = 'v3.0'

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

CRISPY_TEMPLATE_PACK = 'bootstrap3'

ADMIN_URL_PATH = get_env_variable('ADMIN_URL_PATH')

@@ -3629,8 +3629,1052 @@
 _PATH')%0A
+%0ALOGGING = %7B%0A    'version': 1,%0A    'disable_existing_loggers': False,%0A    'filters': %7B%0A        'require_debug_false': %7B%0A            '()': 'django.utils.log.RequireDebugFalse'%0A        %7D%0A    %7D,%0A    'formatters': %7B%0A        'verbose': %7B%0A            'format': '%25(levelname)s %25(asctime)s %25(module)s '%0A                      '%25(process)d %25(thread)d %25(message)s'%0A        %7D,%0A    %7D,%0A    'handlers': %7B%0A        'mail_admins': %7B%0A            'level': 'ERROR',%0A            'filters': %5B'require_debug_false'%5D,%0A            'class': 'django.utils.log.AdminEmailHandler'%0A        %7D,%0A        'console': %7B%0A            'level': 'DEBUG',%0A            'class': 'logging.StreamHandler',%0A            'formatter': 'verbose',%0A        %7D,%0A    %7D,%0A    'loggers': %7B%0A        'django.request': %7B%0A            'handlers': %5B'mail_admins'%5D,%0A            'level': 'ERROR',%0A            'propagate': True%0A        %7D,%0A        'django.security.DisallowedHost': %7B%0A            'level': 'ERROR',%0A            'handlers': %5B'console', 'mail_admins'%5D,%0A            'propagate': True%0A        %7D%0A    %7D%0A%7D%0A

Add logging settings