"""
Django settings for oncall project.

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

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

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

import os
import ldap
from django_auth_ldap.config import LDAPSearch, PosixGroupType

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, "templates")
STATIC_DIR = os.path.join(BASE_DIR,"static")

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ["DEBUG"]

ALLOWED_HOSTS = ['*']

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

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'roster',
]

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


ROOT_URLCONF = 'oncall.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR,],
        '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 = 'oncall.wsgi.application'

# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'OPTIONS': {
            'sql_mode': 'traditional',
        },
    'NAME': os.environ['DB_DATABASE'],
    'USER': os.environ['DB_USERNAME'],
    'PASSWORD': os.environ['DB_PASSWORD'],
    'HOST': os.environ['DB_HOSTNAME'],
    'PORT': os.environ['DB_PORT'],
    }
}

CACHES = {
     "default": {
         "BACKEND": "django_redis.cache.RedisCache",
         "LOCATION": "redis://redis:6379/0",
         "OPTIONS": {
             "CLIENT_CLASS": "django_redis.client.DefaultClient"
         },
         "KEY_PREFIX": "oncall"
     }
 }

# Celery Configuration
CELERY_BROKER_URL = 'redis://redis:6379/0'
CELERY_RESULT_BACKEND = 'redis://redis:6379/0'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Australia/Adelaide'

# LDAP Configuration - using django-auth-ldap
# LDAP URI - ldaps://ldap.adelaide.edu.au
# AUTH_LDAP_SERVER_URI = os.environ['LDAPURI']
# AUTH_LDAP_BIND_AS_AUTHENTICATING_USER = True
# AUTH_LDAP_START_TLS = True
# AUTH_LDAP_USER_DN_TEMPLATE = "uid=%(user)s,ou=People,dc=adelaide,dc=edu,dc=au"

# # Simple group restrictions
# AUTH_LDAP_GROUP_SEARCH = LDAPSearch(
#     'ou=groups,dc=adelaide,dc=edu,dc=au',
#     ldap.SCOPE_SUBTREE,
#     '(objectClass=PosixGroupType)',
# )
# AUTH_LDAP_GROUP_TYPE = PosixGroupType(name_attr='cn')
# AUTH_LDAP_REQUIRE_GROUP = "cn=isoncallroster,ou=groups,dc=adelaide,dc=edu,dc=au"
# AUTH_LDAP_USER_ATTR_MAP = {
# "givenName": "cn",
# "last_name": "sn",
# "email": "mail"
# }
# AUTH_LDAP_MIRROR_GROUPS = True
# AUTH_LDAP_ALWAYS_UPDATE_USER = True
# AUTH_LDAP_USER_FLAGS_BY_GROUP = {
#  "is_active": ("cn=isoncallroster,ou=groups,dc=adelaide,dc=edu,dc=au",),
#  "is_staff": ("cn=isteamleaders,ou=groups,dc=adelaide,dc=edu,dc=au",)
# }
# AUTH_LDAP_GROUP_CACHE_TIMEOUT = 1
# AUTHENTICATION_BACKENDS = [
#     'django_auth_ldap.backend.LDAPBackend',
#     'django.contrib.auth.backends.ModelBackend',
# ]

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

MESSAGE_TAGS = {
    'messages.DEBUG': 'alert-info',
    'messages.INFO': 'alert-info',
    'messages.SUCCESS': 'alert-success',
    'messages.WARNING': 'alert-warning',
    'messages.ERROR': 'alert-danger',
}
# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Australia/Adelaide'

USE_I18N = True

USE_L10N = True

USE_TZ = False


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

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    STATIC_DIR,
]

LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'



