You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
274 lines
8.0 KiB
274 lines
8.0 KiB
"""
|
|
Django settings for config project.
|
|
|
|
Generated by 'django-admin startproject' using Django 6.0.8.
|
|
|
|
For more information on this file, see
|
|
https://docs.djangoproject.com/en/6.0/topics/settings/
|
|
|
|
For the full list of settings and their values, see
|
|
https://docs.djangoproject.com/en/6.0/ref/settings/
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
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 environment variables from the project root
|
|
load_dotenv(BASE_DIR.parent / '.env')
|
|
|
|
# Quick-start development settings - unsuitable for production
|
|
# See https://docs.djangoproject.com/en/6.0/howto/deployment/checklist/
|
|
|
|
# SECURITY WARNING: keep the secret key used in production secret!
|
|
SECRET_KEY = os.getenv('DJANGO_SECRET_KEY', 'django-insecure-@8*97xo5!gdi!x*94-%n8$b63%ljy#@&^#7$h!9v-4a2#r+98t')
|
|
|
|
# Environment setup (development / production)
|
|
# Reads DEV=True for development, DEV=False for production
|
|
IS_DEV = os.getenv('DEV', 'True').lower() in ('true', '1', 't')
|
|
|
|
if IS_DEV:
|
|
DEBUG = True
|
|
ALLOWED_HOSTS = ['*']
|
|
CORS_ALLOW_ALL_ORIGINS = True
|
|
CSRF_TRUSTED_ORIGINS = [
|
|
'http://localhost:5173', 'http://localhost:8000', 'http://127.0.0.1:8000', 'http://localhost', 'http://127.0.0.1',
|
|
'https://divar.nwhco.ir', 'http://divar.nwhco.ir'
|
|
]
|
|
else:
|
|
DEBUG = os.getenv('DJANGO_DEBUG', 'False').lower() in ('true', '1', 't')
|
|
allowed_hosts_raw = os.getenv('ALLOWED_HOSTS', '*')
|
|
ALLOWED_HOSTS = [host.strip() for host in allowed_hosts_raw.split(',') if host.strip()]
|
|
if '*' not in ALLOWED_HOSTS:
|
|
ALLOWED_HOSTS.append('*')
|
|
|
|
CORS_ALLOW_ALL_ORIGINS = True
|
|
csrf_origins = os.getenv('CSRF_TRUSTED_ORIGINS', 'https://divar.nwhco.ir,http://divar.nwhco.ir,http://localhost,http://127.0.0.1')
|
|
CSRF_TRUSTED_ORIGINS = [origin.strip() for origin in csrf_origins.split(',') if origin.strip()]
|
|
|
|
# Reverse Proxy SSL / Host headers configuration (for Nginx / Docker reverse proxies)
|
|
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
|
|
USE_X_FORWARDED_HOST = True
|
|
USE_X_FORWARDED_PORT = True
|
|
|
|
|
|
|
|
# Application definition
|
|
|
|
INSTALLED_APPS = [
|
|
'django.contrib.admin',
|
|
'django.contrib.auth',
|
|
'django.contrib.contenttypes',
|
|
'django.contrib.sessions',
|
|
'django.contrib.messages',
|
|
'django.contrib.staticfiles',
|
|
|
|
# Third party apps
|
|
'rest_framework',
|
|
'corsheaders',
|
|
'django_celery_beat',
|
|
|
|
# Local apps
|
|
'core',
|
|
'crawler',
|
|
'ads',
|
|
]
|
|
|
|
MIDDLEWARE = [
|
|
'corsheaders.middleware.CorsMiddleware',
|
|
'django.middleware.security.SecurityMiddleware',
|
|
'whitenoise.middleware.WhiteNoiseMiddleware',
|
|
'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 = 'config.urls'
|
|
|
|
TEMPLATES = [
|
|
{
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
'DIRS': [BASE_DIR.parent / 'frontend' / 'dist'],
|
|
'APP_DIRS': True,
|
|
'OPTIONS': {
|
|
'context_processors': [
|
|
'django.template.context_processors.request',
|
|
'django.contrib.auth.context_processors.auth',
|
|
'django.contrib.messages.context_processors.messages',
|
|
],
|
|
},
|
|
},
|
|
]
|
|
|
|
WSGI_APPLICATION = 'config.wsgi.application'
|
|
|
|
|
|
# Database
|
|
# https://docs.djangoproject.com/en/6.0/ref/settings/#databases
|
|
|
|
POSTGRES_DB = os.getenv('POSTGRES_DB')
|
|
TESTING = 'test' in sys.argv or os.getenv('TESTING', 'False').lower() in ('true', '1', 't')
|
|
|
|
if POSTGRES_DB and not TESTING:
|
|
DATABASES = {
|
|
'default': {
|
|
'ENGINE': 'django.db.backends.postgresql',
|
|
'NAME': POSTGRES_DB,
|
|
'USER': os.getenv('POSTGRES_USER', 'postgres'),
|
|
'PASSWORD': os.getenv('POSTGRES_PASSWORD', 'postgres'),
|
|
'HOST': os.getenv('POSTGRES_HOST', 'localhost'),
|
|
'PORT': os.getenv('POSTGRES_PORT', '5432'),
|
|
}
|
|
}
|
|
else:
|
|
DATABASES = {
|
|
'default': {
|
|
'ENGINE': 'django.db.backends.sqlite3',
|
|
'NAME': BASE_DIR / 'db.sqlite3',
|
|
}
|
|
}
|
|
|
|
|
|
|
|
# Password validation
|
|
# https://docs.djangoproject.com/en/6.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/6.0/topics/i18n/
|
|
|
|
LANGUAGE_CODE = 'en-us'
|
|
|
|
TIME_ZONE = 'Asia/Tehran'
|
|
|
|
USE_I18N = True
|
|
|
|
USE_TZ = True
|
|
|
|
|
|
# Static files (CSS, JavaScript, Images)
|
|
# https://docs.djangoproject.com/en/6.0/howto/static-files/
|
|
|
|
STATIC_URL = 'static/'
|
|
STATIC_ROOT = BASE_DIR / 'staticfiles'
|
|
STATICFILES_DIRS = [
|
|
BASE_DIR.parent / 'frontend' / 'dist',
|
|
] if (BASE_DIR.parent / 'frontend' / 'dist').exists() else []
|
|
|
|
STORAGES = {
|
|
"default": {
|
|
"BACKEND": "django.db.models.fields.files.Storage",
|
|
},
|
|
"staticfiles": {
|
|
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
|
|
},
|
|
}
|
|
|
|
# Default primary key field type
|
|
# https://docs.djangoproject.com/en/6.0/ref/settings/#default-auto-field
|
|
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
|
|
|
# CORS & CSRF Settings
|
|
CORS_ALLOW_ALL_ORIGINS = os.getenv('CORS_ALLOW_ALL_ORIGINS', 'True').lower() in ('true', '1', 't')
|
|
if not CORS_ALLOW_ALL_ORIGINS:
|
|
CORS_ALLOWED_ORIGINS = [origin.strip() for origin in os.getenv('CORS_ALLOWED_ORIGINS', '').split(',') if origin.strip()]
|
|
|
|
csrf_origins = os.getenv('CSRF_TRUSTED_ORIGINS', 'http://localhost,http://127.0.0.1,http://localhost:5173,http://localhost:8000')
|
|
CSRF_TRUSTED_ORIGINS = [origin.strip() for origin in csrf_origins.split(',') if origin.strip()]
|
|
|
|
# Django REST Framework Settings
|
|
REST_FRAMEWORK = {
|
|
'DEFAULT_PERMISSION_CLASSES': [
|
|
'rest_framework.permissions.AllowAny',
|
|
],
|
|
'DEFAULT_RENDERER_CLASSES': [
|
|
'rest_framework.renderers.JSONRenderer',
|
|
'rest_framework.renderers.BrowsableAPIRenderer',
|
|
],
|
|
}
|
|
|
|
# Celery Configuration Options
|
|
CELERY_BROKER_URL = os.getenv('REDIS_URL', 'redis://localhost:6379/0')
|
|
CELERY_RESULT_BACKEND = os.getenv('REDIS_URL', 'redis://localhost:6379/0')
|
|
CELERY_ACCEPT_CONTENT = ['json']
|
|
CELERY_TASK_SERIALIZER = 'json'
|
|
CELERY_RESULT_SERIALIZER = 'json'
|
|
CELERY_TIMEZONE = 'UTC'
|
|
|
|
# Celery Beat Schedule
|
|
CELERY_BEAT_SCHEDULE = {
|
|
'check-and-schedule-crawl-tasks-every-minute': {
|
|
'task': 'crawler.tasks.check_and_schedule_crawl_tasks',
|
|
'schedule': 60.0,
|
|
},
|
|
}
|
|
|
|
# Standard Logging System Configuration
|
|
LOG_LEVEL = os.getenv('LOG_LEVEL', 'DEBUG' if IS_DEV else 'INFO')
|
|
|
|
LOGGING = {
|
|
'version': 1,
|
|
'disable_existing_loggers': False,
|
|
'formatters': {
|
|
'verbose': {
|
|
'format': '[{asctime}] [{levelname}] [{name}:{lineno}] {message}',
|
|
'style': '{',
|
|
'datefmt': '%Y-%m-%d %H:%M:%S',
|
|
},
|
|
'simple': {
|
|
'format': '{levelname} {message}',
|
|
'style': '{',
|
|
},
|
|
},
|
|
'handlers': {
|
|
'console': {
|
|
'class': 'logging.StreamHandler',
|
|
'formatter': 'verbose',
|
|
},
|
|
},
|
|
'root': {
|
|
'handlers': ['console'],
|
|
'level': LOG_LEVEL,
|
|
},
|
|
'loggers': {
|
|
'django': {
|
|
'handlers': ['console'],
|
|
'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
|
|
'propagate': False,
|
|
},
|
|
'crawler': {
|
|
'handlers': ['console'],
|
|
'level': LOG_LEVEL,
|
|
'propagate': False,
|
|
},
|
|
'ads': {
|
|
'handlers': ['console'],
|
|
'level': LOG_LEVEL,
|
|
'propagate': False,
|
|
},
|
|
},
|
|
}
|
|
|
|
|