tubearchivist/tubearchivist/config/settings.py

211 lines
5.9 KiB
Python
Raw Normal View History

2021-09-05 17:10:14 +00:00
"""
Django settings for config project.
Generated by 'django-admin startproject' using Django 3.2.5.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
2021-10-25 07:22:23 +00:00
import hashlib
2021-10-25 06:40:09 +00:00
from os import environ, path
2021-09-18 13:02:54 +00:00
from pathlib import Path
2021-09-05 17:10:14 +00:00
import ldap
2022-01-14 07:46:00 +00:00
from corsheaders.defaults import default_headers
from django_auth_ldap.config import LDAPSearch
2022-01-22 15:13:37 +00:00
from home.src.ta.config import AppConfig
2021-10-25 06:40:09 +00:00
2021-09-05 17:10:14 +00:00
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
2021-10-25 12:28:05 +00:00
PW_HASH = hashlib.sha256(environ.get("TA_PASSWORD").encode())
2021-10-25 07:22:23 +00:00
SECRET_KEY = PW_HASH.hexdigest()
2021-09-05 17:10:14 +00:00
# SECURITY WARNING: don't run with debug turned on in production!
2021-09-21 09:25:22 +00:00
DEBUG = bool(environ.get("DJANGO_DEBUG"))
2021-09-05 17:10:14 +00:00
2022-07-14 10:26:10 +00:00
ALLOWED_HOSTS = [i.strip() for i in environ.get("TA_HOST").split()]
2022-08-13 07:03:17 +00:00
CSRF_TRUSTED_ORIGINS = []
for host in ALLOWED_HOSTS:
if host.startswith("http://") or host.startswith("https://"):
CSRF_TRUSTED_ORIGINS.append(host)
else:
CSRF_TRUSTED_ORIGINS.append(f"http://{host}")
2021-09-05 17:10:14 +00:00
# Application definition
INSTALLED_APPS = [
2021-09-21 09:25:22 +00:00
"home.apps.HomeConfig",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
2022-01-14 07:46:00 +00:00
"corsheaders",
2021-09-21 09:25:22 +00:00
"whitenoise.runserver_nostatic",
"django.contrib.staticfiles",
"django.contrib.humanize",
"rest_framework",
2022-01-11 07:15:36 +00:00
"rest_framework.authtoken",
"api",
2021-09-05 17:10:14 +00:00
]
MIDDLEWARE = [
2021-09-21 09:25:22 +00:00
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
2022-01-14 07:46:00 +00:00
"corsheaders.middleware.CorsMiddleware",
2021-09-21 09:25:22 +00:00
"whitenoise.middleware.WhiteNoiseMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
2021-09-05 17:10:14 +00:00
]
2021-09-21 09:25:22 +00:00
ROOT_URLCONF = "config.urls"
2021-09-05 17:10:14 +00:00
TEMPLATES = [
{
2021-09-21 09:25:22 +00:00
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"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",
2021-09-05 17:10:14 +00:00
],
},
},
]
2021-09-21 09:25:22 +00:00
WSGI_APPLICATION = "config.wsgi.application"
2021-09-05 17:10:14 +00:00
if bool(environ.get("TA_LDAP")):
2022-08-13 07:03:17 +00:00
# pylint: disable=global-at-module-level
global AUTH_LDAP_SERVER_URI
AUTH_LDAP_SERVER_URI = environ.get("TA_LDAP_SERVER_URI")
global AUTH_LDAP_BIND_DN
AUTH_LDAP_BIND_DN = environ.get("TA_LDAP_BIND_DN")
global AUTH_LDAP_BIND_PASSWORD
AUTH_LDAP_BIND_PASSWORD = environ.get("TA_LDAP_BIND_PASSWORD")
global AUTH_LDAP_USER_SEARCH
2022-08-13 07:03:17 +00:00
# pylint: disable=no-member
AUTH_LDAP_USER_SEARCH = LDAPSearch(
environ.get("TA_LDAP_USER_BASE"),
ldap.SCOPE_SUBTREE,
"(&(uid=%(user)s)" + environ.get("TA_LDAP_USER_FILTER") + ")",
)
global AUTH_LDAP_USER_ATTR_MAP
AUTH_LDAP_USER_ATTR_MAP = {
"username": "uid",
"first_name": "givenName",
"last_name": "sn",
"email": "mail",
}
if bool(environ.get("TA_LDAP_DISABLE_CERT_CHECK")):
global AUTH_LDAP_GLOBAL_OPTIONS
AUTH_LDAP_GLOBAL_OPTIONS = {
ldap.OPT_X_TLS_REQUIRE_CERT: ldap.OPT_X_TLS_NEVER,
}
global AUTHENTICATION_BACKENDS
AUTHENTICATION_BACKENDS = ("django_auth_ldap.backend.LDAPBackend",)
2021-09-05 17:10:14 +00:00
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
2021-10-25 06:40:09 +00:00
CACHE_DIR = AppConfig().config["application"]["cache_dir"]
DB_PATH = path.join(CACHE_DIR, "db.sqlite3")
2021-09-05 17:10:14 +00:00
DATABASES = {
2021-09-21 09:25:22 +00:00
"default": {
"ENGINE": "django.db.backends.sqlite3",
2021-10-25 06:40:09 +00:00
"NAME": DB_PATH,
2021-09-05 17:10:14 +00:00
}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
2021-09-21 09:25:22 +00:00
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", # noqa: E501
2021-09-05 17:10:14 +00:00
},
{
2021-09-21 09:25:22 +00:00
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", # noqa: E501
2021-09-05 17:10:14 +00:00
},
{
2021-09-21 09:25:22 +00:00
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", # noqa: E501
2021-09-05 17:10:14 +00:00
},
{
2021-09-21 09:25:22 +00:00
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", # noqa: E501
2021-09-05 17:10:14 +00:00
},
]
2021-10-25 12:28:05 +00:00
AUTH_USER_MODEL = "home.Account"
2021-09-05 17:10:14 +00:00
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
2021-09-21 09:25:22 +00:00
LANGUAGE_CODE = "en-us"
2021-12-02 07:44:37 +00:00
TIME_ZONE = environ.get("TZ") or "UTC"
2021-09-05 17:10:14 +00:00
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
2021-09-21 09:25:22 +00:00
STATIC_URL = "/static/"
STATICFILES_DIRS = (str(BASE_DIR.joinpath("static")),)
STATIC_ROOT = str(BASE_DIR.joinpath("staticfiles"))
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
2021-09-05 17:10:14 +00:00
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
2021-09-21 09:25:22 +00:00
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
LOGIN_URL = "/login/"
2021-10-22 04:45:06 +00:00
LOGOUT_REDIRECT_URL = "/login/"
2022-01-14 07:46:00 +00:00
# Cors needed for browser extension
# background.js makes the request so HTTP_ORIGIN will be from extension
if environ.get("DISABLE_CORS"):
# disable cors
CORS_ORIGIN_ALLOW_ALL = True
else:
CORS_ALLOWED_ORIGIN_REGEXES = [
r"moz-extension://*",
r"chrome-extension://*",
]
2022-04-23 13:17:31 +00:00
CORS_ALLOWED_ORIGINS = ["http://localhost:3000"]
2022-01-14 07:46:00 +00:00
CORS_ALLOW_HEADERS = list(default_headers) + [
"mode",
]
2022-04-05 11:43:15 +00:00
# TA application settings
TA_UPSTREAM = "https://github.com/tubearchivist/tubearchivist"
2022-09-19 08:21:55 +00:00
TA_VERSION = "v0.2.2"