"""
Django settings for KubeOps project.

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

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

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

import os
import sys
import pymysql
pymysql.version_info = (1, 3, 13, "final", 0)

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))

# 存放key文件
TMP_DIR = os.path.join(BASE_DIR, 'tmp')
if not os.path.isdir(TMP_DIR):
    os.makedirs(TMP_DIR)

# 存放sftp临时文件
SFTP_DIR = os.path.join(BASE_DIR, 'sftp_tmp')
if not os.path.isdir(SFTP_DIR):
    os.makedirs(SFTP_DIR)

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'qrq=wh0&7v0%o88k-!3i^c+8@ou%g96qoc#%%r8*1bsoy*bj%2'

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

ALLOWED_HOSTS = ['*']

# Application definition

INSTALLED_APPS = [
    'simpleui',
    'channels',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'django_filters',
    'corsheaders',  # Django跨域解决
    'django_celery_beat',
    'rbac',
    'assets',
    'webssh',
    'monitor',
    'file',
    'wiki',
    'settings',
    'k8s',
    'tasks'
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',  # corsheaders跨域
    'django.middleware.common.CommonMiddleware',  # corsheaders跨域
    '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',
]

# 跨域CORS设置
CORS_ORIGIN_ALLOW_ALL = True  # 默认为False，如果为True则允许所有连接
CORS_ORIGIN_WHITELIST = ()
# CORS_ORIGIN_WHITELIST = (  # 配置允许访问的白名单
#     'http://localhost:8080',
#     'http://localhost:8081',
#     'http://127.0.0.1:8080',
#     'http://127.0.0.1:8081',
# )

ROOT_URLCONF = 'KubeOps.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 = 'KubeOps.wsgi.application'
ASGI_APPLICATION = 'KubeOps.routing.application'

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

pymysql.install_as_MySQLdb()
DATABASES = {
    'default': {
        # 'ENGINE': 'django.db.backends.sqlite3',
        # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'kubeops_rbac',
        'USER': 'root',
        'PASSWORD': '19218xql',
        'PORT': '3306',
        'HOST': '127.0.0.1',
        'TEST': {
            'CHARSET': 'utf8',
            'COLLATION': 'utf8_general_ci'
        }
    }
}

# Password validation
# https://docs.djangoproject.com/en/3.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/3.0/topics/i18n/

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = True
# 数据库存储使用时间，True时间会被存为UTC的时间
# USE_TZ = False

AUTH_USER_MODEL = 'rbac.UserProfile'

# CELERY配置
CELERY_BROKER_URL = 'redis://127.0.0.1:6379/0'  # Broker配置，使用Redis作为消息中间件
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/0'  # BACKEND配置
CELERY_ACCEPT_CONTENT = ['application/json', ]
CELERY_RESULT_SERIALIZER = 'json'  # 结果序列化方案
CELERY_TIMEZONE = TIME_ZONE

# 指定simpleui默认的主题,指定一个文件名，相对路径就从simpleui的theme目录读取
SIMPLEUI_DEFAULT_THEME = 'admin.lte.css'
SIMPLEUI_HOME_INFO = False
SIMPLEUI_ICON = {
    'Wiki': 'fab fa-wikipedia-w',
    'Assets': 'fas fa-database',
    '服务器资产表': 'fas fa-list',
    '背景颜色': 'fas fa-th',
    '角色': 'fas fa-users',
}

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

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")

# rest framework配置
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'utils.authorization.MyAuthentication'
    ],
    # 权限认证
    'DEFAULT_PERMISSION_CLASSES': [
        'utils.permissions.MyPermission',
    ],
    'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
    'DEFAULT_PAGINATION_CLASS': 'utils.pagination.MyPageNumberPagination',
    'DEFAULT_FILTER_BACKENDS': [
        'django_filters.rest_framework.DjangoFilterBackend',
        'rest_framework.filters.OrderingFilter']
}
