DRF_Template/backend/config/settings.py

387 lines
11 KiB
Python
Raw Normal View History

2024-01-06 12:13:39 +08:00
"""
Django settings for test backend project.
Generated by 'django-admin startproject' using Django 4.2.6.
For more information on this file, see
https://docs.djangoproject.com/en/4.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.2/ref/settings/
"""
from datetime import timedelta
from pathlib import Path
from dotenv import load_dotenv, find_dotenv # Python dotenv
2024-01-06 12:13:39 +08:00
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
# Backend folder (/backend)
2024-01-06 12:13:39 +08:00
BASE_DIR = Path(__file__).resolve().parent.parent
# Root folder where docker-compose.yml is located
ROOT_DIR = Path(__file__).resolve().parent.parent.parent
# If you're hosting this on the cloud, have this set
CLOUD = bool(os.getenv('CLOUD', False))
load_dotenv(find_dotenv())
def get_secret(secret_name):
if CLOUD:
try:
pass
# Add specific implementations here if deploying to Azure, GCP, or AWS to get secrets
except:
secret_value = ""
else:
# Fallback to .env or system environment variables for local development
secret_value = os.getenv(secret_name)
if secret_value is None:
raise ValueError(f"Secret '{secret_name}' not found.")
else:
return secret_value
# Frontend Domain
DOMAIN = get_secret('DOMAIN')
# Backend Domain
BACKEND_DOMAIN = get_secret('BACKEND_DOMAIN')
# URL Prefixes
USE_HTTPS = (get_secret('USE_HTTPS') == 'True')
URL_PREFIX = 'https://' if CLOUD and USE_HTTPS else 'http://'
BACKEND_URL = f'{URL_PREFIX}{BACKEND_DOMAIN}'
FRONTEND_URL = f'{URL_PREFIX}{DOMAIN}'
2024-01-06 12:13:39 +08:00
ALLOWED_HOSTS = ['*']
CSRF_TRUSTED_ORIGINS = [
BACKEND_URL,
FRONTEND_URL
]
if CLOUD:
# TODO: If you require additional URLs to be trusted in cloud service providers, add them here
CSRF_TRUSTED_ORIGINS += []
2024-01-06 12:13:39 +08:00
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = (get_secret('BACKEND_DEBUG') == 'True')
# Determines whether or not to insert test data within tables
SEED_DATA = (get_secret('SEED_DATA') == 'True')
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = get_secret('SECRET_KEY')
# Selenium Config
# Initiate CAPTCHA solver in test mode
CAPTCHA_TESTING = (get_secret('CAPTCHA_TESTING') == 'True')
# If using Selenium and/or the provided CAPTCHA solver, determines whether or not to use proxies
USE_PROXY = (get_secret('USE_PROXY') == 'True')
# Stripe (For payments)
STRIPE_SECRET_KEY = get_secret(
"STRIPE_SECRET_KEY")
STRIPE_SECRET_WEBHOOK = get_secret('STRIPE_SECRET_WEBHOOK')
STRIPE_CHECKOUT_URL = f''
2024-01-06 12:13:39 +08:00
# Email credentials
EMAIL_HOST = get_secret('EMAIL_HOST')
EMAIL_HOST_USER = get_secret('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = get_secret('EMAIL_HOST_PASSWORD')
EMAIL_PORT = get_secret('EMAIL_PORT')
EMAIL_USE_TLS = (get_secret('EMAIL_USE_TLS') == 'True')
EMAIL_ADDRESS = (get_secret('EMAIL_ADDRESS') == 'True')
2024-01-06 12:13:39 +08:00
# Application definition
INSTALLED_APPS = [
'silk',
'config',
2024-01-06 12:13:39 +08:00
'unfold',
'unfold.contrib.filters',
2024-01-06 12:13:39 +08:00
'unfold.contrib.simple_history',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'storages',
'django_extensions',
2024-01-06 12:13:39 +08:00
'rest_framework',
'rest_framework_simplejwt',
'django_celery_results',
'django_celery_beat',
2024-01-06 12:13:39 +08:00
'simple_history',
'djoser',
'corsheaders',
'drf_spectacular',
'drf_spectacular_sidecar',
'webdriver',
2024-01-06 12:13:39 +08:00
'accounts',
'user_groups',
'subscriptions',
'payments',
'billing',
'emails',
'notifications'
2024-01-06 12:13:39 +08:00
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
"whitenoise.middleware.WhiteNoiseMiddleware",
"silk.middleware.SilkyMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
2024-01-06 12:13:39 +08:00
"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',
]
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/
ROOT_URLCONF = 'config.urls'
if CLOUD:
# Cloud Storage Settings
CLOUD_BUCKET = get_secret('CLOUD_BUCKET')
CLOUD_BUCKET_CONTAINER = get_secret('CLOUD_BUCKET_CONTAINER')
CLOUD_STATIC_CONTAINER = get_secret('CLOUD_STATIC_CONTAINER')
MEDIA_URL = f'https://{CLOUD_BUCKET}/{CLOUD_BUCKET_CONTAINER}/'
MEDIA_ROOT = f'https://{CLOUD_BUCKET}/'
STATIC_URL = f'https://{CLOUD_BUCKET}/{CLOUD_STATIC_CONTAINER}/'
STATIC_ROOT = f'https://{CLOUD_BUCKET}/{CLOUD_STATIC_CONTAINER}/'
# Consult django-storages documentation when filling in these values. This will vary depending on your cloud service provider
STORAGES = {
'default': {
# TODO: Set this up here if you're using cloud storage
'BACKEND': None,
'OPTIONS': {
# Optional parameters
},
},
'staticfiles': {
# TODO: Set this up here if you're using cloud storage
'BACKEND': None,
'OPTIONS': {
# Optional parameters
},
},
}
else:
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
MEDIA_URL = 'api/v1/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
ROOT_URLCONF = 'config.urls'
2024-01-06 12:13:39 +08:00
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR / 'emails/templates/',
],
2024-01-06 12:13:39 +08:00
'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',
],
},
},
]
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
),
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
],
'DEFAULT_THROTTLE_RATES': {
'anon': '360/min',
'user': '1440/min'
},
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
}
# DRF-Spectacular
SPECTACULAR_SETTINGS = {
'TITLE': 'DRF-Template',
'DESCRIPTION': 'A Template Project by Keannu Bernasol',
2024-01-06 12:13:39 +08:00
'VERSION': '1.0.0',
'SERVE_INCLUDE_SCHEMA': False,
'SWAGGER_UI_DIST': 'SIDECAR',
'SWAGGER_UI_FAVICON_HREF': 'SIDECAR',
'REDOC_DIST': 'SIDECAR',
}
WSGI_APPLICATION = 'config.wsgi.application'
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
# Have this set to True if you're using a connection bouncer
'DISABLE_SERVER_SIDE_CURSORS': True,
"NAME": get_secret("DB_DATABASE"),
"USER": get_secret("DB_USERNAME"),
"PASSWORD": get_secret("DB_PASSWORD"),
"HOST": get_secret("DB_HOST"),
"PORT": get_secret("DB_PORT"),
"OPTIONS": {
"sslmode": get_secret("DB_SSL_MODE")
},
2024-01-06 12:13:39 +08:00
}
}
AUTH_USER_MODEL = 'accounts.CustomUser'
DJOSER = {
'SEND_ACTIVATION_EMAIL': True,
'SEND_CONFIRMATION_EMAIL': True,
'PASSWORD_RESET_CONFIRM_URL': 'reset_password_confirm/{uid}/{token}',
'ACTIVATION_URL': 'activation/{uid}/{token}',
'USER_AUTHENTICATION_RULES': ['djoser.authentication.TokenAuthenticationRule'],
'EMAIL': {
'activation': 'emails.templates.ActivationEmail',
'password_reset': 'emails.templates.PasswordResetEmail'
},
2024-01-06 12:13:39 +08:00
'SERIALIZERS': {
'user': 'accounts.serializers.CustomUserSerializer',
'current_user': 'accounts.serializers.CustomUserSerializer',
'user_create': 'accounts.serializers.UserRegistrationSerializer',
},
'PERMISSIONS': {
# Disable some unneeded endpoints by setting them to admin only
'username_reset': ['rest_framework.permissions.IsAdminUser'],
'username_reset_confirm': ['rest_framework.permissions.IsAdminUser'],
'set_username': ['rest_framework.permissions.IsAdminUser'],
'set_password': ['rest_framework.permissions.IsAdminUser'],
}
2024-01-06 12:13:39 +08:00
}
# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
"OPTIONS": {
"min_length": 8,
}
2024-01-06 12:13:39 +08:00
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
# Additional password validators
{
'NAME': 'accounts.validators.SpecialCharacterValidator',
},
{
'NAME': 'accounts.validators.LowercaseValidator',
},
{
'NAME': 'accounts.validators.UppercaseValidator',
},
{
'NAME': 'accounts.validators.NumberValidator',
},
2024-01-06 12:13:39 +08:00
]
# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Manila'
USE_I18N = True
USE_TZ = True
# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
SITE_NAME = 'DRF-Template'
2024-01-06 12:13:39 +08:00
# JWT Token Lifetimes
2024-01-06 12:13:39 +08:00
SIMPLE_JWT = {
"ACCESS_TOKEN_LIFETIME": timedelta(hours=6),
"REFRESH_TOKEN_LIFETIME": timedelta(days=3)
2024-01-06 12:13:39 +08:00
}
CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_CREDENTIALS = True
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"console": {
"class": "logging.StreamHandler",
},
},
"root": {
"handlers": ["console"],
"level": "DEBUG",
},
}
DJANGO_LOG_LEVEL = "DEBUG"
# Enables VS Code debugger to break on raised exceptions
DEBUG_PROPAGATE_EXCEPTIONS = "DEBUG"
# Celery Configuration Options
CELERY_TIMEZONE = TIME_ZONE
CELERY_TASK_TRACK_STARTED = True
CELERY_TASK_TIME_LIMIT = 30 * 60
CELERY_BROKER_URL = get_secret("CELERY_BROKER")
CELERY_BROKER = get_secret("CELERY_BROKER")
CELERY_BACKEND = get_secret("CELERY_BROKER")
CELERY_RESULT_BACKEND = get_secret("CELERY_RESULT_BACKEND")
CELERY_RESULT_EXTENDED = True
# Celery Beat Options
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
# Maximum number of rows that can be updated within the Django admin panel
DATA_UPLOAD_MAX_NUMBER_FIELDS = 20480
GRAPH_MODELS = {
'app_labels': ['accounts', 'user_groups', 'billing', 'emails', 'payments', 'subscriptions']
}
# Django/DRF Cache
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": f"redis://{get_secret('REDIS_HOST')}:{get_secret('REDIS_PORT')}/2",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}