"""
Django settings for {{ project_name }} project.

Generated by 'django-admin startproject' using Django {{ django_version }}.

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

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

import os

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

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

# Application definition

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "django.contrib.humanize",
    "spirit.core",
    "spirit.admin",
    "spirit.search",
    "spirit.user",
    "spirit.user.admin",
    "spirit.user.auth",
    "spirit.category",
    "spirit.category.admin",
    "spirit.topic",
    "spirit.topic.admin",
    "spirit.topic.favorite",
    "spirit.topic.moderate",
    "spirit.topic.notification",
    "spirit.topic.private",
    "spirit.topic.unread",
    "spirit.comment",
    "spirit.comment.bookmark",
    "spirit.comment.flag",
    "spirit.comment.flag.admin",
    "spirit.comment.history",
    "spirit.comment.like",
    "spirit.comment.poll",
    "djconfig",
    "haystack",
]

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",
    # 'spirit.core.middleware.XForwardedForMiddleware',
    "spirit.user.middleware.TimezoneMiddleware",
    "spirit.user.middleware.LastIPMiddleware",
    "spirit.user.middleware.LastSeenMiddleware",
    "spirit.user.middleware.ActiveUserMiddleware",
    "spirit.core.middleware.PrivateForumMiddleware",
    "djconfig.middleware.DjConfigMiddleware",
]

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [os.path.join(BASE_DIR, "templates")],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.contrib.auth.context_processors.auth",
                "django.template.context_processors.debug",
                "django.template.context_processors.i18n",
                "django.template.context_processors.media",
                "django.template.context_processors.static",
                "django.template.context_processors.tz",
                "django.template.context_processors.request",
                "django.contrib.messages.context_processors.messages",
                "djconfig.context_processors.config",
            ]
        },
    }
]

CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.db.DatabaseCache",
        "LOCATION": "spirit_cache",
    },
    "st_rate_limit": {
        "BACKEND": "django.core.cache.backends.db.DatabaseCache",
        "LOCATION": "spirit_rl_cache",
        "TIMEOUT": None,
    },
}

AUTHENTICATION_BACKENDS = [
    "spirit.user.auth.backends.UsernameAuthBackend",
    "spirit.user.auth.backends.EmailAuthBackend",
]

HAYSTACK_CONNECTIONS = {
    "default": {
        "ENGINE": "haystack.backends.whoosh_backend.WhooshEngine",
        "PATH": os.path.join(BASE_DIR, "st_search"),
    }
}
HAYSTACK_SIGNAL_PROCESSOR = "spirit.search.signals.RealtimeSignalProcessor"

ROOT_URLCONF = "{{ project_name }}.urls"

WSGI_APPLICATION = "{{ project_name }}.wsgi.application"

LOGIN_URL = "spirit:user:auth:login"
LOGIN_REDIRECT_URL = "spirit:user:update"
LOGOUT_REDIRECT_URL = "spirit:index"

# Internationalization
# https://docs.djangoproject.com/en/{{ docs_version }}/topics/i18n/

LANGUAGE_CODE = "en"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True

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

STATIC_URL = "/static/"

MEDIA_URL = "/media/"

MEDIA_ROOT = os.path.join(BASE_DIR, "media")

STATIC_ROOT = os.path.join(BASE_DIR, "static")

STORAGES = {
    "default": {"BACKEND": "spirit.core.storage.OverwriteFileSystemStorage"},
    "staticfiles": {"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"},
}

# Email to the site admins
# on error when DEBUG=False,
# log to console on error always.
LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "verbose": {
            "format": "%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s"
        }
    },
    "filters": {"require_debug_false": {"()": "django.utils.log.RequireDebugFalse"}},
    "handlers": {
        "mail_admins": {
            "class": "django.utils.log.AdminEmailHandler",
            "filters": ["require_debug_false"],
            "formatter": "verbose",
            "level": "ERROR",
        },
        "file": {
            "class": "logging.FileHandler",
            "filename": os.path.join(BASE_DIR, "django.log"),
            "formatter": "verbose",
            "level": "ERROR",
        },
        "console": {"class": "logging.StreamHandler", "formatter": "verbose"},
    },
    "loggers": {
        "": {
            "handlers": ["console", "mail_admins"],
            "level": "INFO",
            "propagate": False,
        },
        "django": {
            "handlers": ["console", "mail_admins"],
            "level": "ERROR",
            "propagate": False,
        },
        "celery": {
            "handlers": ["console", "mail_admins"],
            "level": "INFO",
            "propagate": False,
        },
        "huey": {
            "handlers": ["console", "mail_admins"],
            "level": "INFO",
            "propagate": False,
        },
    },
}
