"""
Django settings for hiwi_stunden project.

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

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

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

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
import ldap
import logging
import configparser
from django_auth_ldap.config import LDAPSearch, LDAPSearch

LOGIN_URL = 'login'
LOGIN_REDIRECT_URL = '/'
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

AUTH_USER_MODEL = 'hiwi_portal.Hiwi'
# 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 = '0nc$y#t0hoi80rlvp&!#(#7w%0mp*l(cooc(ck@a!@kq+k+llb'

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

ALLOWED_HOSTS = ['localhost', 'w4h.scc.kit.edu']

# Application definition

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # "debug_toolbar",
    'hiwi_portal',
    'django_auth_ldap',
    'mathfilters'
)


MIDDLEWARE = [
    # 'debug_toolbar.middleware.DebugToolbarMiddleware',
    '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',
]

ROOT_URLCONF = 'hiwi_stunden.urls'
AUTHENTICATION_BACKENDS = (
    'django_auth_ldap.backend.LDAPBackend',
    'django.contrib.auth.backends.ModelBackend',
)
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',
            ],
        },
    },
]

WSGI_APPLICATION = 'hiwi_stunden.wsgi.application'

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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'hiwi-portal',
        'USER': 'hiwi-portal',
        'PASSWORD': '',
        'HOST': 'localhost',
    }
}

# 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

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)
STATIC_URL = '/static/'

AUTH_LDAP_BIND_DN = "foo"
AUTH_LDAP_BIND_PASSWORD = "foo"
AUTH_LDAP_SERVER_URI = "ldaps://foo:636"
config = configparser.ConfigParser()
config.read("config.ini")
try:
    AUTH_LDAP_BIND_DN = config.get("ldap", "bind_dn")
    AUTH_LDAP_BIND_PASSWORD = config.get("ldap", "bind_pw")
    AUTH_LDAP_SERVER_URI = config.get("ldap", "server_uri")

except configparser.NoOptionError as e:
    print ("Missing configuartion! ", e)
    exit(1)

AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=unix,ou=IDM,dc=kit,dc=edu",
                                   ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
AUTH_LDAP_START_TLS = False
AUTH_LDAP_USER_ATTR_MAP = {"firstname": "givenName", "lastname": "sn", "email": "mail"}
logger = logging.getLogger('django_auth_ldap')
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)


INTERNAL_IPS = [
    '127.0.0.1',
    '::1',
]
