diff --git a/src/backend/InvenTree/InvenTree/context.py b/src/backend/InvenTree/InvenTree/context.py index bbdf5f6eb6..3a3c987d37 100644 --- a/src/backend/InvenTree/InvenTree/context.py +++ b/src/backend/InvenTree/InvenTree/context.py @@ -1,9 +1,9 @@ """Provides extra global data to all templates.""" +import InvenTree.helpers import InvenTree.helpers_email import InvenTree.ready import InvenTree.status -from generic.states.custom import get_custom_classes from users.models import RuleSet, check_user_role @@ -50,10 +50,15 @@ def status_codes(request): # Do not duplicate efforts return {} + from generic.states import StatusCode + request._inventree_status_codes = True + get_custom = InvenTree.ready.isRebuildingData() is False + return { - cls.__name__: cls.template_context() for cls in get_custom_classes(get_custom) + cls.__name__: cls.template_context(custom=get_custom) + for cls in InvenTree.helpers.inheritors(StatusCode) } diff --git a/src/backend/InvenTree/generic/states/custom.py b/src/backend/InvenTree/generic/states/custom.py index f4e4f68757..da1dbe743f 100644 --- a/src/backend/InvenTree/generic/states/custom.py +++ b/src/backend/InvenTree/generic/states/custom.py @@ -7,45 +7,7 @@ from .states import ColorEnum, StatusCode def get_custom_status_labels(include_custom: bool = True): """Return a dict of custom status labels.""" - return {cls.tag(): cls for cls in get_custom_classes(include_custom)} - - -def get_status_api_response(base_class=StatusCode, prefix=None): - """Return a dict of status classes (custom and class defined). - - Args: - base_class: The base class to search for subclasses. - prefix: A list of strings to prefix the class names with. - """ - # TODO: Come back and fix this... - # return { - # '__'.join([*(prefix or []), k.__name__]): { - # 'class': k.__name__, - # 'values': k.dict(), - # } - # for k in get_custom_classes(base_class=base_class, subclass=False) - # } - - classes = inheritors(base_class, subclasses=True) - - data = {} - - for cls in classes: - name = cls.__name__ - data[name] = {'class': name, 'values': cls.dict()} - - # Extend with custom values - for item in cls.custom_values(): - label = str(item.name) - if label not in data[name]['values']: - data[name]['values'][label] = { - 'color': item.color, - 'key': item.key, - 'label': item.label, - 'name': item.name, - } - - return data + return {cls.tag(): cls for cls in inheritors(StatusCode)} def state_color_mappings(): @@ -55,7 +17,7 @@ def state_color_mappings(): def state_reference_mappings(): """Return a list of custom user state references.""" - classes = get_custom_classes(include_custom=False) + classes = inheritors(StatusCode) return [(a.__name__, a.__name__) for a in sorted(classes, key=lambda x: x.__name__)] @@ -64,51 +26,3 @@ def get_logical_value(value, model: str): from common.models import InvenTreeCustomUserStateModel return InvenTreeCustomUserStateModel.objects.get(key=value, model__model=model) - - -def get_custom_classes( - include_custom: bool = True, base_class=StatusCode, subclass=False -): - """Return a dict of status classes (custom and class defined).""" - discovered_classes = inheritors(base_class, subclass) - - if not include_custom: - return discovered_classes - - states = {} - - print('get_custom_classes:', len(discovered_classes)) - - for cls in discovered_classes: - name = cls.__name__ - # states[name] = - - # print("-", cls) - - if name in states: - continue - - # states[name] = cls - - data = [ - (str(m.name), (m.value, m.label, m.color)) - for m in cls - if cls._is_element(m.name) - ] - # values = cls.values() - - labels = [item[0] for item in data] - - for item in cls.custom_values(): - label = str(item.name) - if label not in labels: - setattr(cls, label, (item.key, item.label, item.color)) - # data += [(label, (item.key, item.label, item.color))] - # values += [(str(item.name), (item.key, item.label, item.color))] - - # Re-assemble the enum - # states[name] = base_class(name, values) - # states[name] = base_class(name, data) - states[name] = cls - - return states.values() diff --git a/src/backend/InvenTree/generic/states/states.py b/src/backend/InvenTree/generic/states/states.py index 207bf050ee..678d78afb8 100644 --- a/src/backend/InvenTree/generic/states/states.py +++ b/src/backend/InvenTree/generic/states/states.py @@ -189,23 +189,41 @@ class StatusCode(BaseEnum): return filtered.label @classmethod - def dict(cls, key=None): + def dict(cls, key=None, custom=None): """Return a dict representation containing all required information.""" - return { + data = { x.name: {'color': x.color, 'key': x.value, 'label': x.label, 'name': x.name} for x in cls.values(key) } - @classmethod - def list(cls): - """Return the StatusCode options as a list of mapped key / value items.""" - return list(cls.dict().values()) + if custom: + try: + for item in cls.custom_values(): + if item.name not in data: + data[item.name] = { + 'color': item.color, + 'key': item.key, + 'label': item.label, + 'name': item.name, + } + except Exception: + pass + + return data @classmethod - def template_context(cls): + def list(cls, custom=True): + """Return the StatusCode options as a list of mapped key / value items.""" + return list(cls.dict(custom=custom).values()) + + @classmethod + def template_context(cls, custom=True): """Return a dict representation containing all required information for templates.""" - ret = {x.name: x.value for x in cls.values()} - ret['list'] = cls.list() + data = cls.dict(custom=custom) + + ret = {x['name']: x['key'] for x in data.values()} + + ret['list'] = list(data.values()) return ret