From 9c6d16baba79e6befb6dbe584612ff92f5801d42 Mon Sep 17 00:00:00 2001 From: Oliver Date: Sun, 14 Dec 2025 19:08:52 +1100 Subject: [PATCH] [refactor] Generic status API (#11009) * Fix docs formatting * [refactor] cache custom states - Generic state API endpoint executed query for each state type - We can run a single database query and cache these in memory - Reduces query time by ~50% --- src/backend/InvenTree/generic/states/api.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/backend/InvenTree/generic/states/api.py b/src/backend/InvenTree/generic/states/api.py index 925fc772ce..4047c69161 100644 --- a/src/backend/InvenTree/generic/states/api.py +++ b/src/backend/InvenTree/generic/states/api.py @@ -129,11 +129,24 @@ class AllStatusViews(StatusView): # Find all inherited status classes status_classes = inheritors(StatusCode) - for cls in status_classes: - cls_data = {'status_class': cls.__name__, 'values': cls.dict()} + # Pre-fetch all custom values from the database + # This reduces the number of queries required + from common.models import InvenTreeCustomUserStateModel - # Extend with custom values - for item in cls.custom_values(): + custom_states_map = {} + for state in InvenTreeCustomUserStateModel.objects.all(): + key = state.reference_status + if key not in custom_states_map: + custom_states_map[key] = [] + custom_states_map[key].append(state) + + for cls in status_classes: + cls_data = {'status_class': cls.__name__, 'values': cls.dict(custom=False)} + + # Extend with custom values (from pre-cached queryset) + custom_states = custom_states_map.get(cls.__name__, []) + + for item in custom_states: label = str(item.name) if label not in cls_data['values']: cls_data['values'][label] = {