"""
Django settings for blog project.

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

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

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
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.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 't-5&$6-s%rg7-xzm3+79%7iq_o@@m)gl3icw%*oe&l5t!sfznu'

# SECURITY WARNING: don't run with debug turned on in production!
# DEBUG = True
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',
    # 子应用的注册
    'users.apps.UsersConfig',
    'home.apps.HomeConfig'
]

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',
]
# 注意不能带py后缀
ROOT_URLCONF = 'blog.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 = 'blog.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',  # 数据库引擎
        'HOST': 'localhost',  # 数据库主机名
        'PORT': '3306',  # 数据库端口号
        'USER': 'blog',  # 数据库用户名
        'PASSWORD': '123456',  # 数据库用户密码
        'NAME': 'blog',  # 数据库名
    }
}


# 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/

# 修改语言
# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-Hans'

# 修改时区
# TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'

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 = [
    os.path.join(BASE_DIR, 'static')
]

# redis 的配置

CACHES = {
    'default': {  # 默认
        'BACKEND': 'django_redis.cache.RedisCache',
        #  redis默认是6379端口，第0的数据库，这里我们选择第0个数据库，123456是密码
        # 'LOCATION': 'redis://:123456@127.0.0.1:6379/0',
        # 无密码
        'LOCATION': 'redis://127.0.0.1:6379/0',
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient',
        }
    },
    'session': {  # 会话 session
        'BACKEND': 'django_redis.cache.RedisCache',
        #  redis默认是6379端口，第0的数据库，这里我们选择第1个数据库，123456是密码
        # 'LOCATION': 'redis://:123456@127.0.0.1:6379/1',
        # 无密码
        'LOCATION': 'redis://127.0.0.1:6379/1',
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient',
        }
    }
}
# session由数据库存储改为由redis存储
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
SESSION_CACHE_ALIAS = 'session'


# 日志配置

LOGGING = {
    'version': 1,
    # 是否禁用已经存在的日志器
    'disable_existing_loggers': False,
    # 日志格式器配置
    'formatters': {
        'standard': {
            'format': '%(asctime)s [%(threadName)s: %(thread)d]'
                      '%(pathname)s: %(funcName)s: %(lineno)d %(levelname)s - %(message)s'
        },
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(lineno)d %(process)d  %(thread)d %(message)s'
        },
        'simple': {  # 简单格式
            'format': '%(levelname)s %(message)s'
        },
    },
    # 过滤器test配置
    'filters': {
        'require_debug_true': {  # django 在 debug模式下才输出日志
            '()': 'django.utils.log.RequireDebugTrue',
        }
    },
    # 处理器配置
    'handlers': {
        # 终端处理器配置，向终端输出日志
        'console': {
            'level': 'INFO',
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'  # 使用上面定义的standard格式器
        },
        # 文件处理器配置，向文件中输出日志
        'file': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(BASE_DIR, 'logs/blog.log'),  # 日志文件位置
            'maxBytes': 1024*1024*1024,   # 文件大小 达到1G自动分割
            'backupCount': 5,  # 保存备份文件的数量
            'formatter': 'verbose',  # 输出格式，使用上面定义的standard格式器
            'encoding': 'utf-8'  # 指定文件编码
        }
    },
    # 配置日志实例
    'loggers': {  # 日志器
        'django': {  # 日志实例名，定义了一个django日志器
            'handlers': ['console', 'file'],  # 可同时向终端和文件中输出日志
            # 'filters': ['test'],
            'propagate': True,  # 是否继续传递日志信息
            'level': 'INFO'  # 日志接收器的最低日志级别
        }
    }
}

# 自定义User模型代替系统的User
AUTH_USER_MODEL = 'users.User'

# 如果用户未登录的话，则会进行默认的跳转
# 默认的跳转链接是：account/login/?next=xxx
# 修改系统的未登录的跳转链接
LOGIN_URL = '/login/'

# 设置上传的图片保存到media
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

# 设置图片访问的统一路由
MEDIA_URL = '/media/'