mirror of
https://github.com/inventree/InvenTree.git
synced 2025-08-10 05:40:55 +00:00
Refactor get_custom_classes
- Add StatusCode.custom_values method
This commit is contained in:
@@ -53,37 +53,22 @@ def get_custom_classes(
|
|||||||
if not include_custom:
|
if not include_custom:
|
||||||
return discovered_classes
|
return discovered_classes
|
||||||
|
|
||||||
# Gather DB settings
|
|
||||||
from common.models import InvenTreeCustomUserStateModel
|
|
||||||
|
|
||||||
custom_db_states = {}
|
|
||||||
custom_db_mdls = {}
|
|
||||||
for item in list(InvenTreeCustomUserStateModel.objects.all()):
|
|
||||||
if not custom_db_states.get(item.reference_status):
|
|
||||||
custom_db_states[item.reference_status] = []
|
|
||||||
custom_db_states[item.reference_status].append(item)
|
|
||||||
custom_db_mdls[item.model.app_label] = item.reference_status
|
|
||||||
custom_db_mdls_keys = custom_db_mdls.keys()
|
|
||||||
|
|
||||||
states = {}
|
states = {}
|
||||||
|
|
||||||
for cls in discovered_classes:
|
for cls in discovered_classes:
|
||||||
tag = cls.tag()
|
name = cls.__name__
|
||||||
states[tag] = cls
|
states[name] = cls
|
||||||
if custom_db_mdls and tag in custom_db_mdls_keys:
|
|
||||||
data = [(str(m.name), (m.value, m.label, m.color)) for m in states[tag]]
|
|
||||||
data_keys = [i[0] for i in data]
|
|
||||||
|
|
||||||
# Extent with non present tags
|
data = [(str(m.name), (m.value, m.label, m.color)) for m in cls]
|
||||||
for entry in custom_db_states[custom_db_mdls[tag]]:
|
|
||||||
ref_name = str(entry.name.upper().replace(' ', ''))
|
labels = [str(item[0]) for item in data]
|
||||||
if ref_name not in data_keys:
|
|
||||||
data += [
|
for item in cls.custom_values():
|
||||||
(
|
label = str(item.name)
|
||||||
str(entry.name.upper().replace(' ', '')),
|
if label not in labels and label not in cls.labels():
|
||||||
(entry.key, entry.label, entry.color),
|
data += [(str(item.name), (item.key, item.label, item.color))]
|
||||||
)
|
|
||||||
]
|
# Re-assemble the enum
|
||||||
|
states[name] = base_class(name, data)
|
||||||
|
|
||||||
# Re-assemble the enum
|
|
||||||
states[tag] = base_class(f'{tag.capitalize()}Status', data)
|
|
||||||
return states.values()
|
return states.values()
|
||||||
|
@@ -103,9 +103,22 @@ class StatusCode(BaseEnum):
|
|||||||
return isinstance(value.value, int)
|
return isinstance(value.value, int)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def values(cls, key=None):
|
def custom_values(cls):
|
||||||
|
"""Return all user-defined custom values for this status class."""
|
||||||
|
from common.models import InvenTreeCustomUserStateModel
|
||||||
|
|
||||||
|
try:
|
||||||
|
return InvenTreeCustomUserStateModel.objects.filter(
|
||||||
|
reference_status=cls.__name__
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
return []
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def values(cls, key=None, custom=True):
|
||||||
"""Return a dict representation containing all required information."""
|
"""Return a dict representation containing all required information."""
|
||||||
elements = [itm for itm in cls if cls._is_element(itm.name)]
|
elements = [itm for itm in cls if cls._is_element(itm.name)]
|
||||||
|
|
||||||
if key is None:
|
if key is None:
|
||||||
return elements
|
return elements
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user