import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = '$2hf5g$a%_^kk0-l25l$!o5)yc=dvtnfpc8(+$rh4fq4twa_xx'
DEBUG = True
ALLOWED_HOSTS = ["*"]

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'corsheaders',
    'imagekit',
    'Model'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

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



# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
# formal
# DATABASE_DATA = 'Ansjer81'
# SERVER_HOST = 'localhost'
# DATABASES_USER = 'ansjer'
# DATABASES_PASS = '1234'
# test
# DATABASE_DATA = 'AnsjerTest'
# SERVER_HOST = '13.56.215.252'
# DATABASES_USER = 'ansjer'
# DATABASES_PASS = '1234'
DATABASE_DATA = 'ansjerlocal'
SERVER_HOST = '127.0.0.1'
DATABASES_USER = 'root'
DATABASES_PASS = '123456'

DATABASE_DATA2 = 'asjl'
SERVER_HOST2 = '127.0.0.1'
DATABASES_USER2 = 'root'
DATABASES_PASS2 = '123456'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': DATABASE_DATA,
        'USER': DATABASES_USER,
        'PASSWORD': DATABASES_PASS,
        'HOST': SERVER_HOST,
        'PORT': '3306',
        'OPTIONS': {'charset': 'utf8mb4', 'use_unicode': True, 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"},
        'AUTOCOMMIT': True
    },
    'mysql02': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': DATABASE_DATA2,
        'USER': DATABASES_USER2,
        'PASSWORD': DATABASES_PASS2,
        'HOST': SERVER_HOST2,
        'PORT': '3306',
        'OPTIONS': {'charset': 'utf8mb4', 'use_unicode': True, 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"},
        'AUTOCOMMIT': True
    }
}
DATABASE_ROUTERS = ['AnsjerPush.database_router.DatabaseAppsRouter']
DATABASE_APPS_MAPPING = {
    'db1': 'default',
    'db2': 'mysql02',
}



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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'
# 跨域增加忽略
# 跨域增加忽略
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = ()

CORS_ALLOW_METHODS = (
    'DELETE',
    'GET',
    'OPTIONS',
    'PATCH',
    'POST',
    'PUT',
    'VIEW',
)

CORS_ALLOW_HEADERS = (
    'accept',
    'accept-encoding',
    'authorization',
    'content-type',
    'dnt',
    'origin',
    'user-agent',
    'x-csrftoken',
    'x-requested-with',
)


# 日志模块
LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'error_format': {
            # 'format': '{"asctime":"%(asctime)s","thread":"%(threadName)s:%(thread)d","errorline":"%(lineno)d","errorlevel":"%(levelname)s","errorcontent":"%(message)s"}'
            'format': '%(asctime)s %(threadName)s %(thread)d %(lineno)d %(levelname)s %(message)s'
        },
    },
    'filters': {
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'include_html': True,
        },
        'default': {
            'level': 'ERROR',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': BASE_DIR + '/static/log/error.log',
            'maxBytes': 1024 * 1024 * 5,  # 5 MB
            'backupCount': 5,
            'formatter': 'error_format',
        },
        'console': {
            # 'level': 'ERROR',
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'error_format'
        },
    },
    'loggers': {
        'django': {
            'handlers': ['default', 'console'],
            # 'handlers': ['mail_admins','default','console'],
            # 'level': 'ERROR',
            'level': 'DEBUG',
            'propagate': True
        },
        # 'django.db.backends': {
        #     'handlers': ['console'],
        #     'propagate': True,
        #     'level': 'DEBUG',
        # },
    }
}

