"""
Django settings for main project.

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

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

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

from ocyan.core.fender import config
from ocyan.core.plugins import (
    add_plugin_settings,
    get_installed_apps,
    get_middleware,
    get_sass_functions,
)

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

# SECURITY WARNING: keep the secret key used in production secret!
X_FRAME_OPTIONS = "SAMEORIGIN"

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

ALLOWED_HOSTS = ["localhost", "127.0.0.1", "[::1]"]
domain = config.get("django", "domain", "")
if domain:
    ALLOWED_HOSTS.append(domain)

AUTHENTICATION_BACKENDS = ["django.contrib.auth.backends.ModelBackend"]

COMPRESS_ENABLED = True
COMPRESS_PRECOMPILERS = (("text/x-scss", "django_libsass.SassCompiler"),)
COMPRESS_CSS_FILTERS = [
    "compressor.filters.css_default.CssAbsoluteFilter",
    "compressor.filters.cssmin.rCSSMinFilter",
]

LOGIN_REDIRECT_URL = "/"

LOGGING = {
    "version": 1,
    "disable_existing_loggers": True,
    "formatters": {
        "verbose": {
            "format": "%(name)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s"
        }
    },
    "root": {"level": "INFO", "handlers": ["console"]},
    "handlers": {
        "console": {
            "level": "DEBUG",
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        },
        "null": {"class": "logging.NullHandler"},
    },
    "loggers": {
        "ocyan": {"level": "DEBUG", "propagate": True},
        "configtype": {"level": "DEBUG"},
        "django": {"level": "INFO", "propagate": True},
        "django.request": {"level": "ERROR", "propagate": True},
        "oscar.alerts": {"level": "ERROR", "propagate": True},
        # "elasticsearch": {"level": "DEBUG", "propagate": True},
        "elastic_transport": {"level": "ERROR", "propagate": True},
        "uwsgidecorators": {"level": "ERROR", "propagate": True},
        # "django.db.backends": {"level": "DEBUG", "propagate": True},
        "django.template": {"level": "WARNING", "propagate": True},
        "sorl.thumbnail": {"level": "CRITICAL", "propagate": True},
    },
}

# Application definition

_installed_apps = get_installed_apps(
    django=[
        "django.contrib.admin",
        "django.contrib.auth",
        "django.contrib.contenttypes",
        "django.contrib.sessions",
        "django.contrib.messages",
        "django.contrib.staticfiles",
        "django.contrib.sites",
        "ocyan.main",
    ],
    apps=["compressor"],
)

INSTALLED_APPS = _installed_apps.unique_list()

LIBSASS_CUSTOM_FUNCTIONS = get_sass_functions()

MIDDLEWARE = get_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",
)

ROOT_URLCONF = "ocyan.main.urls"

TEMPLATES = [
    {
        "BACKEND": "ocyan.main.template.OcyanDjangoTemplates",
        "DIRS": [],
        "NAME": "django",
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.template.context_processors.static",
                "django.template.context_processors.media",
                "django.template.context_processors.i18n",
                "django.contrib.messages.context_processors.messages",
            ],
            "loaders": [
                "django.template.loaders.filesystem.Loader",
                # use instead of django.template.loaders.app_directories.Loader
                # to enforce searching for templates in replaced apps and extended apps
                "ocyan.main.templateloaders.OcyanExtendedAppTemplateLoader",
            ],
        },
    }
]

FORM_RENDERER = "ocyan.main.renderers.OcyanFormRenderer"

WSGI_APPLICATION = "ocyan.main.wsgi.application"

# Password validation
# https://docs.djangoproject.com/en/2.0/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/2.0/topics/i18n/

LANGUAGE_CODE = config.get("django", "language_code", "") or "nl"

# make the 404 handler changeable in settings
OCYAN_404_HANDLER = "django.views.defaults.page_not_found"

TIME_ZONE = "UTC"
USE_I18N = True
USE_L10N = True
USE_TZ = True

# The default email backend is one that doesn't break.
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"

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

MEDIA_URL = "/media/"
STATIC_URL = "/static/"

STATICFILES_FINDERS = [
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
    "compressor.finders.CompressorFinder",
]

# https://docs.djangoproject.com/en/5.1/ref/settings/#storages
STORAGES = {
    "default": {
        "BACKEND": "django.core.files.storage.FileSystemStorage",
    },
    "staticfiles": {
        "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
    },
}

SITE_ID = 1

FILE_UPLOAD_PERMISSIONS = 0o644

COMPRESS_CSS_HASHING_METHOD = None

WAGTAILIMAGES_WEBP_QUALITY = WAGTAILIMAGES_JPEG_QUALITY = THUMBNAIL_QUALITY = 60
THUMBNAIL_PROGRESSIVE = False

# 14days * seconds per day (86400)
OCYAN_RANDOM_TOKEN_TIMEOUT = 14 * 86400

# Import settings from Ocyan plugins
add_plugin_settings(locals())

# make other changes here
