"""
Django settings for sp_sso 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/
"""

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

from django.conf import global_settings
from django.utils.translation import ugettext_lazy as _

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


# Logger config copied from the authentic2 project settings:
LOGGING_CONFIG = None
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '[%(asctime)s] %(ip)s %(user)s %(request_id)s %(levelname)s %(name)s.%(funcName)s: %(message)s',
            'datefmt': '%Y-%m-%d %a %H:%M:%S'
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class':'logging.StreamHandler',
            'formatter': 'verbose',
            'filters': ['cleaning', 'request_context'],
            'filename' : '/var/log/django/sp_sso.log',
        },
    },
    'loggers': {
        # even when debugging seeing SQL queries is too much, activate it
        # explicitly using DEBUG_DB
        'django': {
                'level': 'INFO',
                'handlers' : ['console'],
        },
    },
}

# 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 = '3568aw3i!gv@xb_nloh0$(lz%^y-2-&ey_qx^i)&_j!26#q)73'

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = ['localhost', 'sp-condorcet.dev.entrouvert.org']

MELLON_HACK = True

TEMPLATE_DIRS = (
    'templates',
)

USE_I18N = False

AUTHENTICATION_BACKENDS = (
    'mellon.backends.SAMLBackend',
    'django.contrib.auth.backends.ModelBackend',
)

AUTH_USER_MODEL = 'saml.SupAnnUser'

LOGIN_REDIRECT_URL = '/register/'
LOGIN_URL = '/login/'
LOGOUT_URL = '/logout/'

MELLON_ATTRIBUTE_MAPPING = {
    'first_name': '{attributes[fname_test][0]}',
    'last_name': '{attributes[lname_test][0]}',
    'email' : '{attributes[email_test][0]}',
    'password' : '{attributes[password_test][0]}',
    'ep_principal_name' : '{attributes[ep_principal_name][0]}',
    's_etablissement' : '{attributes[s_etablissement][0]}',
    'ep_primary_affiliation' : '{attributes[ep_primary_affiliation][0]}',
    'ep_affiliation' : '{attributes[ep_affiliation][0]}',
    's_entite_affectation_principale' : '{attributes[s_entite_affectation_principale][0]}',
    's_entite_affectation' : '{attributes[s_entite_affectation][0]}',
    's_emp_corps' : '{attributes[s_emp_corps][0]}',
    's_liste_rouge' : '{attributes[s_liste_rouge][0]}',
}

MELLON_SUPERUSER_MAPPING = {
    'is_superuser': 'true',
}

MELLON_USERNAME_TEMPLATE = '{attributes[name_id_content]}'

MELLON_IDENTITY_PROVIDERS = [{
    'METADATA_URL': 'http://idp-condorcet.dev.entrouvert.org/idp/saml2/metadata'
}]


INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'saml',
    'mellon',
    'gadjo',
)

MIDDLEWARE_CLASSES = (
    '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.locale.LocaleMiddleware',
)

ROOT_URLCONF = 'sp_sso.urls'

WSGI_APPLICATION = 'sp_sso.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'),
    }
}

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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = False

USE_TZ = True

LANGUAGES = (
    ('fr', _('French')),
    ('en', _('English')),
)

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale_dir/'),
)

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

STATIC_URL = '/static/'
STATIC_ROOT = 'collected_static'

STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
STATICFILES_FINDERS = global_settings.STATICFILES_FINDERS + ('gadjo.finders.XStaticFinder',)

SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SESSION_COOKIE_AGE = 30 * 60
