<reponame>yanshicheng/super-ops<filename>super_ops/settings.py
"""
Django settings for super_ops project.

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

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

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
import datetime
import os
from pathlib import Path
from base.config import get_config
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '<KEY>'

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

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # 内部
    'apps.user.apps.UserConfig',
    'apps.permission.apps.PermissionConfig',
    'apps.cmdb.apps.CmdbConfig',
    'apps.service_tree.apps.ServiceTreeConfig',

    # 第三方
    'rest_framework',
    'django_filters',
    'corsheaders',

]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',  # 放在中间件的最上面，就是给响应头加上了一个响应头跨域
    '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',
]

ROOT_URLCONF = 'super_ops.urls'

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

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

# 数据库配置
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': get_config('mysql.prod', 'MYSQL_DB'),
        'HOST': get_config('mysql.prod', 'MYSQL_HOST'),
        'PORT': get_config('mysql.prod', 'MYSQL_PORT'),
        'USER': get_config('mysql.prod', 'MYSQL_USER'),
        'PASSWORD': get_config('mysql.prod', 'MYSQL_PASSWORD'),
        'OPTIONS': {'charset': get_config('mysql.prod', 'MYSQL_CHARSET')},
    }
}

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

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

APPEND_SLASH = False
USE_I18N = True

USE_L10N = True
USE_TZ = False

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

STATIC_URL = '/static/'

# CORS组的配置信息
CORS_ORIGIN_WHITELIST = (
    'http://127.0.0.1:8080',
    'http://172.16.17.32:8080',
    'http://172.16.17.32:8000',
    'http://172.16.17.32:9000',
    'http://172.16.17.32:80',
    'http://172.21.16.16:8080',
    'http://demo.superops.top:8080',
    'http://demo.superops.top:8000',
    'http://demo.superops.top:9000',
    'http://demo.superops.top:80',
    'http://demo.superops.top',
)
# 是否允许ajax跨域请求时携带cookie，False表示不用，我们后面也用不到cookie，所以关掉它就可以了，以防有人通过cookie来搞我们的网站
CORS_ALLOW_CREDENTIALS = False
# 防止黑客入侵
ALLOWED_HOSTS = [
    "127.0.0.1",
    "172.16.17.32",
    "172.21.16.16",
    "demo.superops.top"
]

CORS_ALLOW_METHODS = [
    'DELETE',
    'GET',
    'OPTIONS',
    'PATCH',
    'POST',
    'PUT',
]

CORS_ALLOW_HEADERS = [
    'accept',
    'accept-encoding',
    'authorization',
    'content-type',
    'dnt',
    'origin',
    'user-agent',
    'x-csrftoken',
    'x-requested-with',
    'XMLHttpRequest',
    'X_FILENAME',
    'Pragma',
]
REST_FRAMEWORK = {
    # 自定义异常
    'EXCEPTION_HANDLER': 'base.exceptions.custom_exception_handler',
    # 验证
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
        'apps.permission.utils.verify.ApiRBACPermission'
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
    ),
    'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema',
    'DEFAULT_PAGINATION_CLASS': 'base.base_pagination.NewPagination',
    'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',
                                'rest_framework.filters.SearchFilter',
                                'rest_framework.filters.OrderingFilter',),
    'DATETIME_FORMAT': "%Y-%m-%d %H:%M:%S",
}

JWT_AUTH = {
    'JWT_AUTH_HEADER_PREFIX': 'JWT',
    'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=300000),
    'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=1),
}

# 注意此处不要写成列表或元组的形式
MEDIA_ROOT = os.path.join(BASE_DIR, 'upload')

# 配置 MEDIA_URL 作为公用 URL，指向上传文件的基本路径
MEDIA_URL = '/media/'

AUTH_USER_MODEL = "user.UserProfile"


# 权限白名单
VALID_URL_LIST = [
    '/admin/',
    '/api/v1/user/info/',
    '/api/v1/user/logout/',
    '/api/v1/prem/menu/tree/',
    '/docs/',
    '/api/v1/user/user-profile/1/'
]

"""
Django settings for super_ops project.

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

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

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
import datetime
import os
from pathlib import Path
from base.config import get_config
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '<KEY>'

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

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # 内部
    'apps.user.apps.UserConfig',
    'apps.permission.apps.PermissionConfig',
    'apps.cmdb.apps.CmdbConfig',
    'apps.service_tree.apps.ServiceTreeConfig',

    # 第三方
    'rest_framework',
    'django_filters',
    'corsheaders',

]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',  # 放在中间件的最上面，就是给响应头加上了一个响应头跨域
    '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',
]

