mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Allow more settings to be specified via environment variables
This commit is contained in:
parent
2277d225eb
commit
3ae4125df3
@ -20,9 +20,28 @@ from datetime import datetime
|
|||||||
import yaml
|
import yaml
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
|
||||||
def _is_true(x):
|
def _is_true(x):
|
||||||
return x in [True, "True", "true", "Y", "y", "1"]
|
# Shortcut function to determine if a value "looks" like a boolean
|
||||||
|
return str(x).lower() in ['1', 'y', 'yes', 't', 'true']
|
||||||
|
|
||||||
|
def get_setting(environment_var, backup_val, default_value = None):
|
||||||
|
"""
|
||||||
|
Helper function for retrieving a configuration setting value
|
||||||
|
|
||||||
|
- First preference is to look for the environment variable
|
||||||
|
- Second preference is to look for the value of the settings file
|
||||||
|
- Third preference is the default value
|
||||||
|
"""
|
||||||
|
|
||||||
|
val = os.getenv(environment_var)
|
||||||
|
|
||||||
|
if val is not None:
|
||||||
|
return val
|
||||||
|
|
||||||
|
if backup_val is not None:
|
||||||
|
return backup_val
|
||||||
|
|
||||||
|
return default_value
|
||||||
|
|
||||||
|
|
||||||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
||||||
@ -39,10 +58,17 @@ with open(cfg_filename, 'r') as cfg:
|
|||||||
|
|
||||||
# Default action is to run the system in Debug mode
|
# Default action is to run the system in Debug mode
|
||||||
# SECURITY WARNING: don't run with debug turned on in production!
|
# SECURITY WARNING: don't run with debug turned on in production!
|
||||||
DEBUG = _is_true(os.getenv("INVENTREE_DEBUG", CONFIG.get("debug", True)))
|
DEBUG = _is_true(get_setting(
|
||||||
|
'INVENTREE_DEBUG',
|
||||||
|
CONFIG.get('debug', True)
|
||||||
|
))
|
||||||
|
|
||||||
# Configure logging settings
|
# Configure logging settings
|
||||||
log_level = CONFIG.get('log_level', 'DEBUG').upper()
|
log_level = get_setting(
|
||||||
|
'INVENTREE_LOG_LEVEL',
|
||||||
|
CONFIG.get('log_level', 'DEBUG')
|
||||||
|
)
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=log_level,
|
level=log_level,
|
||||||
format="%(asctime)s %(levelname)s %(message)s",
|
format="%(asctime)s %(levelname)s %(message)s",
|
||||||
@ -75,6 +101,7 @@ if os.getenv("INVENTREE_SECRET_KEY"):
|
|||||||
else:
|
else:
|
||||||
# Secret key passed in by file location
|
# Secret key passed in by file location
|
||||||
key_file = os.getenv("INVENTREE_SECRET_KEY_FILE")
|
key_file = os.getenv("INVENTREE_SECRET_KEY_FILE")
|
||||||
|
|
||||||
if key_file:
|
if key_file:
|
||||||
if os.path.isfile(key_file):
|
if os.path.isfile(key_file):
|
||||||
logger.info("SECRET_KEY loaded by INVENTREE_SECRET_KEY_FILE")
|
logger.info("SECRET_KEY loaded by INVENTREE_SECRET_KEY_FILE")
|
||||||
@ -112,7 +139,12 @@ if cors_opt:
|
|||||||
STATIC_URL = '/static/'
|
STATIC_URL = '/static/'
|
||||||
|
|
||||||
# The filesystem location for served static files
|
# The filesystem location for served static files
|
||||||
STATIC_ROOT = os.path.abspath(CONFIG.get('static_root', os.path.join(BASE_DIR, 'static')))
|
STATIC_ROOT = os.path.abspath(
|
||||||
|
get_setting(
|
||||||
|
'INVENTREE_STATIC_ROOT',
|
||||||
|
CONFIG.get('static_root', os.path.join(BASE_DIR, 'static'))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
STATICFILES_DIRS = [
|
STATICFILES_DIRS = [
|
||||||
os.path.join(BASE_DIR, 'InvenTree', 'static'),
|
os.path.join(BASE_DIR, 'InvenTree', 'static'),
|
||||||
@ -125,7 +157,12 @@ STATIC_COLOR_THEMES_DIR = os.path.join(STATIC_ROOT, 'css', 'color-themes')
|
|||||||
MEDIA_URL = '/media/'
|
MEDIA_URL = '/media/'
|
||||||
|
|
||||||
# The filesystem location for served static files
|
# The filesystem location for served static files
|
||||||
MEDIA_ROOT = os.path.abspath(CONFIG.get('media_root', os.path.join(BASE_DIR, 'media')))
|
MEDIA_ROOT = os.path.abspath(
|
||||||
|
get_setting(
|
||||||
|
'INVENTREE_MEDIA_ROOT',
|
||||||
|
CONFIG.get('media_root', os.path.join(BASE_DIR, 'media'))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
logger.info("InvenTree running in DEBUG mode")
|
logger.info("InvenTree running in DEBUG mode")
|
||||||
@ -133,30 +170,6 @@ if DEBUG:
|
|||||||
logger.info(f"MEDIA_ROOT: '{MEDIA_ROOT}'")
|
logger.info(f"MEDIA_ROOT: '{MEDIA_ROOT}'")
|
||||||
logger.info(f"STATIC_ROOT: '{STATIC_ROOT}'")
|
logger.info(f"STATIC_ROOT: '{STATIC_ROOT}'")
|
||||||
|
|
||||||
# Does the user wish to use the sentry.io integration?
|
|
||||||
sentry_opts = CONFIG.get('sentry', {})
|
|
||||||
|
|
||||||
if sentry_opts.get('enabled', False):
|
|
||||||
|
|
||||||
logger.info("Configuring sentry.io integration")
|
|
||||||
|
|
||||||
dsn = sentry_opts.get('dsn', None)
|
|
||||||
|
|
||||||
if dsn is not None:
|
|
||||||
# Try to import required modules (exit if not installed)
|
|
||||||
try:
|
|
||||||
import sentry_sdk
|
|
||||||
from sentry_sdk.integrations.django import DjangoIntegration
|
|
||||||
|
|
||||||
sentry_sdk.init(dsn=dsn, integrations=[DjangoIntegration()], send_default_pii=True)
|
|
||||||
|
|
||||||
except ModuleNotFoundError:
|
|
||||||
logger.error("sentry_sdk module not found. Install using 'pip install sentry-sdk'")
|
|
||||||
sys.exit(-1)
|
|
||||||
|
|
||||||
else:
|
|
||||||
logger.warning("Sentry.io DSN not specified in config file")
|
|
||||||
|
|
||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
@ -430,16 +443,17 @@ if not type(EXTRA_URL_SCHEMES) in [list]:
|
|||||||
EXTRA_URL_SCHEMES = []
|
EXTRA_URL_SCHEMES = []
|
||||||
|
|
||||||
# Internationalization
|
# Internationalization
|
||||||
# https://docs.djangoproject.com/en/1.10/topics/i18n/
|
# https://docs.djangoproject.com/en/dev/topics/i18n/
|
||||||
|
|
||||||
LANGUAGE_CODE = CONFIG.get('language', 'en-us')
|
LANGUAGE_CODE = CONFIG.get('language', 'en-us')
|
||||||
|
|
||||||
# If a new language translation is supported, it must be added here
|
# If a new language translation is supported, it must be added here
|
||||||
LANGUAGES = [
|
LANGUAGES = [
|
||||||
('en', _('English')),
|
('en', _('English')),
|
||||||
('de', _('German')),
|
|
||||||
('fr', _('French')),
|
('fr', _('French')),
|
||||||
|
('de', _('German')),
|
||||||
('pk', _('Polish')),
|
('pk', _('Polish')),
|
||||||
|
('tr', _('Turkish')),
|
||||||
]
|
]
|
||||||
|
|
||||||
# Currencies available for use
|
# Currencies available for use
|
||||||
@ -491,10 +505,15 @@ CRISPY_TEMPLATE_PACK = 'bootstrap3'
|
|||||||
# Use database transactions when importing / exporting data
|
# Use database transactions when importing / exporting data
|
||||||
IMPORT_EXPORT_USE_TRANSACTIONS = True
|
IMPORT_EXPORT_USE_TRANSACTIONS = True
|
||||||
|
|
||||||
|
BACKUP_DIR = get_setting(
|
||||||
|
'INVENTREE_BACKUP_DIR',
|
||||||
|
CONFIG.get('backup_dir', tempfile.gettempdir()),
|
||||||
|
)
|
||||||
|
|
||||||
# Settings for dbbsettings app
|
# Settings for dbbsettings app
|
||||||
DBBACKUP_STORAGE = 'django.core.files.storage.FileSystemStorage'
|
DBBACKUP_STORAGE = 'django.core.files.storage.FileSystemStorage'
|
||||||
DBBACKUP_STORAGE_OPTIONS = {
|
DBBACKUP_STORAGE_OPTIONS = {
|
||||||
'location': CONFIG.get('backup_dir', tempfile.gettempdir()),
|
'location': BACKUP_DIR,
|
||||||
}
|
}
|
||||||
|
|
||||||
# Internal IP addresses allowed to see the debug toolbar
|
# Internal IP addresses allowed to see the debug toolbar
|
||||||
|
@ -107,13 +107,6 @@ static_root: '../inventree_static'
|
|||||||
# If unspecified, the local user's temp directory will be used
|
# If unspecified, the local user's temp directory will be used
|
||||||
#backup_dir: '/home/inventree/backup/'
|
#backup_dir: '/home/inventree/backup/'
|
||||||
|
|
||||||
# Sentry.io integration
|
|
||||||
# If you have a sentry.io account, it can be used to log server errors
|
|
||||||
# Ensure sentry_sdk is installed by running 'pip install sentry-sdk'
|
|
||||||
sentry:
|
|
||||||
enabled: False
|
|
||||||
# dsn: add-your-sentry-dsn-here
|
|
||||||
|
|
||||||
# LaTeX report rendering
|
# LaTeX report rendering
|
||||||
# InvenTree uses the django-tex plugin to enable LaTeX report rendering
|
# InvenTree uses the django-tex plugin to enable LaTeX report rendering
|
||||||
# Ref: https://pypi.org/project/django-tex/
|
# Ref: https://pypi.org/project/django-tex/
|
||||||
|
Loading…
Reference in New Issue
Block a user