"""
Django settings for users project.

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

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

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""
__author__ = ["Arun Reghunathan"]

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
from celery.schedules import crontab
from mongoengine import connect

from project import settings_conf
from project.private_conf import PRIVATE_SECRET_KEY, PRIVATE_REDIS_PASSWORD, PRIVATE_REDIS_HOST, PRIVATE_REDIS_PORT, \
    PRIVATE_MONGO_USER, PRIVATE_MONGO_PASSWORD, PRIVATE_MONGO_HOST, PRIVATE_MONGO_DB

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


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = PRIVATE_SECRET_KEY

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

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework_mongoengine',
    'mongoengine',
    'corsheaders'
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    # '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',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    # 'corsheaders.middleware.CorsPostCsrfMiddleware',
    'src.common.libraries.middleware.ExceptionMiddleware',
    'src.common.libraries.middleware.AuthenticationMiddleware',
    'src.common.libraries.middleware.RequestMiddleware'
]

ROOT_URLCONF = 'project.urls'

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

# django rest framework specific
REST_FRAMEWORK = {
    'PAGE_SIZE': 10,
    'EXCEPTION_HANDLER': 'src.common.libraries.customexceptionhandler.custom_exception_handler',
    'DEFAULT_AUTHENTICATION_CLASSES': [],
    'DEFAULT_PERMISSION_CLASSES': [],
    # 'DEFAULT_PERMISSION_CLASSES': (
    #      'rest_framework.permissions.AllowAny',
    # ),

    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
    ),

    'DEFAULT_PARSER_CLASSES': (
        'rest_framework.parsers.JSONParser',

    ),
}

WSGI_APPLICATION = 'project.wsgi.application'


REDIS_URL_NO_DB = 'redis://:{0}@{1}:{2}/'.format(PRIVATE_REDIS_PASSWORD, PRIVATE_REDIS_HOST, PRIVATE_REDIS_PORT)
REDIS_CELERY_BROKER_DB = '5'
REDIS_CELERY_RESULT_DB = '6'
REDIS_CELERY_BROKER_URL = REDIS_URL_NO_DB + REDIS_CELERY_BROKER_DB
REDIS_CELERY_RESULT_URL = REDIS_URL_NO_DB + REDIS_CELERY_RESULT_DB

# CELERY_TASK_SERIALIZER = 'json'
# CELERY_BEAT_SCHEDULER = 'redisbeat.RedisScheduler'
CELERY_BROKER_URL = REDIS_CELERY_BROKER_URL
CELERY_RESULT_BACKEND = REDIS_CELERY_RESULT_URL  # 'sqs;//' no support for SQS result backend for now
# CELERY_REDIS_MULTI_NODE_MODE = True

CELERY_RESULT_SERIALIZER = 'json'
CELERY_ENABLE_UTC = True
CELERY_DEFAULT_QUEUE = 'celery'
CELERY_QUEUES = {
    CELERY_DEFAULT_QUEUE: {
        'exchange': CELERY_DEFAULT_QUEUE,
        'binding_key': CELERY_DEFAULT_QUEUE,
    }
}

CELERY_BEAT_SCHEDULE = {
    'test': {
        'task': 'project.tasks.test',
        'schedule': crontab(minute=0, hour='*')
    }
}


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

DATABASES = {
    'default': {'ENGINE': 'django.db.backends.dummy'}
}

# Mongoengine settings
# SESSION_ENGINE = 'django_mongoengine.sessions'
# SESSION_SERIALIZER = 'django_mongoengine.sessions.BSONSerializer'

MONGO_CONSTANTS = (PRIVATE_MONGO_USER, PRIVATE_MONGO_PASSWORD, PRIVATE_MONGO_HOST, PRIVATE_MONGO_DB)
MONGO_URL = 'mongodb://{0}:{1}@{2}/{3}'.format(*MONGO_CONSTANTS)

DEFAULT_CONNECTION_NAME = connect(db=PRIVATE_MONGO_DB, host=MONGO_URL, alias='default')


# Password validation
# https://docs.djangoproject.com/en/1.11/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',
    },
]
AUTHENTICATION_BACKENDS = (
    'django_mongoengine.mongo_auth.backends.MongoEngineBackend',
)

# Internationalization
# https://docs.djangoproject.com/en/1.11/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.11/howto/static-files/

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = STATIC_ROOT+'/'

# CORS settings
CORS_ORIGIN_ALLOW_ALL = True

CORS_ALLOW_METHODS = (
    'GET',
    'POST',
    'PUT',
    'PATCH',
    'DELETE',
    'OPTIONS'
)

CORS_ALLOW_HEADERS = (
    'accept',
    'accept-encoding',
    'authorization',
    'content-type',
    'dnt',
    'origin',
    'user-agent',
    'x-csrftoken',
    'x-requested-with',
)




LOGGING = settings_conf.LOGGING
