From 69655008e07ec0818c5293289fd08676d6d6e131 Mon Sep 17 00:00:00 2001 From: x11x <28614156+x11x@users.noreply.github.com> Date: Sun, 18 Feb 2018 14:19:29 +1000 Subject: [PATCH] Refactor out the secret.txt file handling to a utils module --- etesync_server/settings.py | 14 ++++---------- etesync_server/utils.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 10 deletions(-) create mode 100644 etesync_server/utils.py diff --git a/etesync_server/settings.py b/etesync_server/settings.py index ad433cb..fe65225 100644 --- a/etesync_server/settings.py +++ b/etesync_server/settings.py @@ -11,7 +11,7 @@ https://docs.djangoproject.com/en/1.10/ref/settings/ """ import os -from django.core.management import utils +from .utils import get_secret_from_file # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -21,15 +21,9 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY_FILE = os.path.join(BASE_DIR, "secret.txt") - -try: - with open(SECRET_KEY_FILE, "r") as f: - SECRET_KEY = f.read().strip() -except EnvironmentError: - with open(SECRET_KEY_FILE, "w") as f: - SECRET_KEY = utils.get_random_secret_key() - f.write(SECRET_KEY) +# See secret.py for how this is generated; uses a file 'secret.txt' in the root +# directory +SECRET_KEY = get_secret_from_file(os.path.join(BASE_DIR, "secret.txt")) # SECURITY WARNING: don't run with debug turned on in production! DEBUG = False diff --git a/etesync_server/utils.py b/etesync_server/utils.py new file mode 100644 index 0000000..8f85f10 --- /dev/null +++ b/etesync_server/utils.py @@ -0,0 +1,11 @@ +from django.core.management import utils + +def get_secret_from_file(path): + try: + with open(path, "r") as f: + return f.read().strip() + except EnvironmentError: + with open(path, "w") as f: + secret_key = utils.get_random_secret_key() + f.write(secret_key) + return secret_key