"""
Django settings for tectools project.

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

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
import subprocess
from pathlib import Path
from django.contrib import messages 
import django_heroku

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# 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 = '@$2y$12$sdH54eCPKFDqIdhrP4.haOXGaMm8HRTalXwNlQvFZj2ZS0s/rMs42'


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

import socket
host_name_str = socket.gethostname()
ahost_debug = ('LAPTOP-7KDAHV3M')
if host_name_str in ahost_debug:
    DEBUG = True
else:
    DEBUG = False

ALLOWED_HOSTS = ['django3-tectools.herokuapp.com','localhost','LAPTOP-7KDAHV3M.PITSolution.local']

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'bootstrap4',
    'bootstrap_datepicker_plus',
    'fontawesome_5',
    'report',
    'core',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django_session_timeout.middleware.SessionTimeoutMiddleware',    
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'tectools.urls'


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

WSGI_APPLICATION = 'tectools.wsgi.application'


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

if host_name_str in ahost_debug:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'tectoolsdb.sqlite3'),
        }
    }
else:
    pass
# Acessando uma base POSTGRESQL remota.
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'dforf24ocahi07',
        'USER': 'ijyvdmbwfesnoi',
        'PASSWORD': '6f282537d935ff3dd661edb499d5170a08e3e7952d5f895762ba935f42086bb5',
        'HOST': 'ec2-52-21-0-111.compute-1.amazonaws.com',
        'PORT': '5432',
    }
}



# 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 = 'pt-br'

TIME_ZONE = 'America/Sao_Paulo' # Default: UTC

USE_I18N = True

USE_L10N = True

USE_TZ = False # Default: True


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

STATIC_URL = '/static/'
MEDIA_URL = '/media/'


STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]


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

OS_SEPARATOR = os.sep

PDF_ROOT = os.path.join(BASE_DIR,'media'+os.sep+'pdf')

CSS_REPORT_ROOT = os.path.join(BASE_DIR,'report'+os.sep+'static'+os.sep+'css')

# python manage.py collectstatic joga todos os arquivos estaticos na STATIC_ROOT
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")

# WhiteNoise
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

"""This combines automatic compression with the caching behaviour provided by Django’s ManifestStaticFilesStorage backend. 
   If you want to apply compression but don’t want the caching behaviour then you can use:
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
"""

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'media'),
)

# Redirect to home URL after login (Default redirects to /accounts/profile/)
LOGIN_REDIRECT_URL = '/'

# Esta configurado o envio de email para o console até que se tenha um provedor real de email
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

MSG_TAGS = {
    messages.INFO:'text-info',
    messages.SUCCESS:'text-success',
    messages.ERROR:'text-danger',
    messages.DEBUG:'text-default',
    messages.WARNING:'text-warning',
}

FONTAWESOME_5_CSS = 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css'

"""
FONTAWESOME_5_CSS_ADMIN = None
    default: 'fontawesome_5/css/django-fontawesome.css'
FONTAWESOME_5_CSS_ADMIN = URL or path
    default: None
FONTAWESOME_5_ICON_CLASS = 'default' or 'semantic_ui' 
    default: 'default'
FONTAWESOME_5_PREFIX = 'custom_prefix'
    default: 'fa'
"""


if 'DYNO' in os.environ:
    
    WKHTMLTOPDF_CMD = "bin/wkhtmltopdf"

else:
    print ('loading wkhtmltopdf path on localhost')
    WKHTMLTOPDF_CMD = "C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe"
    
WKHTMLTOPDF_CMD_OPTIONS = {
    'quiet': True,
}    

# https://pypi.org/project/django-session-timeout/
# SESSION_EXPIRE_SECONDS = 900  # 15 minutos
SESSION_EXPIRE_SECONDS = 900  # 15 minutos
SESSION_EXPIRE_AFTER_LAST_ACTIVITY = True
SESSION_EXPIRE_AFTER_LAST_ACTIVITY_GRACE_PERIOD = 60 # group by minute
SESSION_TIMEOUT_REDIRECT = '/logout/'

django_heroku.settings(locals())