From 38fa3f079501380824ea8f4c5cce8bc8c7ce9b32 Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Tue, 14 Dec 2021 23:54:41 +0100 Subject: [PATCH] Remove instance-specific data from settings.py --- wish/settings.py | 59 ++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/wish/settings.py b/wish/settings.py index bc46d94..090ccc2 100644 --- a/wish/settings.py +++ b/wish/settings.py @@ -13,20 +13,11 @@ https://docs.djangoproject.com/en/3.1/ref/settings/ from pathlib import Path from os.path import isfile -SECRETS_FILE = '~/wish.secrets' -# FIXME: What if the secret key is not found or not read! -if isfile(SECRETS_FILE): - with open(SECRETS_FILE, "r") as f: - DEBUG = False - SECRET_KEY = f.readline().strip() - ALLOWED_HOSTS = ['wish.ledoian.cz'] - print("Running in production") -else: - DEBUG = True - # Bad, old, already has been in Git... - SECRET_KEY = 'h-mqswfvdslj#c@p264l*@qpsmsj6$$c052dsrh7a^(uza4q2a' - ALLOWED_HOSTS = [] - print("TEST SERVER!") +DEBUG = True +# Bad, old, already has been in Git... +SECRET_KEY = 'h-mqswfvdslj#c@p264l*@qpsmsj6$$c052dsrh7a^(uza4q2a' +ALLOWED_HOSTS = [] +EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -93,22 +84,12 @@ WSGI_APPLICATION = 'wish.wsgi.application' # Database # https://docs.djangoproject.com/en/3.1/ref/settings/#databases -if DEBUG: - DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': BASE_DIR / 'db.sqlite3', - } +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', } -else: - DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.postgresql', - 'NAME': 'wish', - 'USER': 'wish', - } - } - +} # Password validation # https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators @@ -147,3 +128,23 @@ USE_TZ = True # https://docs.djangoproject.com/en/3.1/howto/static-files/ AUTH_USER_MODEL = 'utils.MyUser' + + +SECRETS_FILE = '~/wish.secrets' +# Override settings +if isfile(SECRETS_FILE): + with open(SECRETS_FILE, "r") as f: + exec(f.read()) + +# verify that required variables are set +print('TEST SERVER' if DEBUG else 'Production server') +if not DEBUG: + for var in [ + 'SECRET_KEY', + 'ALLOWED_HOSTS', + 'EMAIL_BACKEND', + 'DEFAULT_FROM_EMAIL', + 'SERVER_EMAIL', + ]: + if var not in dir(): + raise ValueError(f'Required variable {var} not defined')