From 276a926fcbd2a03fb8775fad27ca8e2b9d5261eb Mon Sep 17 00:00:00 2001 From: x11x <28614156+x11x@users.noreply.github.com> Date: Sun, 18 Feb 2018 13:43:29 +1000 Subject: [PATCH] Use secret.txt file auto-generated in project root as default SECRET_KEY Also add it to .gitignore --- .gitignore | 2 ++ etesync_server/settings.py | 11 ++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 10f1650..aa7817e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,8 @@ Session.vim /.venv /.coverage /htmlcov +/secret.txt +/static __pycache__ .*.swp diff --git a/etesync_server/settings.py b/etesync_server/settings.py index fd1ae53..ad433cb 100644 --- a/etesync_server/settings.py +++ b/etesync_server/settings.py @@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/1.10/ref/settings/ """ import os +from django.core.management import utils # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -20,7 +21,15 @@ 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 = '' +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) # SECURITY WARNING: don't run with debug turned on in production! DEBUG = False