"""
Django settings for openstack_lease_it project.

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

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

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

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
import ast
import logging

from openstack_lease_it.config import GLOBAL_CONFIG, load_config

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Load configuration
load_config()

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = GLOBAL_CONFIG['DJANGO_SECRET_KEY']

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = ast.literal_eval(GLOBAL_CONFIG['DJANGO_DEBUG'])

# ALLOWED_HOSTS secure django app access
ALLOWED_HOSTS = []

# A email as format must match this regular expression
# If you not understand, please
EMAIL_REGEXP = r"^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\.-]+\.[A-Za-z]*$"

# Application definition
INSTALLED_APPS = (
    'openstack_auth',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'openstack_lease_it',
    'lease_it',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    '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 = 'openstack_lease_it.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 = 'openstack_lease_it.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en'
TIME_ZONE = 'Europe/Paris'
USE_I18N = True
USE_L10N = True
USE_TZ = True
DEFAULT_CHARSET = 'utf-8'

# We use memcached as cache backend
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '{MEMCACHED_HOST}:{MEMCACHED_PORT}'.format(**GLOBAL_CONFIG),
    }
}
SESSION_COOKIE_SECURE = False
SESSION_TIMEOUT = 1800
# A token can be near the end of validity when a page starts loading, and
# invalid during the rendering which can cause errors when a page load.
# TOKEN_TIMEOUT_MARGIN defines a time in seconds we retrieve from token
# validity to avoid this issue. You can adjust this time depending on the
# performance of the infrastructure.
TOKEN_TIMEOUT_MARGIN = 100

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

LOGIN_URL = 'login'
LOGOUT_URL = 'logout'
LOGIN_REDIRECT_URL = '/'

SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'

if GLOBAL_CONFIG['BACKEND_PLUGIN'] == 'Openstack':
    # UserId on django-openstack_auth need specific User model
    AUTH_USER_MODEL = 'openstack_auth.User'

    # Define keystone URL for authentification
    OPENSTACK_KEYSTONE_URL = GLOBAL_CONFIG['OS_AUTH_URL']

    # We use keystone v3 API
    OPENSTACK_API_VERSIONS = {
        "identity": GLOBAL_CONFIG['OS_IDENTITY_API_VERSION'],
    }
    # We use multidomain
    OPENSTACK_KEYSTONE_MULTIDOMAIN_SUPPORT = True

    # We load Openstack_auth backend
    AUTHENTICATION_BACKENDS = (
        'openstack_auth.backend.KeystoneBackend',
        'django.contrib.auth.backends.ModelBackend',
    )
else:
    AUTHENTICATION_BACKENDS = (
        'django.contrib.auth.backends.ModelBackend',
    )

# Configure logging
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'simple': {
            'format': '%(levelname)s %(asctime)s: %(message)s'
        },
    },
    'handlers': {
        'django': {
            'level': GLOBAL_CONFIG['DJANGO_LOGLEVEL'],
            'class': 'logging.FileHandler',
            'filename': os.path.join(GLOBAL_CONFIG['DJANGO_LOGDIR'], 'django.log'),
            'formatter': 'simple'
        },
        'main': {
            'level': GLOBAL_CONFIG['DJANGO_LOGLEVEL'],
            'class': 'logging.FileHandler',
            'filename': os.path.join(GLOBAL_CONFIG['DJANGO_LOGDIR'], 'main.log'),
            'formatter': 'simple'
        },
        'notification': {
            'level': GLOBAL_CONFIG['DJANGO_LOGLEVEL'],
            'class': 'logging.FileHandler',
            'filename': os.path.join(GLOBAL_CONFIG['DJANGO_LOGDIR'], 'notification.log'),
            'formatter': 'simple'
        },
        'instances': {
            'level': GLOBAL_CONFIG['DJANGO_LOGLEVEL'],
            'class': 'logging.FileHandler',
            'filename': os.path.join(GLOBAL_CONFIG['DJANGO_LOGDIR'], 'instances.log'),
            'formatter': 'simple'
        },
    },
    'loggers': {
        'django': {
            'handlers': ['django'],
            'level': GLOBAL_CONFIG['DJANGO_LOGLEVEL'],
            'propagate': True,
        },
        'main': {
            'handlers': ['main'],
            'level': GLOBAL_CONFIG['DJANGO_LOGLEVEL'],
            'propagate': True,
        },
        'notification': {
            'handlers': ['notification'],
            'level': GLOBAL_CONFIG['DJANGO_LOGLEVEL'],
            'propagate': True,
        },
        'instances': {
            'handlers': ['instances'],
            'level': GLOBAL_CONFIG['DJANGO_LOGLEVEL'],
            'propagate': True,
        },
    },
}

LOGGER = logging.getLogger('main')
LOGGER_NOTIFICATION = logging.getLogger('notification')
LOGGER_INSTANCES = logging.getLogger('instances')

[((23, 0, 23, 13), 'openstack_lease_it.config.load_config', 'load_config', ({}, {}), '()', False, 'from openstack_lease_it.config import GLOBAL_CONFIG, load_config\n'), ((29, 8, 29, 55), 'ast.literal_eval', 'ast.literal_eval', ({(29, 25, 29, 54): "GLOBAL_CONFIG['DJANGO_DEBUG']"}, {}), "(GLOBAL_CONFIG['DJANGO_DEBUG'])", False, 'import ast\n'), ((211, 9, 211, 34), 'logging.getLogger', 'logging.getLogger', ({(211, 27, 211, 33): '"""main"""'}, {}), "('main')", False, 'import logging\n'), ((212, 22, 212, 55), 'logging.getLogger', 'logging.getLogger', ({(212, 40, 212, 54): '"""notification"""'}, {}), "('notification')", False, 'import logging\n'), ((213, 19, 213, 49), 'logging.getLogger', 'logging.getLogger', ({(213, 37, 213, 48): '"""instances"""'}, {}), "('instances')", False, 'import logging\n'), ((20, 43, 20, 68), 'os.path.abspath', 'os.path.abspath', ({(20, 59, 20, 67): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((88, 16, 88, 52), 'os.path.join', 'os.path.join', ({(88, 29, 88, 37): 'BASE_DIR', (88, 39, 88, 51): '"""db.sqlite3"""'}, {}), "(BASE_DIR, 'db.sqlite3')", False, 'import os\n'), ((165, 24, 165, 82), 'os.path.join', 'os.path.join', ({(165, 37, 165, 67): "GLOBAL_CONFIG['DJANGO_LOGDIR']", (165, 69, 165, 81): '"""django.log"""'}, {}), "(GLOBAL_CONFIG['DJANGO_LOGDIR'], 'django.log')", False, 'import os\n'), ((171, 24, 171, 80), 'os.path.join', 'os.path.join', ({(171, 37, 171, 67): "GLOBAL_CONFIG['DJANGO_LOGDIR']", (171, 69, 171, 79): '"""main.log"""'}, {}), "(GLOBAL_CONFIG['DJANGO_LOGDIR'], 'main.log')", False, 'import os\n'), ((177, 24, 177, 88), 'os.path.join', 'os.path.join', ({(177, 37, 177, 67): "GLOBAL_CONFIG['DJANGO_LOGDIR']", (177, 69, 177, 87): '"""notification.log"""'}, {}), "(GLOBAL_CONFIG['DJANGO_LOGDIR'], 'notification.log')", False, 'import os\n'), ((183, 24, 183, 85), 'os.path.join', 'os.path.join', ({(183, 37, 183, 67): "GLOBAL_CONFIG['DJANGO_LOGDIR']", (183, 69, 183, 84): '"""instances.log"""'}, {}), "(GLOBAL_CONFIG['DJANGO_LOGDIR'], 'instances.log')", False, 'import os\n')]