perf: change cateogory data strucature

pull/8931/head
ibuler 2022-09-21 20:13:28 +08:00
parent ea1cb158b5
commit 21a60bf55e
2 changed files with 10 additions and 29 deletions

View File

@ -55,9 +55,9 @@ class AllTypes(ChoicesMixin, metaclass=IncludesTextChoicesMeta):
categories = []
for category, tps in cls.category_types():
category_data = {
'id': category.value,
'name': category.label,
'children': [cls.serialize_type(category, tp, with_constraints) for tp in tps]
'value': category.value,
'label': category.label,
'types': [cls.serialize_type(category, tp, with_constraints) for tp in tps]
}
categories.append(category_data)
return categories
@ -65,8 +65,8 @@ class AllTypes(ChoicesMixin, metaclass=IncludesTextChoicesMeta):
@classmethod
def serialize_type(cls, category, tp, with_constraints=True):
data = {
'id': tp.value,
'name': tp.label,
'value': tp.value,
'label': tp.label,
'category': category,
}
@ -86,25 +86,6 @@ class AllTypes(ChoicesMixin, metaclass=IncludesTextChoicesMeta):
(Category.CLOUD, CloudTypes)
)
@classmethod
def grouped_choices(cls):
grouped_types = [(str(ca), tp.choices) for ca, tp in cls.category_types()]
return grouped_types
@classmethod
def grouped_choices_to_objs(cls):
choices = cls.serialize_to_objs(Category.choices)
mapper = dict(cls.grouped_choices())
for choice in choices:
children = cls.serialize_to_objs(mapper[choice['value']])
choice['children'] = children
return choices
@staticmethod
def serialize_to_objs(choices):
title = ['value', 'display_name']
return [dict(zip(title, choice)) for choice in choices]
@staticmethod
def choice_to_node(choice, pid, opened=True, is_parent=True, meta=None):
node = TreeNode(**{

View File

@ -3,13 +3,13 @@ from django.utils.translation import gettext_lazy as _
class TypeSerializer(serializers.Serializer):
id = serializers.CharField(max_length=64, required=False, allow_blank=True, label=_('id'))
name = serializers.CharField(max_length=64, required=False, allow_blank=True, label=_('Name'))
label = serializers.CharField(max_length=64, required=False, allow_blank=True, label=_('Label'))
value = serializers.CharField(max_length=64, required=False, allow_blank=True, label=_('Value'))
category = serializers.CharField(max_length=64, required=False, allow_blank=True, label=_('Category'))
constraints = serializers.JSONField(required=False, allow_null=True, label=_('Constraints'))
class CategorySerializer(serializers.Serializer):
id = serializers.CharField(max_length=64, required=False, allow_blank=True, label=_('id'))
name = serializers.CharField(max_length=64, required=False, allow_blank=True, label=_('Name'))
children = TypeSerializer(many=True, required=False, label=_('Children'), read_only=True)
label = serializers.CharField(max_length=64, required=False, allow_blank=True, label=_('Label'))
value = serializers.CharField(max_length=64, required=False, allow_blank=True, label=_('Value'))
types = TypeSerializer(many=True, required=False, label=_('Types'), read_only=True)