"""
Django settings for openbook_org project.

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

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/
"""

import os
import sentry_sdk
from dotenv import load_dotenv, find_dotenv
from sentry_sdk.integrations.django import DjangoIntegration

load_dotenv(find_dotenv(), verbose=True)

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

sentry_sdk.init(
    dsn=os.environ.get('SENTRY_DSN'),
    integrations=[DjangoIntegration()],
    traces_sample_rate=1.0,

    # If you wish to associate users to errors (assuming you are using
    # django.contrib.auth) you may enable sending PII data.
    send_default_pii=True
)

# Environment flags
ENVIRONMENT = os.environ.get('ENVIRONMENT')

IS_PRODUCTION_ENVIRONMENT = True if ENVIRONMENT == 'production' else False

DEBUG = not IS_PRODUCTION_ENVIRONMENT

# Django secrets

SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')

# Hosts
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS')
if IS_PRODUCTION_ENVIRONMENT:
    if not ALLOWED_HOSTS:
        raise NameError('ALLOWED_HOSTS environment variable is required when running on a production environment')
    ALLOWED_HOSTS = [allowed_host.strip() for allowed_host in ALLOWED_HOSTS.split(',')]
else:
    ALLOWED_HOSTS = ['*']
# Application definition

INSTALLED_APPS = [
    'django.contrib.contenttypes',
    'rest_framework',
    'django_nose',
    'mailchimp3',
    'openbook_org_contact',
    'openbook_org_common',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.middleware.common.CommonMiddleware'
]

ROOT_URLCONF = 'openbook_org.urls'

TEMPLATES = []

WSGI_APPLICATION = 'openbook_org.wsgi.application'

# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
if IS_PRODUCTION_ENVIRONMENT:
    DATABASES = {
    }
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': ':memory:',
        }
    }

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

if ENVIRONMENT == 'build':
    NOSE_ARGS = [
        '--cover-erase',
        '--cover-package=.',
        '--with-spec', '--spec-color',
        '--with-coverage', '--cover-xml',
        '--verbosity=1', '--nologcapture']
else:
    NOSE_ARGS = [
        '--cover-erase',
        '--cover-package=.',
        '--with-spec', '--spec-color',
        '--with-coverage', '--cover-html',
        '--cover-html-dir=reports/cover', '--verbosity=1', '--nologcapture']

# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = []

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'simple': {
            'format': '{levelname} {message}',
            'style': '{',
        },
    },
    'handlers': {
        'file': {
            'level': 'INFO',
            'class': 'logging.FileHandler',
            'filename': os.path.join(BASE_DIR, 'debug.log'),
            'formatter': 'simple'
        }
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'propagate': True,
        }
    }
}

# Mail config

MAILGUN_API_KEY = os.environ.get('MAILGUN_API_KEY')
MAILCHIMP_API_KEY = os.environ.get('MAILCHIMP_API_KEY')
MAILCHIMP_WAITLIST_ID = os.environ.get('MAILCHIMP_WAITLIST_ID')

# Google re-captcha config

GOOGLE_RECAPTCHA_SECRET_KEY = os.environ.get('GOOGLE_RECAPTCHA_SECRET_KEY')

# Django rest framework config

REST_FRAMEWORK = {
    'DEFAULT_PARSER_CLASSES': (
        'rest_framework.parsers.JSONParser',
    ),
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': [],
    'DEFAULT_PERMISSION_CLASSES': [],
    'UNAUTHENTICATED_USER': None
}

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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

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

STATIC_URL = '/static/'

# App config

OPENBOOK_CONTACT_FORM_MAIL = os.environ.get('OPENBOOK_CONTACT_FORM_MAIL')

USE_X_FORWARDED_HOST = IS_PRODUCTION_ENVIRONMENT

if IS_PRODUCTION_ENVIRONMENT:
    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
