diff --git a/apps/applications/api/mixin.py b/apps/applications/api/mixin.py index cd603199b..ff939bf0b 100644 --- a/apps/applications/api/mixin.py +++ b/apps/applications/api/mixin.py @@ -12,6 +12,7 @@ class SerializeApplicationToTreeNodeMixin: def filter_organizations(applications): organization_ids = set(applications.values_list('org_id', flat=True)) organizations = [Organization.get_instance(org_id) for org_id in organization_ids] + organizations.sort(key=lambda x: x.name) return organizations @staticmethod diff --git a/apps/applications/models/application.py b/apps/applications/models/application.py index a7964451d..dafad9c55 100644 --- a/apps/applications/models/application.py +++ b/apps/applications/models/application.py @@ -16,17 +16,16 @@ class ApplicationTreeNodeMixin: category: str @classmethod - def create_choice_node(cls, c, pid, tp, opened=False, counts=None, + def create_choice_node(cls, c, id_, pid, tp, opened=False, counts=None, show_empty=True, show_count=True): - count = counts.get(c.name, 0) + count = counts.get(c.value, 0) if count == 0 and not show_empty: return None - name = c.name label = c.label if count is not None and show_count: label = '{} ({})'.format(label, count) data = { - 'id': name, + 'id': id_, 'name': label, 'title': label, 'pId': pid, @@ -65,8 +64,9 @@ class ApplicationTreeNodeMixin: nodes = [] categories = const.AppType.category_types_mapper().keys() for category in categories: + i = root_node.id + '_' + category.value node = cls.create_choice_node( - category, pid=root_node.id, tp='category', + category, i, pid=root_node.id, tp='category', counts=counts, opened=True, show_empty=show_empty, show_count=show_count ) @@ -76,13 +76,15 @@ class ApplicationTreeNodeMixin: return nodes @classmethod - def create_types_tree_nodes(cls, counts, show_empty=True, show_count=True): + def create_types_tree_nodes(cls, root_node, counts, show_empty=True, show_count=True): nodes = [] type_category_mapper = const.AppType.type_category_mapper() for tp in const.AppType.type_category_mapper().keys(): category = type_category_mapper.get(tp) + pid = root_node.id + '_' + category.value + i = root_node.id + '_' + tp.value node = cls.create_choice_node( - tp, pid=category.name, tp='type', counts=counts, + tp, i, pid, tp='type', counts=counts, show_empty=show_empty, show_count=show_count ) if not node: @@ -93,14 +95,14 @@ class ApplicationTreeNodeMixin: @staticmethod def get_tree_node_counts(queryset): counts = {'applications': queryset.count()} - category_counts = queryset.values('category') \ - .annotate(count=Count('id')) \ + category_counts = queryset.annotate(count=Count('id'))\ + .values('category', 'count') \ .order_by() for item in category_counts: counts[item['category']] = item['count'] - type_counts = queryset.values('type') \ - .annotate(count=Count('id')) \ + type_counts = queryset.annotate(count=Count('id')) \ + .values('type', 'count') \ .order_by() for item in type_counts: counts[item['type']] = item['count'] @@ -108,7 +110,7 @@ class ApplicationTreeNodeMixin: @classmethod def create_tree_nodes(cls, queryset, root_node=None, show_empty=True, show_count=True): - counts = cls.get_tree_node_counts(queryset) if show_count else {} + counts = cls.get_tree_node_counts(queryset) tree_nodes = [] # 根节点有可能是组织名称 @@ -124,15 +126,17 @@ class ApplicationTreeNodeMixin: # 类型的节点 tree_nodes += cls.create_types_tree_nodes( - counts, show_empty=show_empty, show_count=show_count + root_node, counts, show_empty=show_empty, + show_count=show_count ) # 应用的节点 for app in queryset: - tree_nodes.append(app.as_tree_node()) + pid = root_node.id + '_' + app.type + tree_nodes.append(app.as_tree_node(pid)) return tree_nodes - def as_tree_node(self): + def as_tree_node(self, pid): icon_skin_category_mapper = { 'remote_app': 'chrome', 'db': 'database', @@ -143,7 +147,7 @@ class ApplicationTreeNodeMixin: 'id': str(self.id), 'name': self.name, 'title': self.name, - 'pId': str(self.type), + 'pId': pid, 'isParent': False, 'open': False, 'iconSkin': icon_skin, diff --git a/apps/orgs/models.py b/apps/orgs/models.py index f50c686e3..efbee20b2 100644 --- a/apps/orgs/models.py +++ b/apps/orgs/models.py @@ -236,7 +236,7 @@ class Organization(models.Model): def as_tree_node(self, pid, opened=True): node = TreeNode(**{ - 'id': self.id, + 'id': str(self.id), 'name': self.name, 'title': self.name, 'pId': pid,