"""
Django settings for burnsomninet project.

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

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

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

import os
from pathlib import Path
from django.conf import settings
from django import http

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
SITECODE = f"{BASE_DIR}/sitecode"
JS_PATH = f"{SITECODE}/javascript"
SCSS_PATH = f"{SITECODE}/scss"
STATIC_PATH = f"{Path(__file__).resolve().parent.parent.parent}/content"
GIT_PATH = "/srv/git"
TMP_PATH = "/tmp"
VENV_PATH = "/srv/bon_venv"
LOG_PATH = "/var/log/httpd/burnsomninet/log"


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '4_m6_(zq#$!u#i=b0o=911e3)_xfq!_3mh9%)nhxqw5r@24fv&'

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

ALLOWED_HOSTS = [
    "localhost",
    "ang.localhost",
    "django.localhost",
    "127.0.0.1",
    "burnsomni.stage",
    "burnsomni.net",
    "www.burnsomni.net",
    "ang.burnsomni.net",
    "django.burnsomni.net"
]

# Application definition

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

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

    'burnsomninet.middleware.BlockedIpMiddleware'
]

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


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


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

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/Vancouver'
USE_I18N = True
USE_L10N = True
USE_TZ = True


LOGGING = {
    "version": 1,  # the dictConfig format version
    "disable_existing_loggers": False,  # retain the default loggers
    #"handlers": {
    #    "file": {
    #        "class": "logging.FileHandler",
    #        "filename": "general.log",
    #    },
    #},
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'filters': ['require_debug_false'],
            'class': 'logging.FileHandler',
            'filename': "/var/log/httpd/burnsomninet/log",
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
    'applogfile': {
        'level':'DEBUG',
        'class':'logging.handlers.RotatingFileHandler',
        'filename': "/var/log/httpd/burnsomninet/log",
        'maxBytes': 1024*1024*15, # 15MB
        'backupCount': 10,
    },

}



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

STATIC_URL = '/static/'
STATICFILES_DIRS = [
#    str(BASE_DIR.joinpath('static')),
#    str(BASE_DIR.joinpath('ntest/www'))
]
SITE_PATH = str(BASE_DIR.joinpath('burnsomninet'))


COMMIT_ID = None
with open(f"{BASE_DIR}/.git/HEAD", "r") as fp:
    branch_path = fp.read()

    branch_path_part = branch_path[5:].strip()
    full_branch_path = f"{BASE_DIR}/.git/{branch_path_part}"
    if os.path.isfile(full_branch_path):
        with open(full_branch_path, "r") as fp:
            COMMIT_ID = fp.read().strip()
    else:
        COMMIT_ID = branch_path.strip()
