mirror of
https://github.com/inventree/InvenTree.git
synced 2025-08-09 21:30:54 +00:00
Code cleanup
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
"""Provides extra global data to all templates."""
|
"""Provides extra global data to all templates."""
|
||||||
|
|
||||||
|
import InvenTree.helpers
|
||||||
import InvenTree.helpers_email
|
import InvenTree.helpers_email
|
||||||
import InvenTree.ready
|
import InvenTree.ready
|
||||||
import InvenTree.status
|
import InvenTree.status
|
||||||
from generic.states.custom import get_custom_classes
|
|
||||||
from users.models import RuleSet, check_user_role
|
from users.models import RuleSet, check_user_role
|
||||||
|
|
||||||
|
|
||||||
@@ -50,10 +50,15 @@ def status_codes(request):
|
|||||||
# Do not duplicate efforts
|
# Do not duplicate efforts
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
from generic.states import StatusCode
|
||||||
|
|
||||||
request._inventree_status_codes = True
|
request._inventree_status_codes = True
|
||||||
|
|
||||||
get_custom = InvenTree.ready.isRebuildingData() is False
|
get_custom = InvenTree.ready.isRebuildingData() is False
|
||||||
|
|
||||||
return {
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -7,45 +7,7 @@ from .states import ColorEnum, StatusCode
|
|||||||
|
|
||||||
def get_custom_status_labels(include_custom: bool = True):
|
def get_custom_status_labels(include_custom: bool = True):
|
||||||
"""Return a dict of custom status labels."""
|
"""Return a dict of custom status labels."""
|
||||||
return {cls.tag(): cls for cls in get_custom_classes(include_custom)}
|
return {cls.tag(): cls for cls in inheritors(StatusCode)}
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def state_color_mappings():
|
def state_color_mappings():
|
||||||
@@ -55,7 +17,7 @@ def state_color_mappings():
|
|||||||
|
|
||||||
def state_reference_mappings():
|
def state_reference_mappings():
|
||||||
"""Return a list of custom user state references."""
|
"""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__)]
|
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
|
from common.models import InvenTreeCustomUserStateModel
|
||||||
|
|
||||||
return InvenTreeCustomUserStateModel.objects.get(key=value, model__model=model)
|
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()
|
|
||||||
|
@@ -189,23 +189,41 @@ class StatusCode(BaseEnum):
|
|||||||
return filtered.label
|
return filtered.label
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def dict(cls, key=None):
|
def dict(cls, key=None, custom=None):
|
||||||
"""Return a dict representation containing all required information."""
|
"""Return a dict representation containing all required information."""
|
||||||
return {
|
data = {
|
||||||
x.name: {'color': x.color, 'key': x.value, 'label': x.label, 'name': x.name}
|
x.name: {'color': x.color, 'key': x.value, 'label': x.label, 'name': x.name}
|
||||||
for x in cls.values(key)
|
for x in cls.values(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
@classmethod
|
if custom:
|
||||||
def list(cls):
|
try:
|
||||||
"""Return the StatusCode options as a list of mapped key / value items."""
|
for item in cls.custom_values():
|
||||||
return list(cls.dict().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
|
@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."""
|
"""Return a dict representation containing all required information for templates."""
|
||||||
ret = {x.name: x.value for x in cls.values()}
|
data = cls.dict(custom=custom)
|
||||||
ret['list'] = cls.list()
|
|
||||||
|
ret = {x['name']: x['key'] for x in data.values()}
|
||||||
|
|
||||||
|
ret['list'] = list(data.values())
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user