"""
Django settings for selp project.

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

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
import json
import os.path
from os.path import expanduser
from django.core.exceptions import ImproperlyConfigured

# With this, no need to hardcode home directory
home_directory = expanduser("~")
secrets_path = home_directory + "/.env"

if os.path.exists(secrets_path):
    with open(secrets_path) as f:
        secrets = json.loads(f.read())
else:
    secrets = None


def get_secret(setting, secrets, default=""):
    """Get the secret variable or return explicit exception."""
    if secrets is None:
        return default
    try:
        return secrets[setting]
    except KeyError:
        error_msg = "Set the {0} environment variable".format(setting)
        raise ImproperlyConfigured(error_msg)

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

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

from assets.constants import ADMIN, SUPER_ADMIN, SUPERVISOR

USER_ROLES = (
    SUPERVISOR,
    ADMIN,
    SUPER_ADMIN
)

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '5jkb8$c4vz%fs9qe&_$f)+x!9-k4&$b%x66_ww6v26f74(c%n3'

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = ['*']

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': os.path.join(PROJECT_DIR, 'phd_admissions.log'),
        },
        "console": {
            "class": "logging.StreamHandler",
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
        "django_python3_ldap": {
            "handlers": ["console", "file"],
            "level": "INFO",
        },
    },
}

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        'rest_framework.authentication.BasicAuthentication',
    ),
    'FORM_METHOD_OVERRIDE': None,
    'FORM_CONTENT_OVERRIDE': None,
    'FORM_CONTENTTYPE_OVERRIDE': None
}

# Currently, token expiration is switched off
JWT_AUTH = {
    'JWT_VERIFY_EXPIRATION': False
}

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'compressor',
    'rest_framework',
    'django.contrib.humanize',
    'phdadmissions',
    'authentication',
    'silk',
    'tagging',
    'django_python3_ldap',
    'annoying'
)

MIDDLEWARE_CLASSES = (
    'silk.middleware.SilkyMiddleware',
    'phdadmissions.utilities.last_visit_middleware.SetLastVisitMiddleware',
    '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',
)

ROOT_URLCONF = 'assets.urls'

WSGI_APPLICATION = 'assets.wsgi.application'

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

DATABASES = {
    'default': {
        #'ENGINE': 'django.db.backends.sqlite3',
        #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'phdadmissions_db',
        'USER': 'django_user',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

DATE_INPUT_FORMATS = ('%d-%m-%Y', '%Y-%m-%d')

# Internationalization
# https://docs.djangoproject.com/en/1.7/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/1.7/howto/static-files/
STATIC_ROOT = get_secret('STATIC_ROOT', secrets, "")
STATIC_URL = '/static/'
STATIC_DIR = os.path.join(PROJECT_DIR, 'dev_static')
STATICFILES_DIRS = (
    STATIC_DIR,
)
STATICFILES_FINDERS = {
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'compressor.finders.CompressorFinder',
}

COMPRESS_ROOT = STATIC_DIR
COMPRESS_OFFLINE = True
COMPRESS_ENABLED = False

# Template files
TEMPLATE_DIR = os.path.join(PROJECT_DIR, 'templates')
TEMPLATE_DIRS = (
    TEMPLATE_DIR,
)

# development static media server
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')

# development purposes only!
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smarthost.cc.ic.ac.uk'
EMAIL_PORT = 25
EMAIL_HOST_USER = 'ms12115'
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False

# The URL of the LDAP server.
LDAP_AUTH_URL = "ldaps://ldaps-vip.cc.ic.ac.uk:636"

# Initiate TLS on connection.
LDAP_AUTH_USE_TLS = False

# The LDAP search base for looking up users.
LDAP_AUTH_SEARCH_BASE = "dc=ic,dc=ac,dc=uk"

# The LDAP class that represents a user.
LDAP_AUTH_OBJECT_CLASS = "User"

# User model fields mapped to the LDAP
# attributes that represent them.
LDAP_AUTH_USER_FIELDS = {
    "username": "sAMAccountName",
    "first_name": "givenName",
    "last_name": "sn",
    "email": "mail",
}

# A tuple of django model fields used to uniquely identify a user.
LDAP_AUTH_USER_LOOKUP_FIELDS = ("username",)

# Path to a callable that takes a dict of {model_field_name: value},
# returning a dict of clean model data.
# Use this to customize how data loaded from LDAP is saved to the User model.
LDAP_AUTH_CLEAN_USER_DATA = "django_python3_ldap.utils.clean_user_data"

# Path to a callable that takes a user model and a dict of {ldap_field_name: [value]},
# and saves any additional user relationships based on the LDAP data.
# Use this to customize how data loaded from LDAP is saved to User model relations.
# For customizing non-related User model fields, use LDAP_AUTH_CLEAN_USER_DATA.
LDAP_AUTH_SYNC_USER_RELATIONS = "django_python3_ldap.utils.sync_user_relations"

# Path to a callable that takes a dict of {ldap_field_name: value},
# returning a list of [ldap_search_filter]. The search filters will then be AND'd
# together when creating the final search filter.
LDAP_AUTH_FORMAT_SEARCH_FILTERS = "phdadmissions.utilities.ldap_search_filters.doc_staff_search_filter"

# Path to a callable that takes a dict of {model_field_name: value}, and returns
# a string of the username to bind to the LDAP server.
# Use this to support different types of LDAP server.
LDAP_AUTH_FORMAT_USERNAME = "django_python3_ldap.utils.format_username_active_directory_principal"
# Sets the login domain for Active Directory users.
LDAP_AUTH_ACTIVE_DIRECTORY_DOMAIN = "IC.AC.UK"

# The LDAP username and password of a user for querying the LDAP database for user
# details. If None, then the authenticated user will be used for querying, and
# the `ldap_sync_users` command will perform an anonymous query.
LDAP_AUTH_CONNECTION_USERNAME = "ms12115"
LDAP_AUTH_CONNECTION_PASSWORD = get_secret('LDAP_PASSWORD', secrets, "")

# Keep ModelBackend around for per-user permissions and maybe a local
# superuser.
AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'django_python3_ldap.auth.LDAPBackend'
)
