From 531b75c1282c2bb04c643cd966fec729c875490e Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 9 Jun 2022 08:51:47 +0100 Subject: [PATCH] Read configuration from environment --- requirements.txt | 1 + website/settings.py | 64 +++++++++------------------------------------ 2 files changed, 13 insertions(+), 52 deletions(-) diff --git a/requirements.txt b/requirements.txt index 17b8a9b..415c52b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ Django==4.0.5 wagtail==3.0 +django-environ==0.8.1 diff --git a/website/settings.py b/website/settings.py index e760264..751d319 100644 --- a/website/settings.py +++ b/website/settings.py @@ -1,38 +1,25 @@ -""" -Django settings for website project. - -Generated by 'django-admin startproject' using Django 4.0.5. - -For more information on this file, see -https://docs.djangoproject.com/en/4.0/topics/settings/ - -For the full list of settings and their values, see -https://docs.djangoproject.com/en/4.0/ref/settings/ -""" - -# Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os +import environ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +env = environ.Env( + DEBUG=(bool, False), + BASE_HOSTNAME=(str, "example.com") +) -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ +# Read local secrets +environ.Env.read_env(os.path.join(BASE_DIR, '.env')) -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = env('DEBUG') -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = "django-insecure-_w9@x(3f5xaa+3n%!a#v7!i9!n4dh0%hn2nb9@c49=2r2664u*" +SECRET_KEY = env('SECRET_KEY') -# SECURITY WARNING: define the correct hosts in production! ALLOWED_HOSTS = ["*"] EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" - # Application definition - INSTALLED_APPS = [ "website.home", "website.search", @@ -91,36 +78,10 @@ TEMPLATES = [ WSGI_APPLICATION = "website.wsgi.application" -# Database -# https://docs.djangoproject.com/en/4.0/ref/settings/#databases - DATABASES = { - "default": { - "ENGINE": "django.db.backends.sqlite3", - "NAME": os.path.join(BASE_DIR, "db.sqlite3"), - } + 'default': env.db(default=f"sqlite:///{BASE_DIR}/db.sqlite3") } - -# Password validation -# https://docs.djangoproject.com/en/4.0/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", - }, -] - - # Internationalization # https://docs.djangoproject.com/en/4.0/topics/i18n/ @@ -171,6 +132,5 @@ WAGTAILSEARCH_BACKENDS = { } } -# Base URL to use when referring to full URLs within the Wagtail admin backend - -# e.g. in notification emails. Don't include '/admin' or a trailing slash -WAGTAILADMIN_BASE_URL = "http://example.com" +BASE_HOSTNAME = env('BASE_HOSTNAME') +WAGTAILADMIN_BASE_URL = f"https://{BASE_HOSTNAME}"