"""
Django settings for Grades project.

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

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

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

import os

# ./node_modules/.bin/webpack --config webpack.config.js

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import datetime
from django.contrib import messages

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '8ey7a5gg=zeky^p&!sz&&^$cmu02$*0-!p)#0e23=kez0lyn5v'

import sys

TESTING = len(sys.argv) > 1 and sys.argv[1] == 'test'
TEST_MANUAL = os.environ.get('TEST_MANUAL')

ALLOWED_HOSTS = [
    'localhost',
    '127.0.0.1',
    '111.222.333.444',
    'www.gradehub.io',
    'freshers-calc.herokuapp.com',
]

INTERNAL_IPS = (
    '0.0.0.0',
    '127.0.0.1',
)

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'webpack_loader',
    'rest_framework',
    'bdd',
    'mod_auth',
    'mod_choice',
)

# AUTH_USER_MODEL = 'mod_auth.GradesUser'
JWT_AUTH = {
    'JWT_EXPIRATION_DELTA': datetime.timedelta(days=365),
}

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'FreshersCalc.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['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',
                'django.template.context_processors.media',
            ],
        },
    },
]

MESSAGE_TAGS = {
    messages.INFO: 'alert-info',
    messages.WARNING: 'alert-warning',
    messages.ERROR: 'alert-danger',
    messages.SUCCESS: 'alert-success',
    50: 'critical',
}

WSGI_APPLICATION = 'FreshersCalc.wsgi.application'

# Parse database configuration from $DATABASE_URL
# example environmental variable = postgres://stuff_sam:PASSWORD@localhost/pying_django
import dj_database_url

DATABASES = {
    'default': dj_database_url.config()
}

# Enable Connection Pooling
DATABASES['default']['ENGINE'] = 'django_postgrespool'

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

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/

# STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
# STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

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

# MEDIA_URL = 'http://d2wuf2oqs5xulb.cloudfront.net/'
MEDIA_ROOT = os.path.join(BASE_DIR, "assets")
MEDIA_URL = '/'

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'assets'),
    os.path.join(BASE_DIR, 'static'),
)

WEBPACK_LOADER = {
    'DEFAULT': {
        'BUNDLE_DIR_NAME': 'bundles/',
        'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'),
    }
}

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

if TESTING or TEST_MANUAL:
    STATICFILES_STORAGE = None

    PASSWORD_HASHERS = (
        'django.contrib.auth.hashers.MD5PasswordHasher',
    )

    TEST_BROWSER = os.environ.get('TEST_BROWSER') == 'YES'
