2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-12-16 17:28:11 +00:00

[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%
This commit is contained in:
Oliver
2025-12-14 19:08:52 +11:00
committed by GitHub
parent 2a20eeb033
commit 9c6d16baba

View File

@@ -129,11 +129,24 @@ class AllStatusViews(StatusView):
# Find all inherited status classes # Find all inherited status classes
status_classes = inheritors(StatusCode) status_classes = inheritors(StatusCode)
for cls in status_classes: # Pre-fetch all custom values from the database
cls_data = {'status_class': cls.__name__, 'values': cls.dict()} # This reduces the number of queries required
from common.models import InvenTreeCustomUserStateModel
# Extend with custom values custom_states_map = {}
for item in cls.custom_values(): 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) label = str(item.name)
if label not in cls_data['values']: if label not in cls_data['values']:
cls_data['values'][label] = { cls_data['values'][label] = {