""" Django settings for university project. Generated by 'django-admin startproject' using Django 2.1.2. For more information on this file, see https://docs.djangoproject.com/en/2.1/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/2.1/ref/settings/ """ import os import logging import sentry_sdk from sentry_sdk.integrations.django import DjangoIntegration from university.utils.log_filters import skip_illegal_level, get_level # 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.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '5k0%05vx4pgqx_$byoi2l(uyr2bp+(noy1g_8nt07pr)g_r*x5' # SECURITY WARNING: don't run with debug turned on in production! 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', 'graphene_django', 'university', 'pingcap_cookie_sso', 'aliyun_video', 'channels', ] INSTALLED_LECTURES = { 'pdf': 'university.lectures.pdf', 'markdown': 'university.lectures.markdown', 'offline': 'university.lectures.offline', 'video': 'aliyun_video' } MIDDLEWARE = [ 'university.middlewares.cookie.SameSiteNoneMiddleware', 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.locale.LocaleMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'university.middlewares.auth.AuthenticationMiddleware', 'pingcap_cookie_sso.middleware.SsoMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'university.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], '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', ], }, }, { 'BACKEND': 'django.template.backends.jinja2.Jinja2', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'environment': 'university.utils.jinja2.environment', 'extensions': [ 'webpack_loader.contrib.jinja2ext.WebpackExtension', 'jinja2.ext.i18n', 'jinja2.ext.with_' ], '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 = 'university.wsgi.application' ASGI_APPLICATION = 'university.routing.application' CHANNEL_LAYERS = { 'default': { 'BACKEND': 'channels_redis.core.RedisChannelLayer', 'CONFIG': { "hosts": [('127.0.0.1', 6379)], }, }, } # Database # https://docs.djangoproject.com/en/2.1/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.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', }, ] AUTH_USER_MODEL = 'university.User' SSO_USER_BEHAVIOR_TRACK_ENDPOINT = '' SSO_API_TOKEN = '' # Internationalization # https://docs.djangoproject.com/en/2.1/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = True LOCALE_PATHS = ( os.path.join(BASE_DIR, 'university/locale'), ) LANGUAGES = { ('zh-hans', '中文简体'), ('en-us', 'English'), } REST_FRAMEWORK = { 'UNAUTHENTICATED_USER': 'university.models.UniversityAnonymousUser', 'DEFAULT_RENDERER_CLASSES': ( 'rest_framework.renderers.JSONRenderer', ), 'DEFAULT_PERMISSION_CLASSES': ( 'university.api.permissions.DenyAllPermission', ), 'DEFAULT_AUTHENTICATION_CLASSES': ( # 'university.api.authentication.APITokenAuthentication', 'university.api.authentication.SessionAuthentication', ), 'TEST_REQUEST_DEFAULT_FORMAT': 'json', 'DEFAULT_THROTTLE_CLASSES': ( 'rest_framework.throttling.AnonRateThrottle', 'rest_framework.throttling.UserRateThrottle' ), 'DEFAULT_THROTTLE_RATES': { 'anon': '60/m', 'user': '100/m' } } AUTHENTICATION_BACKENDS = ['pingcap_cookie_sso.authentication.SsoAuthBackend'] # Do not check 'django.contrib.auth.middleware.AuthenticationMiddleware' SILENCED_SYSTEM_CHECKS = ['admin.E408'] GRAPHENE = { 'SCHEMA': 'university.schema.schema' } # Celery Config CELERY_BROKER_URL = 'redis://127.0.0.1:6379/2' CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/2' CELERY_TASK_REJECT_ON_WORKER_LOST = True CELERY_TASK_ACKS_LATE = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/2.1/howto/static-files/ BASE_URL = 'http://127.0.0.1' STATIC_URL = '/_static/' STATIC_ROOT = 'static_collected' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'university/admin/frontend/dist/'), ] MEDIA_PRIVATE_URL = '/_private_media_url/' # Logging config LOG_DIRECTORY = '/var/log/pingcap_university/' LOG_INFO = 'info.log' LOG_WARNING = 'warning.log' LOG_ERROR = 'error.log' LOG_CRITICAL = 'critical.log' SENTRY_DSN = 'https://9ae349b9868b431a8e7b16643e348d10@sentry.io/1860566' LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'verbose': { 'format': '[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s', 'datefmt': '%Y-%m-%d %H:%M:%S' }, }, 'handlers': { 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'verbose', } }, 'filters': { 'skip_illegal_info_level': { '()': 'django.utils.log.CallbackFilter', 'callback': skip_illegal_level(logging.DEBUG, logging.WARNING), }, 'info_level': { '()': 'django.utils.log.CallbackFilter', 'callback': get_level(logging.INFO), }, 'warning_level': { '()': 'django.utils.log.CallbackFilter', 'callback': get_level(logging.WARNING), }, 'error_level': { '()': 'django.utils.log.CallbackFilter', 'callback': get_level(logging.ERROR), }, 'critical_level': { '()': 'django.utils.log.CallbackFilter', 'callback': get_level(logging.CRITICAL), }, }, 'loggers': { '': { 'level': 'DEBUG', 'handlers': ['console'], }, } } try: from .local_settings import * # noqa except ImportError: pass WEBPACK_LOADER = { 'DEFAULT': { 'CACHE': not DEBUG, 'BUNDLE_DIR_NAME': '', # must end with slash 'STATS_FILE': os.path.join(BASE_DIR, 'university/admin/frontend/webpack-stats.json'), 'POLL_INTERVAL': 0.1, 'TIMEOUT': None, 'IGNORE': [r'.+\.hot-update.js', r'.+\.map'] } } if not DEBUG: sentry_sdk.init( dsn=SENTRY_DSN, integrations=[DjangoIntegration()] ) LOGGING['handlers'].update({ 'file_info': { 'level': 'INFO', 'class': 'logging.handlers.RotatingFileHandler', 'filename': LOG_DIRECTORY + LOG_INFO, 'formatter': 'verbose', 'filters': ['info_level'], 'maxBytes': 1024 * 1024 * 500, 'backupCount': 10 }, 'file_warning': { 'level': 'WARNING', 'class': 'logging.handlers.RotatingFileHandler', 'filename': LOG_DIRECTORY + LOG_WARNING, 'formatter': 'verbose', 'filters': ['warning_level'], 'maxBytes': 1024 * 1024 * 500, 'backupCount': 10 }, 'file_error': { 'level': 'ERROR', 'class': 'logging.handlers.RotatingFileHandler', 'filename': LOG_DIRECTORY + LOG_ERROR, 'formatter': 'verbose', 'filters': ['error_level'], 'maxBytes': 1024 * 1024 * 500, 'backupCount': 10 }, 'file_critical': { 'level': 'CRITICAL', 'class': 'logging.handlers.RotatingFileHandler', 'filename': LOG_DIRECTORY + LOG_CRITICAL, 'formatter': 'verbose', 'filters': ['critical_level'], 'maxBytes': 1024 * 1024 * 500, } }) LOGGING['loggers']['']['handlers'] += [ 'file_info', 'file_warning', 'file_error', 'file_critical']