Browse Source

Configuration adapted

master
Dirk Alders 4 years ago
parent
commit
970c4f5ee3
2 changed files with 45 additions and 15 deletions
  1. 15
    0
      config_example/config.py
  2. 30
    15
      main/settings.py

+ 15
- 0
config_example/config.py View File

@@ -0,0 +1,15 @@
1
+import os
2
+
3
+BASE_DIR = os.path.dirname(os.path.abspath(__file__))
4
+
5
+#
6
+# General settings
7
+#
8
+# SECRET_KEY = 'define a secret key'
9
+#
10
+# ALLOWED_HOSTS = []
11
+
12
+#
13
+# Style settings
14
+#
15
+# DEFAULT_THEME = 'clear-red'

+ 30
- 15
main/settings.py View File

@@ -10,13 +10,10 @@ For the full list of settings and their values, see
10 10
 https://docs.djangoproject.com/en/2.2/ref/settings/
11 11
 """
12 12
 
13
-try:
14
-    from config import config
15
-    # required keys: SECRET_KEY
16
-    # optional keys: ALLOWED_HOSTS, DEFAULT_THEME
17
-except ImportError:
18
-    config = {}
13
+import config
19 14
 import os
15
+import stat
16
+import sys
20 17
 import random
21 18
 
22 19
 
@@ -24,25 +21,46 @@ import random
24 21
 #
25 22
 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
26 23
 
24
+# Check permission of config.py
25
+#
26
+if sys.platform == 'linux' or sys.platform == 'linux2':
27
+    st = os.stat(os.path.join(BASE_DIR, 'config.py'))
28
+    if st.st_mode & stat.S_IRGRP or st.st_mode & stat.S_IROTH:
29
+        raise PermissionError("conig.py is readable by group or others.")
30
+
31
+# Default values, if not defined in config.py
32
+#
33
+USER_CONFIG_DEFAULTS = {
34
+    'SECRET_KEY': None,
35
+    'DEFAULT_THEME': 'clear-green',
36
+    'ALLOWED_HOSTS': [],
37
+}
38
+
39
+# Set configuration parameters
40
+#
41
+thismodule = sys.modules[__name__]
42
+for property_name in USER_CONFIG_DEFAULTS:
43
+    try:
44
+        value = getattr(config, property_name)
45
+    except AttributeError:
46
+        value = USER_CONFIG_DEFAULTS[property_name]
47
+    setattr(thismodule, property_name, value)
27 48
 
28 49
 # Quick-start development settings - unsuitable for production
29 50
 # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
30 51
 
31 52
 # SECURITY WARNING: keep the secret key used in production secret!
32 53
 #
33
-try:
34
-    SECRET_KEY = config['SECRET_KEY']
35
-except KeyError:
54
+if SECRET_KEY is None:
36 55
     chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
37 56
     s_key = ''.join([random.choice(chars) for n in range(50)])
38 57
     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)
39 58
     raise KeyError(secret_key_warning)
59
+
40 60
 # SECURITY WARNING: don't run with debug turned on in production!
41 61
 #
42 62
 DEBUG = False
43 63
 
44
-ALLOWED_HOSTS = config.get('ALLOWED_HOSTS', [])
45
-
46 64
 
47 65
 # Application definition
48 66
 #
@@ -124,6 +142,7 @@ AUTH_PASSWORD_VALIDATORS = [
124 142
     },
125 143
 ]
126 144
 
145
+
127 146
 # Search Engine
128 147
 #
129 148
 WHOOSH_PATH = os.path.join(BASE_DIR, 'data', 'whoosh_index')
@@ -241,7 +260,3 @@ File "%(pathname)s", line %(lineno)d, in %(funcName)s
241 260
 # Other Configuration issues
242 261
 #
243 262
 LOGIN_URL = 'users-login'
244
-
245
-# App Configuration
246
-#
247
-DEFAULT_THEME = config.get('DEFAULT_THEME', 'clear-green')

Loading…
Cancel
Save