website/kumisystems/settings.py
2023-07-08 20:00:35 +02:00

253 lines
7.1 KiB
Python

from pathlib import Path
from autosecretkey import AutoSecretKey
from django.urls import reverse_lazy
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
ASK = AutoSecretKey(BASE_DIR / 'settings.ini')
SECRET_KEY = ASK.secret_key
CONFIG = ASK.config
DEBUG = CONFIG.get('KumiSystems', 'Debug', fallback=False)
HOST = CONFIG.get('KumiSystems', 'Host', fallback='localhost')
ALLOWED_HOSTS = [HOST]
if HOST.startswith("www."):
ALLOWED_HOSTS.append(HOST[4:])
else:
ALLOWED_HOSTS.append(f"www.{HOST}")
CSRF_TRUSTED_ORIGINS = [f"https://{HOST}" for HOST in ALLOWED_HOSTS]
SITE_ID = CONFIG.getint('KumiSystems', 'SiteID', fallback=1)
# Application definition
INSTALLED_APPS = [
'djangocms_admin_style', # Must be listed before 'django.contrib.admin'
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Required for Django CMS:
# https://docs.django-cms.org/en/latest/how_to/install.html#installed-apps
'django.contrib.sites',
'cms',
'menus',
'treebeard',
'sekizai',
'filer',
'easy_thumbnails',
'mptt',
'djangocms_text_ckeditor',
'djangocms_link',
'djangocms_file',
'djangocms_picture',
'djangocms_video',
'djangocms_snippet',
'djangocms_style',
# Optional for OpenID Connect:
'mozilla_django_oidc',
]
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',
# Required for Django CMS:
# https://docs.django-cms.org/en/latest/how_to/install.html#middleware
'django.middleware.locale.LocaleMiddleware',
'cms.middleware.utils.ApphookReloadMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
'cms.middleware.language.LanguageCookieMiddleware',
]
ROOT_URLCONF = 'kumisystems.urls'
WSGI_APPLICATION = 'kumisystems.wsgi.application'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'frontend/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',
# Required for Django CMS:
# https://docs.django-cms.org/en/latest/how_to/install.html#sekizai
# https://docs.django-cms.org/en/latest/how_to/install.html#context-processors
'django.template.context_processors.i18n',
'sekizai.context_processors.sekizai',
'cms.context_processors.cms_settings',
],
},
},
]
CMS_TEMPLATES = [
('default.html', 'Standard page template'),
('home.html', 'Home page template'),
]
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
if "MySQL" in CONFIG:
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': CONFIG.get("MySQL", "Database"),
'USER': CONFIG.get("MySQL", "Username"),
'PASSWORD': CONFIG.get("MySQL", "Password"),
'HOST': CONFIG.get("MySQL", "Host", fallback="localhost"),
'PORT': CONFIG.getint("MySQL", "Port", fallback=3306),
'OPTIONS': {
'charset': 'utf8mb4',
'sql_mode': 'traditional',
}
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
USE_OIDC = False
if "OIDC" in CONFIG:
USE_OIDC = True
AUTHENTICATION_BACKENDS = [
'kumisystems.auth.OIDCBackend',
]
LOGIN_URL = reverse_lazy("oidc_authentication_init")
OIDC_NAME = CONFIG.get("OIDC", "Name", fallback="OIDC")
OIDC_RP_CLIENT_ID = CONFIG["OIDC"]["ClientID"]
OIDC_RP_CLIENT_SECRET = CONFIG["OIDC"]["ClientSecret"]
OIDC_OP_JWKS_ENDPOINT = CONFIG["OIDC"]["JWKS"]
OIDC_OP_AUTHORIZATION_ENDPOINT = CONFIG["OIDC"]["Authorization"]
OIDC_OP_TOKEN_ENDPOINT = CONFIG["OIDC"]["Token"]
OIDC_OP_USER_ENDPOINT = CONFIG["OIDC"]["UserInfo"]
OIDC_CREATE_USER = CONFIG.getboolean("OIDC", "CreateUsers", fallback=False)
OIDC_RP_SIGN_ALGO = CONFIG.get("OIDC", "Algorithm", fallback="RS256")
MIDDLEWARE.append("mozilla_django_oidc.middleware.SessionRefresh")
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/4.2/topics/i18n/
# https://docs.django-cms.org/en/latest/how_to/install.html#language-settings
LANGUAGE_CODE = 'en'
LANGUAGES = [
('en', 'English'),
('de', 'German'),
]
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images) and media files (user uploads)
# https://docs.djangoproject.com/en/4.2/howto/static-files/
# https://docs.djangoproject.com/en/4.2/topics/files/
STATIC_URL = 'static/'
STATIC_ROOT = CONFIG.get(
"KumiSystems", "StaticRoot", fallback=BASE_DIR / "static")
STATICFILES_DIRS = [
BASE_DIR / "frontend/static",
]
MEDIA_URL = 'media/'
MEDIA_ROOT = CONFIG.get(
"KumiSystems", "MediaRoot", fallback=BASE_DIR / "media")
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = "/"
if "S3" in CONFIG:
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage'
AWS_ACCESS_KEY_ID = CONFIG.get("S3", "AccessKey")
AWS_SECRET_ACCESS_KEY = CONFIG.get("S3", "SecretKey")
AWS_STORAGE_BUCKET_NAME = CONFIG.get("S3", "Bucket")
AWS_S3_ENDPOINT_URL = CONFIG.get("S3", "Endpoint")
# Easy Thumbnails settings
THUMBNAIL_HIGH_RESOLUTION = True
THUMBNAIL_PROCESSORS = (
'easy_thumbnails.processors.colorspace',
'easy_thumbnails.processors.autocrop',
'filer.thumbnail_processors.scale_and_crop_with_subject_location',
'easy_thumbnails.processors.filters'
)
# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# Django CMS settings
X_FRAME_OPTIONS = 'SAMEORIGIN'