"""
Django settings for popojutrze project.

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

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
from django.urls.base import reverse_lazy

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT_INDIRECT = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../')
PROJECT_ROOT = os.path.realpath(PROJECT_ROOT_INDIRECT)
FILE_SERVING_ROOT = os.path.join(PROJECT_ROOT, 'public')

SECRET_KEY = 'w*!4bzk@z@9mej9$y@4y)j@b9b-^7bu47$#f0jq29vpc)8pb7b'

database = os.environ.get('DJANGO_DATABASE', 'dep')
mode = os.environ.get('DJANGO_MODE', 'dep')
server = os.environ.get('DJANGO_SERVER', 'dep')

DEBUG = True if mode == 'dev' else False
template_debug = DEBUG

ALLOWED_HOSTS = ['*']

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'django_inlinecss',
    'django_extensions',
    'embed_video',
    'adminsortable2',
    'dajaxice',
    'django_summernote',

    'popojutrze',
    'articles',
    'exercises',
    'marks',
    'tasks',
    'users',
    'mailing'
]

AUTHENTICATION_BACKENDS = (
    'users.auth_backends.ApprenticeModelBackend',
    'django.contrib.auth.backends.ModelBackend'
)

LOGIN_REDIRECT_URL = reverse_lazy('path-list')
LOGOUT_REDIRECT_URL = reverse_lazy('users:login')

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',
    'og_django_utils.utils.middleware.CriticalLogMiddleware',
]

ROOT_URLCONF = 'popojutrze.urls'

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

WSGI_APPLICATION = 'popojutrze.wsgi_dep.application'

DATABASES = {
    'dev': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'popojutrze',
        'USER': 'popojutrze',
        'PASSWORD': 'Pu8mLB',
        'HOST': 'dev.ordergroup.pl',
    },
    'dep': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'popojutrze',
        'USER': 'popojutrze',
        'PASSWORD': 'nNqrb48u',
        'HOST': 'localhost',
    }
}
DATABASES['default'] = DATABASES[database]
print('Using database: {}'.format(database))

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
LANGUAGE_CODE = 'pl-pl'

TIME_ZONE = 'Europe/Warsaw'
USE_I18N = True
USE_L10N = True
USE_TZ = True

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(FILE_SERVING_ROOT, 'auto_collected/')

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'dajaxice.finders.DajaxiceFinder',
)
STATICFILES_DIRS = (
    os.path.join(FILE_SERVING_ROOT, 'static/'),
)

# Media
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(FILE_SERVING_ROOT, 'media/')

# Loggers
LOG_DIRECTORY = os.path.join(PROJECT_ROOT, 'logs')
LOGFILE_SIZE = 5 * 1024 * 1024
LOGFILE_COUNT = 5
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '[%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d] %(message)s'
        },
    },
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class': 'logging.NullHandler',
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
        'general': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(LOG_DIRECTORY, 'general.log'),
            'formatter': 'verbose',
            'maxBytes': LOGFILE_SIZE,
            'backupCount': LOGFILE_COUNT
        },
        'popojutrze': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(LOG_DIRECTORY, 'popojutrze.log'),
            'formatter': 'verbose',
            'maxBytes': LOGFILE_SIZE,
            'backupCount': LOGFILE_COUNT
        },
        'critical': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(LOG_DIRECTORY, 'critical.log'),
            'formatter': 'verbose',
            'maxBytes': LOGFILE_SIZE,
            'backupCount': LOGFILE_COUNT
        },
        'mailing': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(LOG_DIRECTORY, 'mails.log'),
            'maxBytes': LOGFILE_SIZE,
            'backupCount': LOGFILE_COUNT,
            'formatter': 'verbose'
        },
    },
    'loggers': {
        # handlery dla projektu
        'general': {
            'handlers': ['general', 'console'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'popojutrze': {
            'handlers': ['popojutrze', 'console'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'critical': {
            'handlers': ['critical', 'console'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'mailing': {
            'handlers': ['mailing', 'console'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

# Mailgun
EMAIL_BACKEND = 'django_mailgun.MailgunBackend'
MAILGUN_ACCESS_KEY = 'key-b04332a50ec9bf05b24b34a502be107d'
MAILGUN_SERVER_NAME = 'mg.praesenti.pl'
EMAIL_FROM = 'noreply@praesenti.pl'

BASE_SITE_URL = 'http://praesenti.pl/'

SUMMERNOTE_CONFIG = {
    'toolbar': [
        ['para', ['ul']],
        ['insert', ['link']],
    ]
}
