From f36757c87bf78c7889eea3b5004f75f82501d2d4 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 10 Jul 2019 23:11:13 +1000 Subject: [PATCH] Load database settings from config.yaml file - Provide an initial config file --- .gitignore | 1 + InvenTree/InvenTree/settings.py | 65 ++++++++++++++------------------- InvenTree/config.yaml | 20 ++++++++++ 3 files changed, 48 insertions(+), 38 deletions(-) create mode 100644 InvenTree/config.yaml diff --git a/.gitignore b/.gitignore index 25ae56db0a..f251804992 100644 --- a/.gitignore +++ b/.gitignore @@ -40,6 +40,7 @@ secret_key.txt # IDE / development files .idea/ *.code-workspace +.vscode/ # Coverage reports .coverage diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 83549d70e2..7ed35a11e2 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -1,33 +1,42 @@ """ Django settings for InvenTree project. -Generated by 'django-admin startproject' using Django 1.10.6. +In practice the settings in this file should not be adjusted, +instead settings can be configured in the config.yaml file +located in the top level project directory. -For more information on this file, see -https://docs.djangoproject.com/en/1.10/topics/settings/ +This allows implementation configuration to be hidden from source control, +as well as separate configuration parameters from the more complex +database setup in this file. -For the full list of settings and their values, see -https://docs.djangoproject.com/en/1.10/ref/settings/ """ import os import logging import tempfile +import io +import yaml # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +cfg_filename = os.path.join(BASE_DIR, 'config.yaml') -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/ +if not os.path.exists(cfg_filename): + CONFIG = {} + print("Warning: config.yaml not found - using default settings") +else: + with open(cfg_filename, 'r') as cfg: + CONFIG = yaml.safe_load(cfg) -# SECURITY WARNING: keep the secret key used in production secret! +# Read the autogenerated key-file key_file = open(os.path.join(BASE_DIR, 'secret_key.txt'), 'r') SECRET_KEY = key_file.read().strip() +# Default action is to run the system in Debug mode # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = CONFIG.get('debug', True) ALLOWED_HOSTS = ['*'] @@ -85,14 +94,6 @@ LOGGING = { 'class': 'logging.StreamHandler', }, }, - - # 'loggers': { - # 'ddjango.db.backends': { - # 'level': 'DEBUG', - # 'handlers': ['console',], - # 'propagate': True - # }, - # }, } @@ -108,7 +109,7 @@ MIDDLEWARE = [ 'InvenTree.middleware.AuthRequiredMiddleware', ] -if DEBUG: +if CONFIG.get('log_queries', False): MIDDLEWARE.append('InvenTree.middleware.QueryCountMiddleware') ROOT_URLCONF = 'InvenTree.urls' @@ -137,32 +138,21 @@ REST_FRAMEWORK = { 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.TokenAuthentication', ) - # 'EXCEPTION_HANDLER': 'InvenTree.utils.api_exception_handler', - # 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', - # 'PAGE_SIZE': 50, } WSGI_APPLICATION = 'InvenTree.wsgi.application' +DATABASES = {} -# Database -# https://docs.djangoproject.com/en/1.10/ref/settings/#databases - -DATABASES = { - 'default': { +# Database backend selection +if 'database' in CONFIG: + DATABASES['default'] = CONFIG['database'] +else: + print("Warning: Database backend not specified - using default") + DATABASES['default'] = { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'inventree_db.sqlite3'), - }, - # TODO - Uncomment this when postgresql support is re-integrated - # 'postgresql': { - # 'ENGINE': 'django.db.backends.postgresql', - # 'NAME': 'inventree', - # 'USER': 'inventreeuser', - # 'PASSWORD': 'inventree', - # 'HOST': 'localhost', - # 'PORT': '', - # } -} + } CACHES = { 'default': { @@ -177,7 +167,6 @@ CACHES = { QR_CODE_CACHE_ALIAS = 'qr-code' - # Password validation # https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators diff --git a/InvenTree/config.yaml b/InvenTree/config.yaml new file mode 100644 index 0000000000..f2c34b65fb --- /dev/null +++ b/InvenTree/config.yaml @@ -0,0 +1,20 @@ + +# Database backend selection - Configure backend database settings +# Ref: https://docs.djangoproject.com/en/2.2/ref/settings/#std:setting-DATABASES +# Specify database parameters below as they appear in the Django docs +database: + ENGINE: django.db.backends.sqlite3 + NAME: inventree_db.sqlite3 + + # For more complex database installations, further parameters are required + # Refer to the django documentation for full list of options + # USER: db_username + # PASSWORD: db_password + # HOST: db_hostname + # PORT: db_port + +# Set debug to False to run in production mode +debug: True + +# Logging options +log_queries: False \ No newline at end of file