Tenhal/tenhal/settings.py
2025-12-29 14:08:24 +03:00

210 lines
5.6 KiB
Python

"""
Django settings for tenhal project.
Generated by 'django-admin startproject' using Django 6.0.
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/
"""
from pathlib import Path
import environ
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
env = environ.Env(
# set casting, default value
DEBUG=(bool, False)
)
environ.Env.read_env(os.path.join(BASE_DIR, '.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 = env('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env('DEBUG')
# ALLOWED_HOSTS = ['*']
ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', default=[])
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rangefilter',
'landing_page',
'django_extensions',
'django_q',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'landing_page.middleware.AnalyticsMiddleware', # Custom Analytics Middleware
]
ROOT_URLCONF = 'tenhal.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'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 = 'tenhal.wsgi.application'
# Database
# https://docs.djangoproject.com/en/6.0/ref/settings/#databases
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
# }
# settings.py
# 1. Database Optimization (Django 5.1+)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
'OPTIONS': {
'timeout': 20, # Wait 20s for locks
'transaction_mode': 'IMMEDIATE', # Prevents deadlocks
'init_command': (
'PRAGMA journal_mode=WAL;' # Read while writing
'PRAGMA synchronous=NORMAL;' # Fast writes
'PRAGMA mmap_size=134217728;' # 128MB Memory mapping for speed
'PRAGMA cache_size=2000;' # Increase page cache
),
},
}
}
# 2. Django-Q2 Settings
Q_CLUSTER = {
'name': 'LandingPageQueue',
'workers': 2, # Keep workers low for SQLite (1-2 is best)
'recycle': 500, # Restart workers occasionally to free memory
'timeout': 60, # Kill task if it hangs
'retry': 120, # Retry task after 2 mins if it failed
'orm': 'default', # USE SQLITE AS THE QUEUE
'save_limit': 100, # Only keep last 100 successful tasks (Prevents DB bloat)
'ack_failures': True, # Remove failed tasks from queue after retry
'poll': 0.5, # How often to check for new tasks (seconds)
}
# 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/Riyadh'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/6.0/howto/static-files/
STATIC_URL = 'static/'
STATICFILES_DIRS = [
BASE_DIR / 'static',
]
# The absolute path to the directory where collectstatic will gather files for deployment
STATIC_ROOT = BASE_DIR / 'staticfiles'
MEDIA_URL = '/media/' # The URL prefix for user-uploaded media files
MEDIA_ROOT = BASE_DIR / 'media'
# 3. Define Languages (English and Arabic)
LANGUAGE_CODE = 'en' # Default language
LANGUAGES = [
('en', 'English'),
('ar', 'Arabic'),
]
# 4. Use cookies to remember language choice
LANGUAGE_COOKIE_NAME = 'django_language'
LOCALE_PATHS = [
BASE_DIR / 'locale'
]
# Path to the directory containing GeoLite2-City.mmdb and GeoLite2-Country.mmdb
GEOIP_PATH = BASE_DIR / 'geoip'
if not DEBUG:
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
else:
SECURE_SSL_REDIRECT = False
SESSION_COOKIE_SECURE = False
CSRF_COOKIE_SECURE = False
CSRF_TRUSTED_ORIGINS = [f"https://{host}" for host in ALLOWED_HOSTS if "ngrok-free.app" in host]