mirror of https://github.com/jumpserver/jumpserver
perf: 修复授权应用树的问题
parent
d0e119fb50
commit
f031f4d560
|
@ -12,6 +12,7 @@ class SerializeApplicationToTreeNodeMixin:
|
||||||
def filter_organizations(applications):
|
def filter_organizations(applications):
|
||||||
organization_ids = set(applications.values_list('org_id', flat=True))
|
organization_ids = set(applications.values_list('org_id', flat=True))
|
||||||
organizations = [Organization.get_instance(org_id) for org_id in organization_ids]
|
organizations = [Organization.get_instance(org_id) for org_id in organization_ids]
|
||||||
|
organizations.sort(key=lambda x: x.name)
|
||||||
return organizations
|
return organizations
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -16,17 +16,16 @@ class ApplicationTreeNodeMixin:
|
||||||
category: str
|
category: str
|
||||||
|
|
||||||
@classmethod
|
@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):
|
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:
|
if count == 0 and not show_empty:
|
||||||
return None
|
return None
|
||||||
name = c.name
|
|
||||||
label = c.label
|
label = c.label
|
||||||
if count is not None and show_count:
|
if count is not None and show_count:
|
||||||
label = '{} ({})'.format(label, count)
|
label = '{} ({})'.format(label, count)
|
||||||
data = {
|
data = {
|
||||||
'id': name,
|
'id': id_,
|
||||||
'name': label,
|
'name': label,
|
||||||
'title': label,
|
'title': label,
|
||||||
'pId': pid,
|
'pId': pid,
|
||||||
|
@ -65,8 +64,9 @@ class ApplicationTreeNodeMixin:
|
||||||
nodes = []
|
nodes = []
|
||||||
categories = const.AppType.category_types_mapper().keys()
|
categories = const.AppType.category_types_mapper().keys()
|
||||||
for category in categories:
|
for category in categories:
|
||||||
|
i = root_node.id + '_' + category.value
|
||||||
node = cls.create_choice_node(
|
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,
|
counts=counts, opened=True, show_empty=show_empty,
|
||||||
show_count=show_count
|
show_count=show_count
|
||||||
)
|
)
|
||||||
|
@ -76,13 +76,15 @@ class ApplicationTreeNodeMixin:
|
||||||
return nodes
|
return nodes
|
||||||
|
|
||||||
@classmethod
|
@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 = []
|
nodes = []
|
||||||
type_category_mapper = const.AppType.type_category_mapper()
|
type_category_mapper = const.AppType.type_category_mapper()
|
||||||
for tp in const.AppType.type_category_mapper().keys():
|
for tp in const.AppType.type_category_mapper().keys():
|
||||||
category = type_category_mapper.get(tp)
|
category = type_category_mapper.get(tp)
|
||||||
|
pid = root_node.id + '_' + category.value
|
||||||
|
i = root_node.id + '_' + tp.value
|
||||||
node = cls.create_choice_node(
|
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
|
show_empty=show_empty, show_count=show_count
|
||||||
)
|
)
|
||||||
if not node:
|
if not node:
|
||||||
|
@ -93,14 +95,14 @@ class ApplicationTreeNodeMixin:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_tree_node_counts(queryset):
|
def get_tree_node_counts(queryset):
|
||||||
counts = {'applications': queryset.count()}
|
counts = {'applications': queryset.count()}
|
||||||
category_counts = queryset.values('category') \
|
category_counts = queryset.annotate(count=Count('id'))\
|
||||||
.annotate(count=Count('id')) \
|
.values('category', 'count') \
|
||||||
.order_by()
|
.order_by()
|
||||||
for item in category_counts:
|
for item in category_counts:
|
||||||
counts[item['category']] = item['count']
|
counts[item['category']] = item['count']
|
||||||
|
|
||||||
type_counts = queryset.values('type') \
|
type_counts = queryset.annotate(count=Count('id')) \
|
||||||
.annotate(count=Count('id')) \
|
.values('type', 'count') \
|
||||||
.order_by()
|
.order_by()
|
||||||
for item in type_counts:
|
for item in type_counts:
|
||||||
counts[item['type']] = item['count']
|
counts[item['type']] = item['count']
|
||||||
|
@ -108,7 +110,7 @@ class ApplicationTreeNodeMixin:
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_tree_nodes(cls, queryset, root_node=None, show_empty=True, show_count=True):
|
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 = []
|
tree_nodes = []
|
||||||
|
|
||||||
# 根节点有可能是组织名称
|
# 根节点有可能是组织名称
|
||||||
|
@ -124,15 +126,17 @@ class ApplicationTreeNodeMixin:
|
||||||
|
|
||||||
# 类型的节点
|
# 类型的节点
|
||||||
tree_nodes += cls.create_types_tree_nodes(
|
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:
|
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
|
return tree_nodes
|
||||||
|
|
||||||
def as_tree_node(self):
|
def as_tree_node(self, pid):
|
||||||
icon_skin_category_mapper = {
|
icon_skin_category_mapper = {
|
||||||
'remote_app': 'chrome',
|
'remote_app': 'chrome',
|
||||||
'db': 'database',
|
'db': 'database',
|
||||||
|
@ -143,7 +147,7 @@ class ApplicationTreeNodeMixin:
|
||||||
'id': str(self.id),
|
'id': str(self.id),
|
||||||
'name': self.name,
|
'name': self.name,
|
||||||
'title': self.name,
|
'title': self.name,
|
||||||
'pId': str(self.type),
|
'pId': pid,
|
||||||
'isParent': False,
|
'isParent': False,
|
||||||
'open': False,
|
'open': False,
|
||||||
'iconSkin': icon_skin,
|
'iconSkin': icon_skin,
|
||||||
|
|
|
@ -236,7 +236,7 @@ class Organization(models.Model):
|
||||||
|
|
||||||
def as_tree_node(self, pid, opened=True):
|
def as_tree_node(self, pid, opened=True):
|
||||||
node = TreeNode(**{
|
node = TreeNode(**{
|
||||||
'id': self.id,
|
'id': str(self.id),
|
||||||
'name': self.name,
|
'name': self.name,
|
||||||
'title': self.name,
|
'title': self.name,
|
||||||
'pId': pid,
|
'pId': pid,
|
||||||
|
|
Loading…
Reference in New Issue