"""
Django settings for TAdminPro project.

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

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

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

import os
import sys

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
from celery.schedules import crontab, timedelta
from django.middleware.csrf import CsrfViewMiddleware
from django.middleware.security import SecurityMiddleware

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/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'n10urh+_z_lf$&475p8c@ock*y3r*6n0m0m3mdmtix7s3ik7!q'

# 设置app存放的位置
sys.path.insert(0, os.path.join(BASE_DIR, 'myapps'))
sys.path.insert(1, os.path.join(BASE_DIR, 'exapps'))

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

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = [
    # 'django.contrib.admin',
    'xadmin',
    'crispy_forms',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'art',
    'DjangoUeditor',
    'user',
    'djcelery',  # django-celery的app
    'rest_framework',

]

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',
    'user.helper.log_exec',
]

APPEND_SLASH = False

ROOT_URLCONF = 'TAdminPro.urls'

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 = 'TAdminPro.wsgi.application'

# Database
# https://docs.djangoproject.com/en/2.0/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.0/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.0/topics/i18n/

LANGUAGE_CODE = 'zh-Hans'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = True

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

# 访问静态文件的url路径
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

# 媒体文件路径
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/ups')

# 配置媒体文件的访问路径(富文本中使用)
MEDIA_URL = '/static/ups/'

REDIS = {
    'host': '127.0.0.1',
    'db': 0,
    'port': 6379
}

# ------start celery config--------
import djcelery

djcelery.setup_loader()  # 装载celery服务

BROKER_URL = 'redis://127.0.0.1:6379/10'  # 配置消息中间件位置

CELERY_TIMEZONE = 'Asia/Shanghai'  # 时区

# 配置计划任务存储的位置
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'

# 配置定时任务
CELERY_IMPORTS = ('art.tasks',)
CELERYBEAT_SCHEDULE = {
    u'ReadTask': {
        'task': 'art.tasks.readArt',
        'schedule': crontab(minute='*/10'),  # 每2分钟执行一次
        'args': (1, 8),
    },
    u'BuyTask': {
        'task': 'art.tasks.buyArt',
        'schedule': timedelta(seconds=60),
        'args': (1, 5),
    }
}

# ----end celery config-----


# －－－配置日志－－－－－
LOGGING = {
    'version': 1.0,
    'disable_exists_logger': True,
    # 日志格式器
    'formatters': {
        'info': {
            'format': '[<%(asctime)s>:%(funcName)s-%(lineno)s]%(message)s',
            'datefmt': '%Y-%m-%d %H:%M:%S',
        }
    },
    # 处理器:  格式器Formatter
    'handlers': {
        'console': {
            'level': 'INFO',
            'formatter': 'info',
            'class': 'logging.StreamHandler'
        },
        'logFile': {
            'level': 'INFO',
            'formatter': 'info',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'filename': 'a.log',
            'encoding': 'utf-8'  # 解决Window的中文乱码
        }
    },

    # 日志对象： 处理器Handler
    'loggers': {
        'mdjango': {
            'handlers': ['console', 'logFile'],
            'level': 'INFO',
            'propagate': False
        }
    }
}

# -----end logger------


# --django-rest-framework----
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES':[
        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
    ]
}

# ----end django-rest-framework-----