- Redis / Celery / Celery Beat - кэширование популярных тегов и лучших участников - асинхронные уведомления в Centrifugo - отправка email-уведомлений через Celery - полнотекстовый поиск и подсказки - Docker Compose инфраструктура
196 lines
5.6 KiB
Python
196 lines
5.6 KiB
Python
"""
|
|
Django settings for application project.
|
|
|
|
Generated by 'django-admin startproject' using Django 5.2.12.
|
|
|
|
For more information on this file, see
|
|
https://docs.djangoproject.com/en/5.2/topics/settings/
|
|
|
|
For the full list of settings and their values, see
|
|
https://docs.djangoproject.com/en/5.2/ref/settings/
|
|
"""
|
|
import os
|
|
from pathlib import Path
|
|
from dotenv import load_dotenv
|
|
|
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
|
|
|
load_dotenv()
|
|
# Quick-start development settings - unsuitable for production
|
|
# See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/
|
|
|
|
# SECURITY WARNING: keep the secret key used in production secret!
|
|
SECRET_KEY = os.getenv("SECRET_KEY")
|
|
|
|
# SECURITY WARNING: don't run with debug turned on in production!
|
|
DEBUG = os.getenv("DEBUG") == "True"
|
|
|
|
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
|
|
|
|
# Allow requests coming through nginx on host:8080 (used in local Docker Compose)
|
|
# Add any additional origins you use (including scheme and port) here.
|
|
CSRF_TRUSTED_ORIGINS = [
|
|
'http://127.0.0.1:8080',
|
|
'http://localhost:8080',
|
|
]
|
|
|
|
|
|
# Application definition
|
|
|
|
INSTALLED_APPS = [
|
|
'django.contrib.admin',
|
|
'django.contrib.auth',
|
|
'django.contrib.contenttypes',
|
|
'django.contrib.sessions',
|
|
'django.contrib.messages',
|
|
'django.contrib.staticfiles',
|
|
'django.contrib.postgres',
|
|
'questions',
|
|
]
|
|
|
|
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',
|
|
]
|
|
|
|
ROOT_URLCONF = 'application.urls'
|
|
|
|
STATICFILES_DIRS = [
|
|
BASE_DIR / 'questions' / 'static',
|
|
]
|
|
|
|
|
|
TEMPLATES = [
|
|
{
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
'DIRS': [BASE_DIR / 'questions' / 'templates'],
|
|
'APP_DIRS': True,
|
|
'OPTIONS': {
|
|
'context_processors': [
|
|
'django.template.context_processors.request',
|
|
'django.contrib.auth.context_processors.auth',
|
|
'django.contrib.messages.context_processors.messages',
|
|
'questions.context_processors.sidebar_data',
|
|
],
|
|
},
|
|
},
|
|
]
|
|
|
|
WSGI_APPLICATION = 'application.wsgi.application'
|
|
|
|
|
|
# Database
|
|
# https://docs.djangoproject.com/en/5.2/ref/settings/#databases
|
|
|
|
DATABASES = {
|
|
'default': {
|
|
'ENGINE': os.getenv('DB_ENGINE'),
|
|
'NAME': os.getenv('DB_NAME'),
|
|
'USER': os.getenv('DB_USER'),
|
|
'PASSWORD': os.getenv('DB_PASSWORD'),
|
|
'HOST': os.getenv('DB_HOST'),
|
|
'PORT': os.getenv('DB_PORT'),
|
|
}
|
|
}
|
|
|
|
|
|
# Password validation
|
|
# https://docs.djangoproject.com/en/5.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/5.2/topics/i18n/
|
|
|
|
LANGUAGE_CODE = 'ru-RU'
|
|
|
|
TIME_ZONE = 'UTC'
|
|
|
|
USE_I18N = True
|
|
|
|
USE_TZ = True
|
|
|
|
|
|
# Static files (CSS, JavaScript, Images)
|
|
# https://docs.djangoproject.com/en/5.2/howto/static-files/
|
|
|
|
STATIC_URL = '/static/'
|
|
|
|
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
|
|
|
|
# Default primary key field type
|
|
# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
|
|
|
|
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
|
|
|
|
|
MEDIA_URL = '/media/'
|
|
MEDIA_ROOT = BASE_DIR / 'media'
|
|
|
|
# Redis / Celery / Cache settings
|
|
REDIS_HOST = os.getenv('REDIS_HOST', 'redis')
|
|
REDIS_PORT = os.getenv('REDIS_PORT', '6379')
|
|
REDIS_CACHE_DB = os.getenv('REDIS_CACHE_DB', '1')
|
|
REDIS_BROKER_DB = os.getenv('REDIS_BROKER_DB', '2')
|
|
REDIS_BEAT_DB = os.getenv('REDIS_BEAT_DB', '3')
|
|
|
|
CACHES = {
|
|
"default": {
|
|
"BACKEND": "django_redis.cache.RedisCache",
|
|
"LOCATION": f"redis://{REDIS_HOST}:{REDIS_PORT}/{REDIS_CACHE_DB}",
|
|
"OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"},
|
|
"TIMEOUT": 60 * 10,
|
|
}
|
|
}
|
|
|
|
# Celery settings
|
|
CELERY_BROKER_URL = f"redis://{REDIS_HOST}:{REDIS_PORT}/{REDIS_BROKER_DB}"
|
|
CELERY_RESULT_BACKEND = f"redis://{REDIS_HOST}:{REDIS_PORT}/{REDIS_BEAT_DB}"
|
|
CELERY_BEAT_SCHEDULER = "redbeat.RedBeatScheduler"
|
|
CELERY_REDBEAT_REDIS_URL = f"redis://{REDIS_HOST}:{REDIS_PORT}/{REDIS_BEAT_DB}"
|
|
|
|
# Centrifugo (real-time) settings
|
|
CENTRIFUGO_URL = os.getenv('CENTRIFUGO_URL', 'http://centrifugo:8000')
|
|
CENTRIFUGO_API_KEY = os.getenv('CENTRIFUGO_API_KEY', 'centrifugo_secret')
|
|
|
|
# Email settings (use maildev for local development)
|
|
EMAIL_BACKEND = os.getenv('EMAIL_BACKEND', 'django.core.mail.backends.smtp.EmailBackend')
|
|
EMAIL_HOST = os.getenv('EMAIL_HOST', 'maildev')
|
|
EMAIL_PORT = int(os.getenv('EMAIL_PORT', '1025'))
|
|
EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS', 'False') == 'True'
|
|
DEFAULT_FROM_EMAIL = os.getenv('DEFAULT_FROM_EMAIL', 'webmaster@localhost')
|
|
|
|
# Celery beat schedule (periodic tasks)
|
|
from celery.schedules import crontab
|
|
|
|
CELERY_BEAT_SCHEDULE = {
|
|
'rebuild_popular_tags': {
|
|
'task': 'questions.tasks.rebuild_popular_tags',
|
|
'schedule': crontab(minute='*/30'),
|
|
},
|
|
'rebuild_best_members': {
|
|
'task': 'questions.tasks.rebuild_best_members',
|
|
'schedule': crontab(minute='*/15'),
|
|
},
|
|
}
|