ROOT_URLCONF = 'super_ops.urls'

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

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

# 数据库配置
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': get_config('mysql.prod', 'MYSQL_DB'),
        'HOST': get_config('mysql.prod', 'MYSQL_HOST'),
        'PORT': get_config('mysql.prod', 'MYSQL_PORT'),
        'USER': get_config('mysql.prod', 'MYSQL_USER'),
        'PASSWORD': get_config('mysql.prod', 'MYSQL_PASSWORD'),
        'OPTIONS': {'charset': get_config('mysql.prod', 'MYSQL_CHARSET')},
    }
}

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

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

APPEND_SLASH = False
USE_I18N = True

USE_L10N = True
USE_TZ = False

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

STATIC_URL = '/static/'

# CORS组的配置信息
CORS_ORIGIN_WHITELIST = (
    'http://127.0.0.1:8080',
    'http://172.16.17.32:8080',
    'http://172.16.17.32:8000',
    'http://172.16.17.32:9000',
    'http://172.16.17.32:80',
    'http://172.21.16.16:8080',
    'http://demo.superops.top:8080',
    'http://demo.superops.top:8000',
    'http://demo.superops.top:9000',
    'http://demo.superops.top:80',
    'http://demo.superops.top',
)
# 是否允许ajax跨域请求时携带cookie，False表示不用，我们后面也用不到cookie，所以关掉它就可以了，以防有人通过cookie来搞我们的网站
CORS_ALLOW_CREDENTIALS = False
# 防止黑客入侵
ALLOWED_HOSTS = [
    "127.0.0.1",
    "172.16.17.32",
    "172.21.16.16",
    "demo.superops.top"
]

CORS_ALLOW_METHODS = [
    'DELETE',
    'GET',
    'OPTIONS',
    'PATCH',
    'POST',
    'PUT',
]

CORS_ALLOW_HEADERS = [
    'accept',
    'accept-encoding',
    'authorization',
    'content-type',
    'dnt',
    'origin',
    'user-agent',
    'x-csrftoken',
    'x-requested-with',
    'XMLHttpRequest',
    'X_FILENAME',
    'Pragma',
]
REST_FRAMEWORK = {
    # 自定义异常
    'EXCEPTION_HANDLER': 'base.exceptions.custom_exception_handler',
    # 验证
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
        'apps.permission.utils.verify.ApiRBACPermission'
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
    ),
    'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema',
    'DEFAULT_PAGINATION_CLASS': 'base.base_pagination.NewPagination',
    'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',
                                'rest_framework.filters.SearchFilter',
                                'rest_framework.filters.OrderingFilter',),
    'DATETIME_FORMAT': "%Y-%m-%d %H:%M:%S",
}

JWT_AUTH = {
    'JWT_AUTH_HEADER_PREFIX': 'JWT',
    'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=300000),
    'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=1),
}

# 注意此处不要写成列表或元组的形式
MEDIA_ROOT = os.path.join(BASE_DIR, 'upload')

# 配置 MEDIA_URL 作为公用 URL，指向上传文件的基本路径
MEDIA_URL = '/media/'

AUTH_USER_MODEL = "user.UserProfile"


# 权限白名单
VALID_URL_LIST = [
    '/admin/',
    '/api/v1/user/info/',
    '/api/v1/user/logout/',
    '/api/v1/prem/menu/tree/',
    '/docs/',
    '/api/v1/user/user-profile/1/'
]
Django settings for super_ops project.  Generated by 'django-admin startproject' using Django 3.1.2.  For more information on this file, see https://docs.djangoproject.com/en/3.1/topics/settings/  For the full list of settings and their values, see https://docs.djangoproject.com/en/3.1/ref/settings/ # Build paths inside the project like this: BASE_DIR / 'subdir'. # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! # SECURITY WARNING: don't run with debug turned on in production! # Application definition # 内部 # 第三方 # 放在中间件的最上面，就是给响应头加上了一个响应头跨域 # Database # https://docs.djangoproject.com/en/3.1/ref/settings/#databases # 数据库配置 # Password validation # https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators # Internationalization # https://docs.djangoproject.com/en/3.1/topics/i18n/ # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.1/howto/static-files/ # CORS组的配置信息 # 是否允许ajax跨域请求时携带cookie，False表示不用，我们后面也用不到cookie，所以关掉它就可以了，以防有人通过cookie来搞我们的网站 # 防止黑客入侵 # 自定义异常 # 验证 # 注意此处不要写成列表或元组的形式 # 配置 MEDIA_URL 作为公用 URL，指向上传文件的基本路径 # 权限白名单