"""
Django settings for nep project.

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

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

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

import os

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

DEBUG = False

TIME_ZONE = 'America/Sao_Paulo'

# Application definition

INSTALLED_APPS = [
    # Experiments app need to be before django.contrib.admin because this
    # way the templates that have the same name as from admin templates are
    # read first. We do this way because the authentication system is reused
    # from Django.
    'experiments.apps.ExperimentsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'haystack',
    'downloads',
    'bootstrapform',
    'django_celery_results',
    'modeltranslation',
]

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',
]

ROOT_URLCONF = 'nep.urls'

LOGIN_URL = 'login'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'

##
# Haystack
# --------
# change 'URL' to reflect the elasticsearch server location/port
HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE':
            'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
        'URL': 'http://127.0.0.1:9200/',
        'INDEX_NAME': 'haystack',
    }
}
HAYSTACK_DEFAULT_OPERATOR = 'OR'
HAYSTACK_CUSTOM_HIGHLIGHTER = 'experiments.appclasses.NepHighlighter'
# TODO: this is for doesn't paginate the SearchQueryset from NepSearchForm
# TODO: this has to be solved after first Portal release. It's just a jerry-rig
HAYSTACK_SEARCH_RESULTS_PER_PAGE = 500

# <host>:<port> for testing tha uses haystack/elasticsearch
# This is used when substituting HAYSTACK_CONNECTIONS for another, separated
# instance of the engine backend.
# IMPORTANT: overwrite it in local settings not here as settings.py is
# updated by new releases
HAYSTACK_TEST_URL = 'http://127.0.0.1:9200/'

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.template.context_processors.media',
                'django.template.context_processors.static',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.i18n',
            ],
        },
    },
]

WSGI_APPLICATION = 'nep.wsgi.application'


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

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

LANGUAGE_CODE = 'en'

LANGUAGES = (
    ('en', 'English'),
    ('pt-br', 'Português'),
)

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'experiments/locale'),
    os.path.join(BASE_DIR, 'downloads/locale')
)

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'

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

# Celery
CELERY_RESULT_BACKEND = 'django-db'

# Import local settings
try:
    from .local_settings import *
    if 'DEV_APPS' in locals():
        INSTALLED_APPS += DEV_APPS
except ImportError:
    pass
