Configuration adapted

This commit is contained in:
Dirk Alders 2020-01-31 12:50:42 +01:00
parent da3e48ec1f
commit 970c4f5ee3
2 changed files with 45 additions and 15 deletions

15
config_example/config.py Normal file
View File

@ -0,0 +1,15 @@
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
#
# General settings
#
# SECRET_KEY = 'define a secret key'
#
# ALLOWED_HOSTS = []
#
# Style settings
#
# DEFAULT_THEME = 'clear-red'

View File

@ -10,13 +10,10 @@ For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/ https://docs.djangoproject.com/en/2.2/ref/settings/
""" """
try: import config
from config import config
# required keys: SECRET_KEY
# optional keys: ALLOWED_HOSTS, DEFAULT_THEME
except ImportError:
config = {}
import os import os
import stat
import sys
import random import random
@ -24,25 +21,46 @@ import random
# #
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Check permission of config.py
#
if sys.platform == 'linux' or sys.platform == 'linux2':
st = os.stat(os.path.join(BASE_DIR, 'config.py'))
if st.st_mode & stat.S_IRGRP or st.st_mode & stat.S_IROTH:
raise PermissionError("conig.py is readable by group or others.")
# Default values, if not defined in config.py
#
USER_CONFIG_DEFAULTS = {
'SECRET_KEY': None,
'DEFAULT_THEME': 'clear-green',
'ALLOWED_HOSTS': [],
}
# Set configuration parameters
#
thismodule = sys.modules[__name__]
for property_name in USER_CONFIG_DEFAULTS:
try:
value = getattr(config, property_name)
except AttributeError:
value = USER_CONFIG_DEFAULTS[property_name]
setattr(thismodule, property_name, value)
# Quick-start development settings - unsuitable for production # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret! # SECURITY WARNING: keep the secret key used in production secret!
# #
try: if SECRET_KEY is None:
SECRET_KEY = config['SECRET_KEY']
except KeyError:
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)' chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
s_key = ''.join([random.choice(chars) for n in range(50)]) s_key = ''.join([random.choice(chars) for n in range(50)])
secret_key_warning = "You need to create a config.py file including a variable config which is a dict with at least a SECRET_KEY definition (e.g.: %s)." % repr(s_key) secret_key_warning = "You need to create a config.py file including a variable config which is a dict with at least a SECRET_KEY definition (e.g.: %s)." % repr(s_key)
raise KeyError(secret_key_warning) raise KeyError(secret_key_warning)
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
# #
DEBUG = False DEBUG = False
ALLOWED_HOSTS = config.get('ALLOWED_HOSTS', [])
# Application definition # Application definition
# #
@ -124,6 +142,7 @@ AUTH_PASSWORD_VALIDATORS = [
}, },
] ]
# Search Engine # Search Engine
# #
WHOOSH_PATH = os.path.join(BASE_DIR, 'data', 'whoosh_index') WHOOSH_PATH = os.path.join(BASE_DIR, 'data', 'whoosh_index')
@ -241,7 +260,3 @@ File "%(pathname)s", line %(lineno)d, in %(funcName)s
# Other Configuration issues # Other Configuration issues
# #
LOGIN_URL = 'users-login' LOGIN_URL = 'users-login'
# App Configuration
#
DEFAULT_THEME = config.get('DEFAULT_THEME', 'clear-green')