From 74d3e28e8ed75fb2faa24f4cc7f4969dcc60b396 Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Sat, 6 Jul 2024 14:19:58 +0200 Subject: [PATCH] Ensure schema generation is more stable (#7562) * Enable passing of enviroment variables * keep server url static see #6882 (3) * keep plugin api out of schema see #6882 (1) * Add kwarg to override db settings * keep currencies stable for schema generation see #6882 (2) * use str instead of bool * add version bump --- .../InvenTree/InvenTree/api_version.py | 5 +++- src/backend/InvenTree/common/currency.py | 4 +++- src/backend/InvenTree/common/settings.py | 9 ++++++- tasks.py | 24 ++++++++++++++++--- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/backend/InvenTree/InvenTree/api_version.py b/src/backend/InvenTree/InvenTree/api_version.py index dfbcdc521e..a4b8b47cfe 100644 --- a/src/backend/InvenTree/InvenTree/api_version.py +++ b/src/backend/InvenTree/InvenTree/api_version.py @@ -1,11 +1,14 @@ """InvenTree API version information.""" # InvenTree API version -INVENTREE_API_VERSION = 211 +INVENTREE_API_VERSION = 212 """Increment this API version number whenever there is a significant change to the API that any clients need to know about.""" INVENTREE_API_TEXT = """ +v212 - 2024-07-06 : https://github.com/inventree/InvenTree/pull/7562 + - Makes API generation more robust (no functional changes) + v211 - 2024-06-26 : https://github.com/inventree/InvenTree/pull/6911 - Adds API endpoints for managing data import and export diff --git a/src/backend/InvenTree/common/currency.py b/src/backend/InvenTree/common/currency.py index 5590505f12..1769cf8c36 100644 --- a/src/backend/InvenTree/common/currency.py +++ b/src/backend/InvenTree/common/currency.py @@ -59,7 +59,9 @@ def currency_codes() -> list: """Returns the current currency codes.""" from common.settings import get_global_setting - codes = get_global_setting('CURRENCY_CODES', create=False).strip() + codes = get_global_setting( + 'CURRENCY_CODES', create=False, enviroment_key='INVENTREE_CURRENCY_CODES' + ).strip() if not codes: codes = currency_codes_default_list() diff --git a/src/backend/InvenTree/common/settings.py b/src/backend/InvenTree/common/settings.py index 6788b427e2..08b8b6be38 100644 --- a/src/backend/InvenTree/common/settings.py +++ b/src/backend/InvenTree/common/settings.py @@ -1,10 +1,17 @@ """User-configurable settings for the common app.""" +from os import environ -def get_global_setting(key, backup_value=None, **kwargs): + +def get_global_setting(key, backup_value=None, enviroment_key=None, **kwargs): """Return the value of a global setting using the provided key.""" from common.models import InvenTreeSetting + if enviroment_key: + value = environ.get(enviroment_key) + if value: + return value + if backup_value is not None: kwargs['backup_value'] = backup_value diff --git a/tasks.py b/tasks.py index 94fc91485f..fe6dd3a8c9 100644 --- a/tasks.py +++ b/tasks.py @@ -136,17 +136,20 @@ def managePyPath(): return managePyDir().joinpath('manage.py') -def manage(c, cmd, pty: bool = False): +def manage(c, cmd, pty: bool = False, env=None): """Runs a given command against django's "manage.py" script. Args: c: Command line context. cmd: Django command to run. pty (bool, optional): Run an interactive session. Defaults to False. + env (dict, optional): Environment variables to pass to the command. Defaults to None. """ + env = env or {} c.run( 'cd "{path}" && python3 manage.py {cmd}'.format(path=managePyDir(), cmd=cmd), pty=pty, + env=env, ) @@ -1020,9 +1023,12 @@ def setup_test(c, ignore_update=False, dev=False, path='inventree-demo-dataset') help={ 'filename': "Output filename (default = 'schema.yml')", 'overwrite': 'Overwrite existing files without asking first (default = off/False)', + 'no_default': 'Do not use default settings for schema (default = off/False)', } ) -def schema(c, filename='schema.yml', overwrite=False, ignore_warnings=False): +def schema( + c, filename='schema.yml', overwrite=False, ignore_warnings=False, no_default=False +): """Export current API schema.""" check_file_existance(filename, overwrite) @@ -1035,7 +1041,19 @@ def schema(c, filename='schema.yml', overwrite=False, ignore_warnings=False): if not ignore_warnings: cmd += ' --fail-on-warn' - manage(c, cmd, pty=True) + envs = {} + if not no_default: + envs['INVENTREE_SITE_URL'] = ( + 'http://localhost:8000' # Default site URL - to ensure server field is stable + ) + envs['INVENTREE_PLUGINS_ENABLED'] = ( + 'False' # Disable plugins to ensure they are kep out of schema + ) + envs['INVENTREE_CURRENCY_CODES'] = ( + 'AUD,CNY,EUR,USD' # Default currency codes to ensure they are stable + ) + + manage(c, cmd, pty=True, env=envs) assert os.path.exists(filename)