208 lines
No EOL
8 KiB
Python
208 lines
No EOL
8 KiB
Python
# ruff: noqa: E501
|
|
import logging
|
|
|
|
import sentry_sdk
|
|
from sentry_sdk.integrations.celery import CeleryIntegration
|
|
from sentry_sdk.integrations.django import DjangoIntegration
|
|
from sentry_sdk.integrations.logging import LoggingIntegration
|
|
from sentry_sdk.integrations.redis import RedisIntegration
|
|
|
|
from .base import * # noqa: F403
|
|
from .base import DATABASES
|
|
from .base import INSTALLED_APPS
|
|
from .base import REDIS_URL
|
|
from .base import SPECTACULAR_SETTINGS
|
|
from .base import env
|
|
|
|
# GENERAL
|
|
# ------------------------------------------------------------------------------
|
|
# https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
|
|
SECRET_KEY = env("DJANGO_SECRET_KEY")
|
|
# https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts
|
|
ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS", default=["learngpt.tech"])
|
|
|
|
# DATABASES
|
|
# ------------------------------------------------------------------------------
|
|
DATABASES["default"]["CONN_MAX_AGE"] = env.int("CONN_MAX_AGE", default=60)
|
|
|
|
# CACHES
|
|
# ------------------------------------------------------------------------------
|
|
CACHES = {
|
|
"default": {
|
|
"BACKEND": "django_redis.cache.RedisCache",
|
|
"LOCATION": REDIS_URL,
|
|
"OPTIONS": {
|
|
"CLIENT_CLASS": "django_redis.client.DefaultClient",
|
|
# Mimicking memcache behavior.
|
|
# https://github.com/jazzband/django-redis#memcached-exceptions-behavior
|
|
"IGNORE_EXCEPTIONS": True,
|
|
},
|
|
},
|
|
}
|
|
|
|
# SECURITY
|
|
# ------------------------------------------------------------------------------
|
|
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-proxy-ssl-header
|
|
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
|
|
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-ssl-redirect
|
|
SECURE_SSL_REDIRECT = env.bool("DJANGO_SECURE_SSL_REDIRECT", default=True)
|
|
# https://docs.djangoproject.com/en/dev/ref/settings/#session-cookie-secure
|
|
SESSION_COOKIE_SECURE = True
|
|
# https://docs.djangoproject.com/en/dev/ref/settings/#session-cookie-name
|
|
SESSION_COOKIE_NAME = "__Secure-sessionid"
|
|
# https://docs.djangoproject.com/en/dev/ref/settings/#csrf-cookie-secure
|
|
CSRF_COOKIE_SECURE = True
|
|
# https://docs.djangoproject.com/en/dev/ref/settings/#csrf-cookie-name
|
|
CSRF_COOKIE_NAME = "__Secure-csrftoken"
|
|
# https://docs.djangoproject.com/en/dev/topics/security/#ssl-https
|
|
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-hsts-seconds
|
|
# TODO: set this to 60 seconds first and then to 518400 once you prove the former works
|
|
SECURE_HSTS_SECONDS = 60
|
|
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-hsts-include-subdomains
|
|
SECURE_HSTS_INCLUDE_SUBDOMAINS = env.bool(
|
|
"DJANGO_SECURE_HSTS_INCLUDE_SUBDOMAINS",
|
|
default=True,
|
|
)
|
|
# https://docs.djangoproject.com/en/dev/ref/settings/#secure-hsts-preload
|
|
SECURE_HSTS_PRELOAD = env.bool("DJANGO_SECURE_HSTS_PRELOAD", default=True)
|
|
# https://docs.djangoproject.com/en/dev/ref/middleware/#x-content-type-options-nosniff
|
|
SECURE_CONTENT_TYPE_NOSNIFF = env.bool(
|
|
"DJANGO_SECURE_CONTENT_TYPE_NOSNIFF",
|
|
default=True,
|
|
)
|
|
|
|
# STATIC & MEDIA
|
|
# ------------------------
|
|
STORAGES = {
|
|
"default": {
|
|
"BACKEND": "django.core.files.storage.FileSystemStorage",
|
|
},
|
|
"staticfiles": {
|
|
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
|
|
},
|
|
}
|
|
|
|
# EMAIL
|
|
# ------------------------------------------------------------------------------
|
|
# https://docs.djangoproject.com/en/dev/ref/settings/#default-from-email
|
|
DEFAULT_FROM_EMAIL = env(
|
|
"DJANGO_DEFAULT_FROM_EMAIL",
|
|
default="Learning Management System <noreply@learngpt.tech>",
|
|
)
|
|
# https://docs.djangoproject.com/en/dev/ref/settings/#server-email
|
|
SERVER_EMAIL = env("DJANGO_SERVER_EMAIL", default=DEFAULT_FROM_EMAIL)
|
|
# https://docs.djangoproject.com/en/dev/ref/settings/#email-subject-prefix
|
|
EMAIL_SUBJECT_PREFIX = env(
|
|
"DJANGO_EMAIL_SUBJECT_PREFIX",
|
|
default="[Learning Management System] ",
|
|
)
|
|
ACCOUNT_EMAIL_SUBJECT_PREFIX = EMAIL_SUBJECT_PREFIX
|
|
|
|
# ADMIN
|
|
# ------------------------------------------------------------------------------
|
|
# Django Admin URL regex.
|
|
ADMIN_URL = env("DJANGO_ADMIN_URL")
|
|
|
|
# Anymail
|
|
# ------------------------------------------------------------------------------
|
|
# https://anymail.readthedocs.io/en/stable/installation/#installing-anymail
|
|
INSTALLED_APPS += ["anymail"]
|
|
# https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
|
|
# https://anymail.readthedocs.io/en/stable/installation/#anymail-settings-reference
|
|
# https://anymail.readthedocs.io/en/stable/esps/mailgun/
|
|
EMAIL_BACKEND = "anymail.backends.mailgun.EmailBackend"
|
|
ANYMAIL = {
|
|
"MAILGUN_API_KEY": env("MAILGUN_API_KEY"),
|
|
"MAILGUN_SENDER_DOMAIN": env("MAILGUN_DOMAIN"),
|
|
"MAILGUN_API_URL": env("MAILGUN_API_URL", default="https://api.mailgun.net/v3"),
|
|
}
|
|
|
|
# django-compressor
|
|
# ------------------------------------------------------------------------------
|
|
# https://django-compressor.readthedocs.io/en/latest/settings/#django.conf.settings.COMPRESS_ENABLED
|
|
COMPRESS_ENABLED = env.bool("COMPRESS_ENABLED", default=True)
|
|
# https://django-compressor.readthedocs.io/en/latest/settings/#django.conf.settings.COMPRESS_STORAGE
|
|
COMPRESS_STORAGE = "compressor.storage.GzipCompressorFileStorage"
|
|
# https://django-compressor.readthedocs.io/en/latest/settings/#django.conf.settings.COMPRESS_URL
|
|
COMPRESS_URL = STATIC_URL # noqa: F405
|
|
# https://django-compressor.readthedocs.io/en/latest/settings/#django.conf.settings.COMPRESS_OFFLINE
|
|
COMPRESS_OFFLINE = True # Offline compression is required when using Whitenoise
|
|
# https://django-compressor.readthedocs.io/en/latest/settings/#django.conf.settings.COMPRESS_FILTERS
|
|
COMPRESS_FILTERS = {
|
|
"css": [
|
|
"compressor.filters.css_default.CssAbsoluteFilter",
|
|
"compressor.filters.cssmin.rCSSMinFilter",
|
|
],
|
|
"js": ["compressor.filters.jsmin.JSMinFilter"],
|
|
}
|
|
|
|
# LOGGING
|
|
# ------------------------------------------------------------------------------
|
|
# https://docs.djangoproject.com/en/dev/ref/settings/#logging
|
|
# See https://docs.djangoproject.com/en/dev/topics/logging for
|
|
# more details on how to customize your logging configuration.
|
|
|
|
LOGGING = {
|
|
"version": 1,
|
|
"disable_existing_loggers": True,
|
|
"formatters": {
|
|
"verbose": {
|
|
"format": "%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s",
|
|
},
|
|
},
|
|
"handlers": {
|
|
"console": {
|
|
"level": "DEBUG",
|
|
"class": "logging.StreamHandler",
|
|
"formatter": "verbose",
|
|
},
|
|
},
|
|
"root": {"level": "INFO", "handlers": ["console"]},
|
|
"loggers": {
|
|
"django.db.backends": {
|
|
"level": "ERROR",
|
|
"handlers": ["console"],
|
|
"propagate": False,
|
|
},
|
|
# Errors logged by the SDK itself
|
|
"sentry_sdk": {"level": "ERROR", "handlers": ["console"], "propagate": False},
|
|
"django.security.DisallowedHost": {
|
|
"level": "ERROR",
|
|
"handlers": ["console"],
|
|
"propagate": False,
|
|
},
|
|
},
|
|
}
|
|
|
|
# Sentry
|
|
# ------------------------------------------------------------------------------
|
|
SENTRY_DSN = env("SENTRY_DSN")
|
|
SENTRY_LOG_LEVEL = env.int("DJANGO_SENTRY_LOG_LEVEL", logging.INFO)
|
|
|
|
sentry_logging = LoggingIntegration(
|
|
level=SENTRY_LOG_LEVEL, # Capture info and above as breadcrumbs
|
|
event_level=logging.ERROR, # Send errors as events
|
|
)
|
|
integrations = [
|
|
sentry_logging,
|
|
DjangoIntegration(),
|
|
CeleryIntegration(),
|
|
RedisIntegration(),
|
|
]
|
|
sentry_sdk.init(
|
|
dsn=SENTRY_DSN,
|
|
integrations=integrations,
|
|
environment=env("SENTRY_ENVIRONMENT", default="production"),
|
|
traces_sample_rate=env.float("SENTRY_TRACES_SAMPLE_RATE", default=0.0),
|
|
)
|
|
|
|
# django-rest-framework
|
|
# -------------------------------------------------------------------------------
|
|
# Tools that generate code samples can use SERVERS to point to the correct domain
|
|
SPECTACULAR_SETTINGS["SERVERS"] = [
|
|
{"url": "https://learngpt.tech", "description": "Production server"},
|
|
]
|
|
# Your stuff...
|
|
# ------------------------------------------------------------------------------
|
|
|
|
SIMPLE_JWT["SIGNING_KEY"]=SECRET_KEY |