mirror of
https://github.com/inventree/InvenTree.git
synced 2026-04-28 13:54:25 +00:00
[setup] Database config (#11813)
* Adjust config template file * Check UPPERCASE value as backup * fix typing issue * ignore ty issues for now Co-authored-by: Copilot <copilot@github.com> --------- Co-authored-by: Matthias Mair <code@mjmair.com> Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
+3
-2
@@ -109,12 +109,13 @@ unresolved-reference="ignore" # 21 # see https://github.com/astral-sh/ty/issues
|
|||||||
unresolved-attribute="ignore" # 505 # need Plugin Mixin typing
|
unresolved-attribute="ignore" # 505 # need Plugin Mixin typing
|
||||||
call-non-callable="ignore" # 8 ##
|
call-non-callable="ignore" # 8 ##
|
||||||
invalid-assignment="ignore" # 17 # need to wait for better django field stubs
|
invalid-assignment="ignore" # 17 # need to wait for better django field stubs
|
||||||
invalid-method-override="ignore" # 99
|
# invalid-method-override="ignore" # 99
|
||||||
invalid-return-type="ignore" # 22 ##
|
invalid-return-type="ignore" # 22 ##
|
||||||
possibly-missing-attribute="ignore" # 25 # https://github.com/astral-sh/ty/issues/164
|
# possibly-missing-attribute="ignore" # 25 # https://github.com/astral-sh/ty/issues/164
|
||||||
unknown-argument="ignore" # 3 # need to wait for better django field stubs
|
unknown-argument="ignore" # 3 # need to wait for better django field stubs
|
||||||
invalid-argument-type="ignore" # 49
|
invalid-argument-type="ignore" # 49
|
||||||
no-matching-overload="ignore" # 3 # need to wait for betterdjango field stubs
|
no-matching-overload="ignore" # 3 # need to wait for betterdjango field stubs
|
||||||
|
possibly-unbound-attribute="ignore" # 21
|
||||||
|
|
||||||
[tool.coverage.run]
|
[tool.coverage.run]
|
||||||
source = ["src/backend/InvenTree", "InvenTree"]
|
source = ["src/backend/InvenTree", "InvenTree"]
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import shutil
|
|||||||
import string
|
import string
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
logger = logging.getLogger('inventree')
|
logger = logging.getLogger('inventree')
|
||||||
CONFIG_DATA = None
|
CONFIG_DATA = None
|
||||||
@@ -254,6 +254,24 @@ def do_typecast(value, type, var_name=None):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def get_config_value(config_key: str) -> Optional[Any]:
|
||||||
|
"""Helper function to retrieve a configuration value from the config file."""
|
||||||
|
cfg_data = load_config_data()
|
||||||
|
|
||||||
|
result = None
|
||||||
|
|
||||||
|
# Hack to allow 'path traversal' in configuration file
|
||||||
|
for key in config_key.strip().split('.'):
|
||||||
|
if type(cfg_data) is not dict or key not in cfg_data:
|
||||||
|
result = None
|
||||||
|
break
|
||||||
|
|
||||||
|
result = cfg_data[key]
|
||||||
|
cfg_data = cfg_data[key]
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def get_setting(env_var=None, config_key=None, default_value=None, typecast=None):
|
def get_setting(env_var=None, config_key=None, default_value=None, typecast=None):
|
||||||
"""Helper function for retrieving a configuration setting value.
|
"""Helper function for retrieving a configuration setting value.
|
||||||
|
|
||||||
@@ -291,18 +309,7 @@ def get_setting(env_var=None, config_key=None, default_value=None, typecast=None
|
|||||||
|
|
||||||
# Next, try to load from configuration file
|
# Next, try to load from configuration file
|
||||||
if config_key is not None:
|
if config_key is not None:
|
||||||
cfg_data = load_config_data()
|
result = get_config_value(config_key)
|
||||||
|
|
||||||
result = None
|
|
||||||
|
|
||||||
# Hack to allow 'path traversal' in configuration file
|
|
||||||
for key in config_key.strip().split('.'):
|
|
||||||
if type(cfg_data) is not dict or key not in cfg_data:
|
|
||||||
result = None
|
|
||||||
break
|
|
||||||
|
|
||||||
result = cfg_data[key]
|
|
||||||
cfg_data = cfg_data[key]
|
|
||||||
|
|
||||||
if result is not None:
|
if result is not None:
|
||||||
set_metadata('yaml')
|
set_metadata('yaml')
|
||||||
|
|||||||
@@ -4,27 +4,38 @@ from pathlib import Path
|
|||||||
|
|
||||||
import structlog
|
import structlog
|
||||||
|
|
||||||
from InvenTree.config import get_boolean_setting, get_setting
|
from InvenTree.config import get_boolean_setting, get_config_value, get_setting
|
||||||
|
|
||||||
logger = structlog.get_logger('inventree')
|
logger = structlog.get_logger('inventree')
|
||||||
|
|
||||||
|
|
||||||
def get_db_backend():
|
def get_db_backend():
|
||||||
"""Return the database backend configuration."""
|
"""Return the database backend configuration."""
|
||||||
|
# For the core database configuration values, we test for UPPERCASE configuration values as a backup,
|
||||||
|
# due to legacy reasons (original config files were uppercase,
|
||||||
|
# but we moved to lowercase for consistency with other settings.
|
||||||
|
|
||||||
db_config = {
|
db_config = {
|
||||||
'ENGINE': get_setting(
|
'ENGINE': get_setting(
|
||||||
'INVENTREE_DB_ENGINE', 'database.engine', '', typecast=str
|
'INVENTREE_DB_ENGINE', 'database.engine', '', typecast=str
|
||||||
),
|
)
|
||||||
'NAME': get_setting('INVENTREE_DB_NAME', 'database.name', '', typecast=str),
|
or get_config_value('database.ENGINE'),
|
||||||
'USER': get_setting('INVENTREE_DB_USER', 'database.user', '', typecast=str),
|
'NAME': get_setting('INVENTREE_DB_NAME', 'database.name', '', typecast=str)
|
||||||
|
or get_config_value('database.NAME'),
|
||||||
|
'USER': get_setting('INVENTREE_DB_USER', 'database.user', '', typecast=str)
|
||||||
|
or get_config_value('database.USER'),
|
||||||
'PASSWORD': get_setting(
|
'PASSWORD': get_setting(
|
||||||
'INVENTREE_DB_PASSWORD', 'database.password', '', typecast=str
|
'INVENTREE_DB_PASSWORD', 'database.password', '', typecast=str
|
||||||
),
|
)
|
||||||
'HOST': get_setting('INVENTREE_DB_HOST', 'database.host', '', typecast=str),
|
or get_config_value('database.PASSWORD'),
|
||||||
'PORT': get_setting('INVENTREE_DB_PORT', 'database.port', '', typecast=str),
|
'HOST': get_setting('INVENTREE_DB_HOST', 'database.host', '', typecast=str)
|
||||||
|
or get_config_value('database.HOST'),
|
||||||
|
'PORT': get_setting('INVENTREE_DB_PORT', 'database.port', '', typecast=str)
|
||||||
|
or get_config_value('database.PORT'),
|
||||||
'OPTIONS': get_setting(
|
'OPTIONS': get_setting(
|
||||||
'INVENTREE_DB_OPTIONS', 'database.options', {}, typecast=dict
|
'INVENTREE_DB_OPTIONS', 'database.options', {}, typecast=dict
|
||||||
)
|
)
|
||||||
|
or get_config_value('database.OPTIONS')
|
||||||
or {},
|
or {},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,15 +18,15 @@
|
|||||||
# TO MAINTAINERS: Do not change database strings
|
# TO MAINTAINERS: Do not change database strings
|
||||||
database:
|
database:
|
||||||
# --- Available options: ---
|
# --- Available options: ---
|
||||||
# ENGINE: Database engine. Selection from:
|
# engine: Database engine. Selection from:
|
||||||
# - mysql
|
# - mysql
|
||||||
# - postgresql
|
# - postgresql
|
||||||
# - sqlite3
|
# - sqlite3
|
||||||
# NAME: Database name
|
# name: Database name
|
||||||
# USER: Database username (if required)
|
# user: Database username (if required)
|
||||||
# PASSWORD: Database password (if required)
|
# password: Database password (if required)
|
||||||
# HOST: Database host address (if required)
|
# host: Database host address (if required)
|
||||||
# PORT: Database host port (if required)
|
# port: Database host port (if required)
|
||||||
|
|
||||||
# Base URL for the InvenTree server (or use the environment variable INVENTREE_SITE_URL)
|
# Base URL for the InvenTree server (or use the environment variable INVENTREE_SITE_URL)
|
||||||
# site_url: 'http://localhost:8000'
|
# site_url: 'http://localhost:8000'
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import json
|
|||||||
import re
|
import re
|
||||||
import warnings
|
import warnings
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from distutils.sysconfig import get_python_lib
|
|
||||||
from importlib.metadata import PackageNotFoundError, metadata
|
from importlib.metadata import PackageNotFoundError, metadata
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
@@ -528,6 +527,8 @@ class InvenTreePlugin(VersionMixin, MixinBase, MetaBase):
|
|||||||
@mark_final
|
@mark_final
|
||||||
def is_editable(cls):
|
def is_editable(cls):
|
||||||
"""Returns if the current part is editable."""
|
"""Returns if the current part is editable."""
|
||||||
|
from distutils.sysconfig import get_python_lib
|
||||||
|
|
||||||
pkg_name = cls.__name__.split('.')[0]
|
pkg_name = cls.__name__.split('.')[0]
|
||||||
dist_info = list(Path(get_python_lib()).glob(f'{pkg_name}-*.dist-info'))
|
dist_info = list(Path(get_python_lib()).glob(f'{pkg_name}-*.dist-info'))
|
||||||
return bool(len(dist_info) == 1)
|
return bool(len(dist_info) == 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user