"""
Django settings for this project.

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

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

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

try:
    from config import config
    # required keys: SECRET_KEY
    # optional keys: ALLOWED_HOSTS, DEFAULT_THEME, ITEM_ROOT, THUMBNAIL_SIZES, WEBNAIL_SIZES
except ImportError:
    config = {}
import os
import random


# 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/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
#
try:
    SECRET_KEY = config['SECRET_KEY']
except KeyError:
    chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
    s_key = ''.join([random.choice(chars) for n in range(50)])
    secret_key_warning = "You need to create a config.py file including a variable config which is a dict  with at least a SECRET_KEY definition (e.g.: %s)." % repr(s_key)
    raise KeyError(secret_key_warning)
# SECURITY WARNING: don't run with debug turned on in production!
#
DEBUG = False

ALLOWED_HOSTS = config.get('ALLOWED_HOSTS', [])


# Application definition
#
INSTALLED_APPS = [
    'pygal.apps.PygalConfig',
    'themes.apps.ThemesConfig',
    'users.apps.UsersConfig',
    #
    '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.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'users.middleware.SettingsMiddleware',
]

ROOT_URLCONF = 'main.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 = 'main.wsgi.application'


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


# Password validation
# https://docs.djangoproject.com/en/2.2/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',
    },
]


# Search Engine
#
WHOOSH_PATH = os.path.join(BASE_DIR, 'data', 'whoosh_index')


# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
#
LANGUAGE_CODE = 'en-us'
LANGUAGES = [
    ('en', 'English'),
    ('de', 'Deutsch'),
]

TIME_ZONE = 'UTC'

USE_I18N = True
LOCALE_PATHS = [
    os.path.join(BASE_DIR, 'themes', 'locale'),
    os.path.join(BASE_DIR, 'users', 'locale'),
    os.path.join(BASE_DIR, 'pygal', 'locale'),
]

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
#
STATIC_ROOT = os.path.join(BASE_DIR, 'data', 'static')
STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'data', 'media')
MEDIA_URL = '/media/'


# Session parameters
#

SESSION_KEY_THUMBNAIL_SIZE = 'thumbnail_size'
SESSION_KEY_WEBNAIL_SIZE = 'webnail_size'
PERSISTENT_SESSION_VARIABLES = [SESSION_KEY_THUMBNAIL_SIZE, SESSION_KEY_WEBNAIL_SIZE]


# Logging Configuration
#
default_handler = ['console_long'] if DEBUG else ['console']
#
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'short': {
            'format': "%(asctime)s \"%(name)s - %(levelname)s - %(message)s\"",
            'datefmt': '[%d/%b/%Y %H:%M:%S]',
        },
        'long': {
            'format': """~~~~(%(levelname)-10s)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
File "%(pathname)s", line %(lineno)d, in %(funcName)s
%(asctime)s: %(name)s - %(message)s
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~""",
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'short',
        },
        'console_long': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'long',
        },
    },
    'loggers': {
        'AUTH': {
            'handlers': default_handler,
            'level': 'INFO',
            'propagate': False,
        },
        'APP': {
            'handlers': default_handler,
            'level': 'INFO',
            'propagate': False,
        },
        'CACHING': {
            'handlers': default_handler,
            'level': 'INFO',
            'propagate': False,
        },
        'WHOOSH': {
            'handlers': default_handler,
            'level': 'INFO',
            'propagate': False,
        },
        'FSTOOLS': {
            'handlers': default_handler,
            'level': 'INFO',
            'propagate': False,
        },
        'GEO': {
            'handlers': default_handler,
            'level': 'INFO',
            'propagate': False,
        },
    },
}


# Other Configuration issues
#

DATA_UPLOAD_MAX_NUMBER_FIELDS = 30000

LOGIN_URL = 'users-login'

XNAIL_ROOT = os.path.join(BASE_DIR, 'data', 'xnails')
TEMP_ROOT = os.path.join(BASE_DIR, 'data', 'temp')


# App Configuration
#
DEFAULT_THEME = config.get('DEFAULT_THEME', 'clear-red')
ITEM_ROOT = config.get('ITEM_ROOT', os.path.join(BASE_DIR, 'data', 'example_data'))
THUMBNAIL_SIZES = config.get('THUMBNAIL_SIZES', [137, 175, 250])
WEBNAIL_SIZES = config.get('WEBNAIL_SIZES', [450, 1100, 1750])
