Results-Page/core/settings.py

214 lines
5.7 KiB
Python

"""
Django settings for Results project.
Generated by 'django-admin startproject' using Django 4.1.5.
For more information on this file, see
https://docs.djangoproject.com/en/4.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""
import json
import environ
import logging
from pathlib import Path
from django.utils import timezone
log = logging.getLogger(__name__)
VERSION = "0.0.0"
# Build paths inside the project like this: BASE_DIR / "subdir".
BASE_DIR = Path(__file__).resolve().parent.parent
# Environment Variables
env = environ.Env(DEBUG=(bool, True))
environ.Env.read_env(BASE_DIR / ".env")
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env("RESULTS_SYSTEM__SECRET_KEY", default="unsecure-default-secret-key")
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env("RESULTS_SYSTEM__DEBUG").lower() == "true"
CSRF_TRUSTED_ORIGINS = [
"http://localhost",
"http://127.0.0.1",
"https://" + env("RESULTS_SYSTEM__HOST", default="127.0.0.1")
]
ALLOWED_HOSTS = [
host.replace("http://", "").replace("https://", "")
for host in CSRF_TRUSTED_ORIGINS
]
# Application definition
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django_extensions",
"rest_framework",
"compressor",
"apps.api",
"apps.home",
]
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 = "core.urls"
APPEND_SLASH = True
LOGIN_URL = "/login/"
LOGIN_REDIRECT_URL = "/"
LOGOUT_REDIRECT_URL = "/"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [BASE_DIR / "templates"],
"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",
],
},
},
]
WSGI_APPLICATION = "core.wsgi.application"
# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
DB_ENGINE = env("RESULTS_SYSTEM__DB_ENGINE", default=None)
if not DB_ENGINE:
db_data = {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "db.sqlite3"
}
else:
db_data = {
"ENGINE": env("DB_ENGINE"),
"NAME": env("DB_NAME"),
"USER": env("DB_USER"),
"PASSWORD": env("DB_PASS"),
"HOST": env("DB_HOST"),
"PORT": env("DB_PORT")
}
DATABASES = { "default": db_data }
# Password validation
# https://docs.djangoproject.com/en/4.1/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" },
]
# Logging
# https://docs.djangoproject.com/en/5.0/topics/logging/
LOGGING_DIR = BASE_DIR / "logs"
LOGGING_DIR.mkdir(exist_ok=True)
with open(LOGGING_DIR / "config.json", "r", encoding="utf-8") as file:
LOGGING = json.load(file)
filename = timezone.now().strftime('%Y-%m-%d_%H-%M-%S')
LOGGING["handlers"]["file"]["filename"] = LOGGING_DIR / f"{filename}.log"
# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/
LANGUAGE_CODE = "en-gb"
TIME_ZONE = "Europe/London"
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_ROOT = BASE_DIR / "staticfiles"
STATIC_URL = "/static/"
STATICFILES_DIRS = (BASE_DIR / "static",)
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
"compressor.finders.CompressorFinder",
)
# Compressors
COMPRESS_ENABLED = True
COMPRESS_OFFLINE = not DEBUG
COMPRESS_PRECOMPILERS = [("text/x-scss", "django_libsass.SassCompiler")]
COMPRESS_CSS_FILTERS = ["compressor.filters.css_default.CssAbsoluteIdentifier"]
LIBSASS_ADDITIONAL_INCLUDE_PATHS = [str(BASE_DIR / "static/")]
# Media Files
MEDIA_ROOT = BASE_DIR / "media"
MEDIA_URL = "/media/"
# region Rest Framework
# https://www.django-rest-framework.org/
REST_FRAMEWORK = {
"DEFAULT_THROTTLE_CLASSES": [
"rest_framework.throttling.AnonRateThrottle",
"rest_framework.throttling.UserRateThrottle"
],
"DEFAULT_THROTTLE_RATES": {
"anon": "100/day",
"user": "10000/hour"
},
"DEFAULT_RENDERER_CLASSES": [
"rest_framework.renderers.JSONRenderer",
# "rest_framework.renderers.AdminRenderer",
"rest_framework.renderers.BrowsableAPIRenderer"
],
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
"PAGE_SIZE": 100
}
# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"