"""
Django settings for peer-testing project.

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

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

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

import os
from . import local
from . import secret

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = local.LOCAL_BASE_DIR

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

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

ADMINS = local.LOCAL_ADMINS

ALLOWED_HOSTS = local.HOSTS


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'common',
    'feedback',
    'file_viewer',
    'student',
    'teacher',
    'test_match',
    'django.contrib.sites',
    'django_comments',
    'social_django'
]

SITE_ID = 1

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',
    'main.oauth_exception_middleware.PeerTestingSocialAuthExceptionMiddleware',
]

ROOT_URLCONF = 'main.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',
                'social_django.context_processors.backends',
                'social_django.context_processors.login_redirect',
                'common.context_processors.logout_url'
            ],
        },
    },
]

WSGI_APPLICATION = 'main.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
#
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.10/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',
    },
]

# https://python-social-auth.readthedocs.io/en/latest/configuration/django.html#authentication-backends
AUTHENTICATION_BACKENDS = (
#    'social_core.backends.open_id.OpenIdAuth',
    'social_core.backends.gitlab.GitLabOAuth2',
#    'social_core.backends.azuread_tenant.AzureADOAuth2',
#    'social_core.backends.azuread_tenant.AzureADTenantOAuth2'
    'django.contrib.auth.backends.ModelBackend'
)

SOCIAL_AUTH_URL_NAMESPACE = 'social'

SOCIAL_AUTH_PIPELINE = (
    'social_core.pipeline.social_auth.social_details',
    'social_core.pipeline.social_auth.social_uid',
    'social_core.pipeline.social_auth.social_user',
    'social_core.pipeline.user.get_username',
    'social_core.pipeline.user.create_user',
    'social_core.pipeline.social_auth.associate_user',
    'social_core.pipeline.social_auth.load_extra_data',
    'social_core.pipeline.user.user_details',
    'main.enrol_new_user.enrol_new_user'
    #'social_core.pipeline.social_auth.associate_by_email',
)

# SOCIAL_AUTH_AZUREAD_OAUTH2_KEY = secret.SOCIAL_AUTH_AZUREAD_OAUTH2_KEY
# SOCIAL_AUTH_AZUREAD_OAUTH2_SECRET = secret.SOCIAL_AUTH_AZUREAD_OAUTH2_KEY
# SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_KEY = secret.SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_KEY
# SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_SECRET = secret.SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_SECRET
# SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_TENANT_ID = secret.SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_TENANT_ID


# SOCIAL_AUTH_AZUREAD_OAUTH2_WHITELISTED_DOMAINS = local.WHITELISTED_DOMAINS
# SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_WHITELISTED_DOMAINS = local.WHITELISTED_DOMAINS
# SOCIAL_AUTH_AZUREAD_OAUTH2_WHITELISTED_EMAILS = local.WHITELISTED_EMAILS
# SOCIAL_AUTH_AZUREAD_TENANT_OAUTH2_WHITELISTED_EMAILS = local.WHITELISTED_EMAILS

VERIFY_SSL=False
SOCIAL_AUTH_GITLAB_SCOPE = ['api']
SOCIAL_AUTH_GITLAB_KEY = secret.SOCIAL_AUTH_GITLAB_KEY
SOCIAL_AUTH_GITLAB_SECRET = secret.SOCIAL_AUTH_GITLAB_SECRET
SOCIAL_AUTH_GITLAB_API_URL = local.SOCIAL_AUTH_GITLAB_API_URL
# Social Django configuration for GitLab OAuth
# https://python-social-auth.readthedocs.io/en/latest/backends/gitlab.html

PEER_TESTING_API_ACCESS_TOKEN = secret.PEER_TESTING_API_ACCESS_TOKEN

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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Europe/London'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = local.HTTP_PREFIX + '/static_files/'
# this is the url that is used in the templates
#  this is absolute so should have prepended paths
STATIC_URL_REDIRECT = 'static_files/'
# this is the bit that is looked for in the url config
# this should not include any prepended paths
STATIC_ROOT = os.path.join(BASE_DIR, 'var/static/')
# this is the place on the disk where the static files are looked for
# everything after the static url is appened to this path

MEDIA_URL = local.HTTP_PREFIX + '/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'var/uploads')
MEDIA_TMP_TEST = os.path.join(BASE_DIR, 'var/tmp/test')


# Auth URLconf
LOGIN_URL = local.HTTP_PREFIX + '/login/gitlab/'
LOGOUT_REDIRECT_URL =  local.HTTP_PREFIX + '/'

# dont run dangerous actions as apache
owner = os.stat(BASE_DIR)
OWNER_UID = owner.st_uid
OWNER_GID = owner.st_gid

SESSION_COOKIE_AGE=3600

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': os.path.join(BASE_DIR, 'var/log/django.log'),
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

import logging.config
logging.config.dictConfig(LOGGING)
