# coding:utf8
"""
Django settings for darkripples project.

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

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

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

import os, sys

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
# web root
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# now root
BASEPATH = os.path.split(__file__)[0]
STATICPATH = os.path.join(BASE_DIR, 'static')
CACHE_DIR = os.path.join(BASE_DIR, 'tmp')
LOGS_DIR = os.path.join(BASE_DIR, 'logs')

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'ao)k*rjqle#h)@1w1-so%-jbxs4*u7lw(&*ex(k)3b+nwo@6n1'

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

# because of Nginx，so，this settings may be CAN'T WORK
ALLOWED_HOSTS = ['127.0.0.1', '192.168.0.105', 'www.darkripples.com', 'localhost']

# 设置请求是否允许携带Cookie，必须和xhrFields: {withCredentials: true,}同时使用
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
# 允许所有的请求头
from corsheaders.defaults import default_headers

# 跨域相关配置
CORS_ALLOW_HEADERS = list(default_headers) + [
    'Token', 'Content-Type'
]

# 跨域源的白名单，需要跨域的源设置在这里
# CORS_ORIGIN_WHITELIST = ['*']
# 指定哪些方法可以发送跨域请求
CORS_ALLOW_METHODS = ('DELETE', 'GET', 'OPTIONS', 'PATCH', 'POST', 'PUT')

# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # django-cors-headers 跨域相关
    'corsheaders',
    'app_blog',
    'app_dr',

]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',  # 跨域：注意顺序，必须放在CommonMiddleware之上
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'corsheaders.middleware.CorsPostCsrfMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

MIDDLEWARE += [
    # 自定义的中间件.必须放在最后
    'darkripples.middleware.dr_middleware.DRMiddleware',
]

ROOT_URLCONF = 'darkripples.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(STATICPATH, 'tmpl')],
        '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 = 'darkripples.wsgi.application'

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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

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

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

TIME_ZONE = 'Asia/Shanghai'
LANGUAGE_CODE = 'zh_Hans'
LANGUAGE_CODE = 'zh-hans'

USE_I18N = True

USE_L10N = True

USE_TZ = True

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

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".  
    # Always use forward slashes, even on Windows.  
    # Don't forget to use absolute paths, not relative paths.  
    os.path.join(BASE_DIR, 'static'),
)

# 设置项是否开启URL访问地址后面不为/跳转至带有/的路径
APPEND_SLASH = True

# 定时任务crontab的字符集
CRONTAB_COMMAND_PREFIX = 'LANG_ALL=zh_cn.UTF-8'

sys.path.insert(0, os.path.join(BASEPATH))

from conf import DATABASES
