From fc3d1308887a764d2efc83c5cf3d2d1160da5f8e Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 5 Nov 2025 07:00:58 +1100 Subject: [PATCH] Refactor settings.py (#10766) - Move drf-spectacular settings into separate file - Cleanup settings.py core file --- .../InvenTree/setting/spectacular.py | 41 +++++++++++++++++ src/backend/InvenTree/InvenTree/settings.py | 44 ++----------------- 2 files changed, 45 insertions(+), 40 deletions(-) create mode 100644 src/backend/InvenTree/InvenTree/setting/spectacular.py diff --git a/src/backend/InvenTree/InvenTree/setting/spectacular.py b/src/backend/InvenTree/InvenTree/setting/spectacular.py new file mode 100644 index 0000000000..56fdbdc4fa --- /dev/null +++ b/src/backend/InvenTree/InvenTree/setting/spectacular.py @@ -0,0 +1,41 @@ +"""Configuration options for drf-spectacular.""" + +from InvenTree.version import inventreeApiVersion + + +def get_spectacular_settings(): + """Return configuration dictionary for drf-spectacular.""" + return { + 'TITLE': 'InvenTree API', + 'DESCRIPTION': 'API for InvenTree - the intuitive open source inventory management system', + 'LICENSE': { + 'name': 'MIT', + 'url': 'https://github.com/inventree/InvenTree/blob/master/LICENSE', + }, + 'EXTERNAL_DOCS': { + 'description': 'More information about InvenTree in the official docs', + 'url': 'https://docs.inventree.org', + }, + 'VERSION': str(inventreeApiVersion()), + 'SERVE_INCLUDE_SCHEMA': False, + 'SCHEMA_PATH_PREFIX': '/api/', + 'POSTPROCESSING_HOOKS': [ + 'drf_spectacular.hooks.postprocess_schema_enums', + 'InvenTree.schema.postprocess_required_nullable', + 'InvenTree.schema.postprocess_print_stats', + ], + 'ENUM_NAME_OVERRIDES': { + 'UserTypeEnum': 'users.models.UserProfile.UserType', + 'TemplateModelTypeEnum': 'report.models.ReportTemplateBase.ModelChoices', + 'AttachmentModelTypeEnum': 'common.models.Attachment.ModelChoices', + 'DataImportSessionModelTypeEnum': 'importer.models.DataImportSession.ModelChoices', + # Allauth + 'UnauthorizedStatus': [[401, 401]], + 'IsTrueEnum': [[True, True]], + }, + # oAuth2 + 'OAUTH2_FLOWS': ['authorizationCode', 'clientCredentials'], + 'OAUTH2_AUTHORIZATION_URL': '/o/authorize/', + 'OAUTH2_TOKEN_URL': '/o/token/', + 'OAUTH2_REFRESH_URL': '/o/revoke_token/', + } diff --git a/src/backend/InvenTree/InvenTree/settings.py b/src/backend/InvenTree/InvenTree/settings.py index 4b5ec58951..c79ce16293 100644 --- a/src/backend/InvenTree/InvenTree/settings.py +++ b/src/backend/InvenTree/InvenTree/settings.py @@ -34,15 +34,11 @@ from InvenTree.config import ( ) from InvenTree.ready import isInMainThread from InvenTree.sentry import default_sentry_dsn, init_sentry -from InvenTree.version import ( - checkMinPythonVersion, - inventreeApiVersion, - inventreeCommitHash, -) +from InvenTree.version import checkMinPythonVersion, inventreeCommitHash from users.oauth2_scopes import oauth2_scopes from . import config -from .setting import locales, markdown, storages +from .setting import locales, markdown, spectacular, storages try: import django_stubs_ext @@ -1483,40 +1479,8 @@ SESAME_MAX_AGE = 300 LOGIN_REDIRECT_URL = '/api/auth/login-redirect/' # Configuration for API schema generation / oAuth2 -SPECTACULAR_SETTINGS = { - 'TITLE': 'InvenTree API', - 'DESCRIPTION': 'API for InvenTree - the intuitive open source inventory management system', - 'LICENSE': { - 'name': 'MIT', - 'url': 'https://github.com/inventree/InvenTree/blob/master/LICENSE', - }, - 'EXTERNAL_DOCS': { - 'description': 'More information about InvenTree in the official docs', - 'url': 'https://docs.inventree.org', - }, - 'VERSION': str(inventreeApiVersion()), - 'SERVE_INCLUDE_SCHEMA': False, - 'SCHEMA_PATH_PREFIX': '/api/', - 'POSTPROCESSING_HOOKS': [ - 'drf_spectacular.hooks.postprocess_schema_enums', - 'InvenTree.schema.postprocess_required_nullable', - 'InvenTree.schema.postprocess_print_stats', - ], - 'ENUM_NAME_OVERRIDES': { - 'UserTypeEnum': 'users.models.UserProfile.UserType', - 'TemplateModelTypeEnum': 'report.models.ReportTemplateBase.ModelChoices', - 'AttachmentModelTypeEnum': 'common.models.Attachment.ModelChoices', - 'DataImportSessionModelTypeEnum': 'importer.models.DataImportSession.ModelChoices', - # Allauth - 'UnauthorizedStatus': [[401, 401]], - 'IsTrueEnum': [[True, True]], - }, - # oAuth2 - 'OAUTH2_FLOWS': ['authorizationCode', 'clientCredentials'], - 'OAUTH2_AUTHORIZATION_URL': '/o/authorize/', - 'OAUTH2_TOKEN_URL': '/o/token/', - 'OAUTH2_REFRESH_URL': '/o/revoke_token/', -} +SPECTACULAR_SETTINGS = spectacular.get_spectacular_settings() + OAUTH2_PROVIDER = { # default scopes 'SCOPES': oauth2_scopes,