2021-07-27 08:06:00 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2020-11-05 03:23:32 +00:00
|
|
|
|
2021-07-27 08:06:00 +00:00
|
|
|
from common.tree import TreeNode
|
|
|
|
from orgs.models import Organization
|
|
|
|
from ..models import Application
|
2020-11-05 03:23:32 +00:00
|
|
|
|
2021-01-05 15:39:38 +00:00
|
|
|
__all__ = ['SerializeApplicationToTreeNodeMixin']
|
2020-11-05 03:23:32 +00:00
|
|
|
|
2020-10-22 09:05:47 +00:00
|
|
|
|
|
|
|
class SerializeApplicationToTreeNodeMixin:
|
|
|
|
@staticmethod
|
2021-07-27 08:06:00 +00:00
|
|
|
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]
|
2021-07-29 11:36:08 +00:00
|
|
|
organizations.sort(key=lambda x: x.name)
|
2021-07-27 08:06:00 +00:00
|
|
|
return organizations
|
2020-12-08 07:33:24 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2021-07-27 08:06:00 +00:00
|
|
|
def create_root_node():
|
|
|
|
name = _('My applications')
|
|
|
|
node = TreeNode(**{
|
|
|
|
'id': 'applications',
|
|
|
|
'name': name,
|
|
|
|
'title': name,
|
2020-12-08 07:33:24 +00:00
|
|
|
'pId': '',
|
|
|
|
'open': True,
|
|
|
|
'isParent': True,
|
|
|
|
'meta': {
|
2021-07-27 08:06:00 +00:00
|
|
|
'type': 'root'
|
2020-12-08 07:33:24 +00:00
|
|
|
}
|
2021-07-27 08:06:00 +00:00
|
|
|
})
|
|
|
|
return node
|
2020-12-08 07:33:24 +00:00
|
|
|
|
|
|
|
def serialize_applications_with_org(self, applications):
|
2021-09-24 05:57:20 +00:00
|
|
|
if not applications:
|
|
|
|
return []
|
2021-07-27 08:06:00 +00:00
|
|
|
root_node = self.create_root_node()
|
|
|
|
tree_nodes = [root_node]
|
2020-12-08 07:33:24 +00:00
|
|
|
organizations = self.filter_organizations(applications)
|
2021-07-27 08:06:00 +00:00
|
|
|
|
|
|
|
for i, org in enumerate(organizations):
|
|
|
|
# 组织节点
|
|
|
|
org_node = org.as_tree_node(pid=root_node.id)
|
|
|
|
tree_nodes.append(org_node)
|
|
|
|
org_applications = applications.filter(org_id=org.id)
|
|
|
|
count = org_applications.count()
|
|
|
|
org_node.name += '({})'.format(count)
|
|
|
|
|
|
|
|
# 各应用节点
|
|
|
|
apps_nodes = Application.create_tree_nodes(
|
|
|
|
queryset=org_applications, root_node=org_node,
|
|
|
|
show_empty=False
|
|
|
|
)
|
|
|
|
tree_nodes += apps_nodes
|
|
|
|
return tree_nodes
|