From 143a4a6e8c70557710d1b207a176f169d145ed1e Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Sat, 12 Nov 2022 22:10:06 -0700 Subject: Start some settings work --- takahe/asgi.py | 2 +- takahe/settings.py | 123 ----------------------------------------- takahe/settings/__init__.py | 0 takahe/settings/base.py | 107 +++++++++++++++++++++++++++++++++++ takahe/settings/development.py | 13 +++++ takahe/settings/production.py | 17 ++++++ takahe/settings/testing.py | 4 ++ takahe/wsgi.py | 2 +- 8 files changed, 143 insertions(+), 125 deletions(-) delete mode 100644 takahe/settings.py create mode 100644 takahe/settings/__init__.py create mode 100644 takahe/settings/base.py create mode 100644 takahe/settings/development.py create mode 100644 takahe/settings/production.py create mode 100644 takahe/settings/testing.py (limited to 'takahe') diff --git a/takahe/asgi.py b/takahe/asgi.py index 99a9cfb..3424b23 100644 --- a/takahe/asgi.py +++ b/takahe/asgi.py @@ -11,6 +11,6 @@ import os from django.core.asgi import get_asgi_application -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "takahe.settings") +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "takahe.settings.production") application = get_asgi_application() diff --git a/takahe/settings.py b/takahe/settings.py deleted file mode 100644 index e8982ae..0000000 --- a/takahe/settings.py +++ /dev/null @@ -1,123 +0,0 @@ -import os -from pathlib import Path - -# Build paths inside the project like this: BASE_DIR / 'subdir'. -BASE_DIR = Path(__file__).resolve().parent.parent - -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = os.environ.get("SECRET_KEY", "insecure_secret") - -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True - -ALLOWED_HOSTS = ["*"] -CSRF_TRUSTED_ORIGINS = ["http://*", "https://*"] - -# Application definition - -INSTALLED_APPS = [ - "django.contrib.admin", - "django.contrib.auth", - "django.contrib.contenttypes", - "django.contrib.sessions", - "django.contrib.messages", - "django.contrib.staticfiles", - "crispy_forms", - "core", - "activities", - "users", - "stator", -] - -MIDDLEWARE = [ - "core.middleware.AlwaysSecureMiddleware", - "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", - "users.middleware.IdentityMiddleware", -] - -ROOT_URLCONF = "takahe.urls" - -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", - "core.context.config_context", - ], - }, - }, -] - -WSGI_APPLICATION = "takahe.wsgi.application" - -DATABASES = { - "default": { - "ENGINE": "django.db.backends.postgresql_psycopg2", - "HOST": os.environ.get("POSTGRES_HOST", "localhost"), - "NAME": os.environ.get("POSTGRES_DB", "takahe"), - "USER": os.environ.get("POSTGRES_USER", "postgres"), - "PASSWORD": os.environ.get("POSTGRES_PASSWORD"), - } -} - -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", - }, -] - -LANGUAGE_CODE = "en-us" - -TIME_ZONE = "UTC" - -USE_I18N = True - -USE_TZ = True - -STATIC_URL = "static/" - -DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" - -AUTH_USER_MODEL = "users.User" - -LOGIN_URL = "/auth/login/" -LOGOUT_URL = "/auth/logout/" -LOGIN_REDIRECT_URL = "/" -LOGOUT_REDIRECT_URL = "/" - -STATICFILES_FINDERS = [ - "django.contrib.staticfiles.finders.FileSystemFinder", - "django.contrib.staticfiles.finders.AppDirectoriesFinder", -] - -STATICFILES_DIRS = [ - BASE_DIR / "static", -] - -CRISPY_FAIL_SILENTLY = not DEBUG - -SITE_NAME = "takahē" -DEFAULT_DOMAIN = "feditest.aeracode.org" -ALLOWED_DOMAINS = ["feditest.aeracode.org"] -IDENTITY_MAX_AGE = 24 * 60 * 60 diff --git a/takahe/settings/__init__.py b/takahe/settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/takahe/settings/base.py b/takahe/settings/base.py new file mode 100644 index 0000000..a2ccb98 --- /dev/null +++ b/takahe/settings/base.py @@ -0,0 +1,107 @@ +import os +from pathlib import Path + +BASE_DIR = Path(__file__).resolve().parent.parent.parent + +# Application definition + +INSTALLED_APPS = [ + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", + "crispy_forms", + "core", + "activities", + "users", + "stator", +] + +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", + "users.middleware.IdentityMiddleware", +] + +ROOT_URLCONF = "takahe.urls" + +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", + "core.context.config_context", + ], + }, + }, +] + +WSGI_APPLICATION = "takahe.wsgi.application" + +DATABASES = { + "default": { + "ENGINE": "django.db.backends.postgresql_psycopg2", + "HOST": os.environ.get("POSTGRES_HOST", "localhost"), + "NAME": os.environ.get("POSTGRES_DB", "takahe"), + "USER": os.environ.get("POSTGRES_USER", "postgres"), + "PASSWORD": os.environ.get("POSTGRES_PASSWORD"), + } +} + +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", + }, +] + +LANGUAGE_CODE = "en-us" + +TIME_ZONE = "UTC" + +USE_I18N = True + +USE_TZ = True + +STATIC_URL = "static/" + +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" + +AUTH_USER_MODEL = "users.User" + +LOGIN_URL = "/auth/login/" +LOGOUT_URL = "/auth/logout/" +LOGIN_REDIRECT_URL = "/" +LOGOUT_REDIRECT_URL = "/" + +STATICFILES_FINDERS = [ + "django.contrib.staticfiles.finders.FileSystemFinder", + "django.contrib.staticfiles.finders.AppDirectoriesFinder", +] + +STATICFILES_DIRS = [ + BASE_DIR / "static", +] + +ALLOWED_HOSTS = ["*"] diff --git a/takahe/settings/development.py b/takahe/settings/development.py new file mode 100644 index 0000000..4e0098b --- /dev/null +++ b/takahe/settings/development.py @@ -0,0 +1,13 @@ +import os + +from .base import * # noqa + +# Load secret key from environment with a fallback +SECRET_KEY = os.environ.get("TAKAHE_SECRET_KEY", "insecure_secret") + +# Disable the CRSF origin protection +MIDDLEWARE.insert(0, "core.middleware.AlwaysSecureMiddleware") + +# Ensure debug features are on +DEBUG = True +CRISPY_FAIL_SILENTLY = False diff --git a/takahe/settings/production.py b/takahe/settings/production.py new file mode 100644 index 0000000..2f943f4 --- /dev/null +++ b/takahe/settings/production.py @@ -0,0 +1,17 @@ +import os + +from .base import * # noqa + +# Load secret key from environment +try: + SECRET_KEY = os.environ["TAKAHE_SECRET_KEY"] +except KeyError: + print("You must specify the TAKAHE_SECRET_KEY environment variable!") + os._exit(1) + +# Ensure debug features are off +DEBUG = False +CRISPY_FAIL_SILENTLY = True + +# TODO: Allow better setting of allowed_hosts, if we need to +ALLOWED_HOSTS = ["*"] diff --git a/takahe/settings/testing.py b/takahe/settings/testing.py new file mode 100644 index 0000000..6527333 --- /dev/null +++ b/takahe/settings/testing.py @@ -0,0 +1,4 @@ +from .base import * # noqa + +# Fixed secret key +SECRET_KEY = "testing_secret" diff --git a/takahe/wsgi.py b/takahe/wsgi.py index 05ae06f..c8ad0a0 100644 --- a/takahe/wsgi.py +++ b/takahe/wsgi.py @@ -11,6 +11,6 @@ import os from django.core.wsgi import get_wsgi_application -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "takahe.settings") +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "takahe.settings.production") application = get_wsgi_application() -- cgit v1.2.3