Load database settings from config.yaml file

- Provide an initial config file
This commit is contained in:
Oliver Walters 2019-07-10 23:11:13 +10:00
parent 1f9e6f4a68
commit f36757c87b
3 changed files with 48 additions and 38 deletions

1
.gitignore vendored
View File

@ -40,6 +40,7 @@ secret_key.txt
# IDE / development files
.idea/
*.code-workspace
.vscode/
# Coverage reports
.coverage

View File

@ -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,31 +138,20 @@ 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 = {
@ -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

20
InvenTree/config.yaml Normal file
View File

@ -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