From 5cea6d1589fa487a248db8e742bda43d24c6579e Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Fri, 31 Jan 2020 10:49:15 +0100 Subject: [PATCH] Configuration adapted --- config_example/config.py | 32 ++++++++++++++++++++++ main/settings.py | 57 ++++++++++++++++++++++++++-------------- pygal | 2 +- 3 files changed, 71 insertions(+), 20 deletions(-) create mode 100644 config_example/config.py diff --git a/config_example/config.py b/config_example/config.py new file mode 100644 index 0000000..61c7606 --- /dev/null +++ b/config_example/config.py @@ -0,0 +1,32 @@ +import os + +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) + +# +# General settings +# +# SECRET_KEY = 'define a secret key' +# +# ITEM_ROOT = os.path.join(BASE_DIR, 'data', 'example_data') +# ALLOWED_HOSTS = [] + +# +# Access Right settings +# +# SUSPEND_PUBLIC = True # Set this to True to ensure, that unauthenticated users have no permission + +# +# Style settings +# +# DEFAULT_THEME = 'clear-red' +# THUMBNAIL_SIZES = [137, 175, 250] +# WEBNAIL_SIZES = [450, 1100, 1750] + +# +# Content settings +# +# SORT_BY = False # Sorting by name if False +# SHOW_IMAGE = True +# SHOW_VIDEO = True +# SHOW_AUDIO = False +# SHOW_OTHER = False diff --git a/main/settings.py b/main/settings.py index 3f0d13a..7525010 100644 --- a/main/settings.py +++ b/main/settings.py @@ -10,13 +10,10 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/2.2/ref/settings/ """ -try: - from config import config - # required keys: SECRET_KEY - # optional keys: ALLOWED_HOSTS, DEFAULT_THEME, ITEM_ROOT, THUMBNAIL_SIZES, WEBNAIL_SIZES -except ImportError: - config = {} +import config import os +import stat +import sys import random @@ -24,25 +21,55 @@ import random # 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-red', + 'ALLOWED_HOSTS': [], + 'ITEM_ROOT': os.path.join(BASE_DIR, 'data', 'example_data'), + 'THUMBNAIL_SIZES': [137, 175, 250], + 'WEBNAIL_SIZES': [450, 1100, 1750], + 'SUSPEND_PUBLIC': True, + 'SORT_BY_DATE': True, + 'SHOW_IMAGE': True, + 'SHOW_VIDEO': True, + 'SHOW_AUDIO': False, + 'SHOW_OTHER': False, +} + +# 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 # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! # -try: - SECRET_KEY = config['SECRET_KEY'] -except KeyError: +if SECRET_KEY is None: chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)' 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) raise KeyError(secret_key_warning) + # SECURITY WARNING: don't run with debug turned on in production! # DEBUG = False -ALLOWED_HOSTS = config.get('ALLOWED_HOSTS', []) - # Application definition # @@ -243,11 +270,3 @@ LOGIN_URL = 'users-login' XNAIL_ROOT = os.path.join(BASE_DIR, 'data', 'xnails') TEMP_ROOT = os.path.join(BASE_DIR, 'data', 'temp') - - -# App Configuration -# -DEFAULT_THEME = config.get('DEFAULT_THEME', 'clear-red') -ITEM_ROOT = config.get('ITEM_ROOT', os.path.join(BASE_DIR, 'data', 'example_data')) -THUMBNAIL_SIZES = config.get('THUMBNAIL_SIZES', [137, 175, 250]) -WEBNAIL_SIZES = config.get('WEBNAIL_SIZES', [450, 1100, 1750]) diff --git a/pygal b/pygal index 983fa59..9da7e60 160000 --- a/pygal +++ b/pygal @@ -1 +1 @@ -Subproject commit 983fa59231403732d08716c1d7677fa3b090bd44 +Subproject commit 9da7e60a131d94fe30ecb3d3ae7ec733fa173dd3