"""
Django settings for the admin project.
"""

import os
from urlparse import urlparse
from website import settings as osf_settings

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = osf_settings.SECRET_KEY

# TODO: generalize this authentication
AUTHENTICATION_BACKENDS = (
    'api.base.authentication.backends.ODMBackend',
)

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

ALLOWED_HOSTS = [
    '.osf.io'
]

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.staticfiles',

    # 3rd party
    'raven.contrib.django.raven_compat',
)

# TODO: Are there more granular ways to configure reporting specifically related to the API?
RAVEN_CONFIG = {
    'dsn': osf_settings.SENTRY_DSN
}

# Settings related to CORS Headers addon: allow API to receive authenticated requests from OSF
# CORS plugin only matches based on "netloc" part of URL, so as workaround we add that to the list
CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = (urlparse(osf_settings.DOMAIN).netloc,
                         osf_settings.DOMAIN,
                         )
CORS_ALLOW_CREDENTIALS = True

MIDDLEWARE_CLASSES = (
    # TokuMX transaction support
    # Needs to go before CommonMiddleware, so that transactions are always started,
    # even in the event of a redirect. CommonMiddleware may cause other middlewares'
    # process_request to be skipped, e.g. when a trailing slash is omitted
    'api.base.middleware.DjangoGlobalMiddleware',
    'api.base.middleware.TokuTransactionsMiddleware',

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

)

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True
    }]


ROOT_URLCONF = 'admin.base.urls'
WSGI_APPLICATION = 'admin.base.wsgi.application'
ADMIN_BASE = 'admin/'
STATIC_URL = '{}static/'.format(ADMIN_BASE)

LANGUAGE_CODE = 'en-us'

@@ -938,16 +938,38 @@
 ompat',%0A
+    'webpack_loader',%0A
 )%0A%0A# TOD
@@ -2558,187 +2558,808 @@
 True
-%0A    %7D%5D%0A%0A%0AROOT_URLCONF = 'admin.base.urls'%0AWSGI_APPLICATION = 'admin.base.wsgi.application'%0AADMIN_BASE = 'admin/'%0ASTATIC_URL = '%7B%7Dstatic/'.format(ADMIN_BASE)%0A%0ALANGUAGE_CODE = 'en-us'
+,%0A        'OPTIONS': %7B%0A            'context_processors': %5B%0A                'django.template.context_processors.debug',%0A                'django.template.context_processors.request',%0A                'django.contrib.auth.context_processors.auth',%0A                'django.contrib.messages.context_processors.messages',%0A            %5D,%0A        %7D%0A    %7D%5D%0A%0A%0AROOT_URLCONF = 'admin.base.urls'%0AWSGI_APPLICATION = 'admin.base.wsgi.application'%0AADMIN_BASE = 'admin/'%0ASTATIC_URL = '%7B%7Dstatic/'.format(ADMIN_BASE)%0A%0ASTATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_root')%0A%0ASTATICFILES_DIRS = (%0A    os.path.join(BASE_DIR, 'static'),%0A)%0A%0ALANGUAGE_CODE = 'en-us'%0A%0AWEBPACK_LOADER = %7B%0A    'BUNDLE_DIR_NAME': osf_settings.STATIC_FOLDER,%0A    'STATS_FILE': os.path.join(osf_settings.APP_PATH, 'webpack-stats.json'),%0A%7D
 %0A

add webpack loader, static root, static dirs