# -*- coding: utf-8 -*-
"""
Django settings for SMS project.

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

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 sys
import ConfigParser

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# set directory with outlying configuration files
CONF_ROOT = os.path.join(BASE_DIR, '..', 'configurations')
sys.path.append(CONF_ROOT)

config = ConfigParser.ConfigParser()

CONF_FILE = (os.path.join(CONF_ROOT, 'CONF.ini'))
config.read(CONF_FILE)

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '_a00_q)39+w=&(t-wc7jxmp3s%69yunzpcd2h&7yh2#h#2e-l4'

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

ALLOWED_HOSTS = ['127.0.0.1', 'localhost']


# Application definition
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'SMS.apps.mainteacher',
    'SMS.apps.teacher',
    'SMS.apps.director',
    'SMS.apps.core',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    # user classes
    'SMS.apps.core.utils.middleware.AutoLogout',
    'SMS.apps.core.utils.middleware.TrackUsersMiddleware'
)

ROOT_URLCONF = 'SMS.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'SMS/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',
                'django.core.context_processors.request',
                'SMS.context_processors.get_user_info',
            ],
        },
    },
]

WSGI_APPLICATION = 'SMS.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DB_ENGINE = config.get('DataBase', 'ENGINE')
DB_HOST = config.get('DataBase', 'HOST')
DB_USER = config.get('DataBase', 'USER')
DB_PASSWORD = config.get('DataBase', 'PASSWORD')
DB_NAME = config.get('DataBase', 'NAME')

DATABASES = {
    'default': {
        'ENGINE': DB_ENGINE,
        'HOST': DB_HOST,
        'USER': DB_USER,
        'PASSWORD': DB_PASSWORD,
        'NAME': DB_NAME,
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'uk'

TIME_ZONE = 'Europe/Kiev'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(
        BASE_DIR,
        'static',
    ),
)

STATIC_URL = '/static/'

# Media files (Images)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

MEDIA_URL = '/media/'

# Logging section
USE_LOGS = config.getboolean('Logs', 'USE_LOGS')

if USE_LOGS:
    USE_CUSTOM_LOG_DIR = config.getboolean('Logs', 'USE_CUSTOM_LOG_DIR')
    if USE_CUSTOM_LOG_DIR:
        LOG_DIR = config.get('Logs', 'CUSTOM_LOG_DIR')
    else:
        # default log directory on one level as the project
        LOG_DIR = os.path.dirname(BASE_DIR) + '/logs'

    TRACK_USERS = config.getboolean('Logs', 'TRACK_USERS')

    from utils.logging import LOGGING


# SMTP settings
ADMIN_EMAIL = config.get('SMTP', 'ADMIN_EMAIL')
EMAIL_HOST = config.get('SMTP', 'EMAIL_HOST')
EMAIL_PORT = config.getint('SMTP', 'EMAIL_PORT')
EMAIL_HOST_USER = config.get('SMTP', 'EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = config.get('SMTP', 'EMAIL_HOST_PASSWORD')
EMAIL_USE_TLS = config.getboolean('SMTP', 'EMAIL_USE_TLS')
EMAIL_USE_SSL = config.getboolean('SMTP', 'EMAIL_USE_SSL')


# Sessions settings
# handle session is not json serializable
SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'
# auto logout delay in minute
AUTO_LOGOUT_DELAY = 4
