From 82070ac6f7ef0217dad5a67cb97e69edd1d4e5a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Segersv=C3=A4rd?= Date: Thu, 14 Sep 2017 01:43:44 +0200 Subject: [PATCH 1/2] Generate a JSON file as a durable config --- .gitignore | 11 +++--- alge/settings.py | 93 ++++++++++++++++++++++++++++-------------------- 2 files changed, 58 insertions(+), 46 deletions(-) diff --git a/.gitignore b/.gitignore index 956190f..d87dd3a 100644 --- a/.gitignore +++ b/.gitignore @@ -63,12 +63,9 @@ target/ #Ipython Notebook .ipynb_checkpoints .idea/ -db.sqlite3 +# Application generated files media/ - -quotes.txt - -userlist.txt - -alge/config.py \ No newline at end of file +db.sqlite3 +streque.json +alge/config.py diff --git a/alge/settings.py b/alge/settings.py index d2d6b76..0f5b4d3 100644 --- a/alge/settings.py +++ b/alge/settings.py @@ -10,33 +10,51 @@ https://docs.djangoproject.com/en/1.9/ref/settings/ """ +import json import os - -#Load the config file. See config.py.sample for the structure -import string import random +import string -try: - import alge.config as config -except ImportError as e: - default_conf = """\ -# Remember to set the correct permissions for this file when in production. -# This is done (in Linux) with the command: -# chown *web server user*:*your user group* config.py && chmod 660 config.py - -DEBUG = True # False in a production environment -SECRET_KEY = '{secret_key}' -EMAIL_USE_TLS = True -EMAIL_HOST = None # smtp.gmail.com -EMAIL_HOST_USER = None # strecklistan@gmail.com -EMAIL_HOST_PASSWORD = None # Remember to set this -EMAIL_PORT = 587 -""".format(secret_key=''.join(random.choice(string.ascii_letters + string.digits) for _ in range(50))) - - with open("alge/config.py", "a") as f: - f.write(default_conf) - import alge.config as config +# Load JSON config if available. Otherwise, if config.py exists, convert it +# to JSON. Otherwise, just create a new one. +filename = "streque.json" +had_config = False +try: + f = open(filename, "r") + config = json.load(f) + had_config = True +except: + try: + import alge.config as c + config = { + "debug": c.DEBUG, + "secret_key": c.SECRET_KEY, + "email": { + "use_tls": c.EMAIL_USE_TLS, + "host": c.EMAIL_HOST, + "user": c.EMAIL_HOST_USER, + "password": c.EMAIL_HOST_PASSWORD, + "port": c.EMAIL_PORT, + }, + } + except ImportError as e: + char = lambda: random.choice(string.ascii_letters + string.digits) + config = { + "debug": False, + "secret_key": ''.join(char() for _ in range(50)), + "email": { + "use_tls": True, + "host": None, + "user": None, + "password": None, + "port": 587, + }, + } + +if not had_config: + with open(filename, "w") as f: + json.dump(config, f, indent=2, sort_keys=True) def filter_non_strings(items): @@ -51,13 +69,11 @@ def filter_non_strings(items): # See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = config.SECRET_KEY +SECRET_KEY = config["secret_key"] # SECURITY WARNING: don't run with debug turned on in production! - - if 'DEBUG' in dir(config): - DEBUG = config.DEBUG + DEBUG = config["debug"] else: DEBUG = True @@ -169,23 +185,22 @@ def filter_non_strings(items): STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, "static/") -#Media root (uploaded files) - +# Media root (uploaded files) MEDIA_ROOT = os.path.join(BASE_DIR, "media") MEDIA_URL = '/media/' -#Gmail stpm settings - -EMAIL_USE_TLS = config.EMAIL_USE_TLS -EMAIL_HOST = config.EMAIL_HOST -EMAIL_HOST_USER = config.EMAIL_HOST_USER -EMAIL_HOST_PASSWORD = config.EMAIL_HOST_PASSWORD -EMAIL_PORT = config.EMAIL_PORT +# E-mail SMTP settings +email_config = config["email"] +EMAIL_USE_TLS = email_config["use_tls"] +EMAIL_HOST = email_config["host"] +EMAIL_HOST_USER = email_config["user"] +EMAIL_HOST_PASSWORD = email_config["password"] +EMAIL_PORT = email_config["port"] -#url to login page +# url to login page LOGIN_URL = '/login' -#Custom user model: +# Custom user model: AUTH_USER_MODEL = 'EmailUser.MyUser' REST_FRAMEWORK = { @@ -203,7 +218,7 @@ def filter_non_strings(items): 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticated', ), - #Set page size so large numbers of data will be split up over multiple pages + # Set page size so large numbers of data will be split up over multiple pages 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', 'PAGE_SIZE': 50, 'MAX_PAGE_SIZE' : 200, From b237f6903b2eeeb7dca7edc224d14dad8da8da52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Segersv=C3=A4rd?= Date: Fri, 15 Sep 2017 00:43:43 +0200 Subject: [PATCH 2/2] Prefer INI to JSON --- .gitignore | 3 +- alge/settings.py | 76 +++++++++++++++++++++++------------------------- 2 files changed, 37 insertions(+), 42 deletions(-) diff --git a/.gitignore b/.gitignore index d87dd3a..27b5bf6 100644 --- a/.gitignore +++ b/.gitignore @@ -67,5 +67,4 @@ target/ # Application generated files media/ db.sqlite3 -streque.json -alge/config.py +streque.ini diff --git a/alge/settings.py b/alge/settings.py index 0f5b4d3..37a5ce7 100644 --- a/alge/settings.py +++ b/alge/settings.py @@ -10,51 +10,49 @@ https://docs.djangoproject.com/en/1.9/ref/settings/ """ -import json +from configparser import ConfigParser import os import random import string -# Load JSON config if available. Otherwise, if config.py exists, convert it -# to JSON. Otherwise, just create a new one. -filename = "streque.json" +# Load INI config if available. Otherwise, if config.py exists, convert it +# to INI. Otherwise, just create a new one. +config = ConfigParser() +filename = "streque.ini" had_config = False try: - f = open(filename, "r") - config = json.load(f) - had_config = True + with open(filename) as file: + config.read_file(file) + had_config = True except: try: import alge.config as c - config = { - "debug": c.DEBUG, - "secret_key": c.SECRET_KEY, + config.read_dict({ + "main": { + "debug": "yes" if c.DEBUG else "no", + "secret_key": c.SECRET_KEY, + }, "email": { - "use_tls": c.EMAIL_USE_TLS, - "host": c.EMAIL_HOST, - "user": c.EMAIL_HOST_USER, - "password": c.EMAIL_HOST_PASSWORD, - "port": c.EMAIL_PORT, + "use_tls": "yes" if c.EMAIL_USE_TLS else "no", + "host": c.EMAIL_HOST or "", + "user": c.EMAIL_HOST_USER or "", + "password": c.EMAIL_HOST_PASSWORD or "", + "port": str(c.EMAIL_PORT), }, - } - except ImportError as e: + }) + except ImportError: + # Generate a new config file char = lambda: random.choice(string.ascii_letters + string.digits) - config = { - "debug": False, - "secret_key": ''.join(char() for _ in range(50)), - "email": { - "use_tls": True, - "host": None, - "user": None, - "password": None, - "port": 587, + config.read_dict({ + "main": { + "secret_key": ''.join(char() for _ in range(50)), }, - } + }) if not had_config: - with open(filename, "w") as f: - json.dump(config, f, indent=2, sort_keys=True) + with open(filename, "w") as file: + config.write(file) def filter_non_strings(items): @@ -69,13 +67,12 @@ def filter_non_strings(items): # See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = config["secret_key"] +SECRET_KEY = config.get("main", "secret_key") # SECURITY WARNING: don't run with debug turned on in production! -if 'DEBUG' in dir(config): - DEBUG = config["debug"] -else: - DEBUG = True +DEBUG = config.getboolean("main", "debug", fallback=False) +if DEBUG: + print("NOTE: Running in DEBUG mode") ALLOWED_HOSTS = ["localhost", "streque.se", "www.streque.se"] @@ -190,12 +187,11 @@ def filter_non_strings(items): MEDIA_URL = '/media/' # E-mail SMTP settings -email_config = config["email"] -EMAIL_USE_TLS = email_config["use_tls"] -EMAIL_HOST = email_config["host"] -EMAIL_HOST_USER = email_config["user"] -EMAIL_HOST_PASSWORD = email_config["password"] -EMAIL_PORT = email_config["port"] +EMAIL_USE_TLS = config.getboolean("email", "use_tls", fallback=True) +EMAIL_HOST = config.get("email", "host", fallback=None) +EMAIL_HOST_USER = config.get("email", "user", fallback=None) +EMAIL_HOST_PASSWORD = config.get("email", "password", fallback=None) +EMAIL_PORT = config.getint("email", "port", fallback=587) # url to login page LOGIN_URL = '/login'