2021-08-30 08:48:46 +00:00
|
|
|
from urllib.parse import urlencode, parse_qsl
|
|
|
|
|
2021-07-27 08:06:00 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2021-08-30 08:48:46 +00:00
|
|
|
from rest_framework.generics import get_object_or_404
|
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
|
2021-08-30 08:48:46 +00:00
|
|
|
from assets.models import SystemUser
|
2022-03-29 09:22:59 +00:00
|
|
|
from applications.utils import KubernetesTree
|
|
|
|
from applications.models import Application
|
2021-08-30 08:48:46 +00:00
|
|
|
from perms.utils.application.permission import get_application_system_user_ids
|
|
|
|
|
|
|
|
|
2022-03-29 09:22:59 +00:00
|
|
|
class GrantedAppTreeUtil:
|
2020-10-22 09:05:47 +00:00
|
|
|
@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,
|
2022-06-06 05:30:04 +00:00
|
|
|
'iconSkin': 'applications',
|
2020-12-08 07:33:24 +00:00
|
|
|
'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
|
|
|
|
2022-06-15 07:13:51 +00:00
|
|
|
@staticmethod
|
|
|
|
def create_empty_node():
|
|
|
|
name = _("Empty")
|
|
|
|
node = TreeNode(**{
|
|
|
|
'id': 'empty',
|
|
|
|
'name': name,
|
|
|
|
'title': name,
|
|
|
|
'pId': '',
|
|
|
|
'isParent': True,
|
|
|
|
'children': [],
|
|
|
|
'meta': {
|
|
|
|
'type': 'application'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return node
|
|
|
|
|
2022-03-29 09:22:59 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_children_nodes(tree_id, parent_info, user):
|
2021-08-30 08:48:46 +00:00
|
|
|
tree_nodes = []
|
|
|
|
parent_info = dict(parse_qsl(parent_info))
|
|
|
|
pod_name = parent_info.get('pod')
|
|
|
|
app_id = parent_info.get('app_id')
|
|
|
|
namespace = parent_info.get('namespace')
|
|
|
|
system_user_id = parent_info.get('system_user_id')
|
2022-03-29 09:22:59 +00:00
|
|
|
|
2021-08-30 08:48:46 +00:00
|
|
|
if app_id and not any([pod_name, namespace, system_user_id]):
|
|
|
|
app = get_object_or_404(Application, id=app_id)
|
|
|
|
system_user_ids = get_application_system_user_ids(user, app)
|
|
|
|
system_users = SystemUser.objects.filter(id__in=system_user_ids).order_by('priority')
|
|
|
|
for system_user in system_users:
|
|
|
|
system_user_node = KubernetesTree(tree_id).as_system_user_tree_node(
|
|
|
|
system_user, parent_info
|
|
|
|
)
|
|
|
|
tree_nodes.append(system_user_node)
|
|
|
|
return tree_nodes
|
|
|
|
tree_nodes = KubernetesTree(tree_id).async_tree_node(parent_info)
|
2021-07-27 08:06:00 +00:00
|
|
|
return tree_nodes
|
2022-03-29 09:22:59 +00:00
|
|
|
|
|
|
|
def create_tree_nodes(self, applications):
|
|
|
|
tree_nodes = []
|
|
|
|
if not applications:
|
2022-06-15 07:13:51 +00:00
|
|
|
return [self.create_empty_node()]
|
2022-03-29 09:22:59 +00:00
|
|
|
|
|
|
|
root_node = self.create_root_node()
|
|
|
|
organizations = self.filter_organizations(applications)
|
|
|
|
|
|
|
|
for i, org in enumerate(organizations):
|
|
|
|
tree_id = urlencode({'org_id': str(org.id)})
|
|
|
|
apps = applications.filter(org_id=org.id)
|
|
|
|
|
|
|
|
# 组织节点
|
|
|
|
org_node = org.as_tree_node(oid=tree_id, pid=root_node.id)
|
|
|
|
org_node.name += '({})'.format(apps.count())
|
|
|
|
tree_nodes.append(org_node)
|
|
|
|
|
|
|
|
# 类别节点
|
|
|
|
category_type_nodes = Application.create_category_type_tree_nodes(
|
|
|
|
apps, tree_id, show_empty=False
|
|
|
|
)
|
|
|
|
tree_nodes += category_type_nodes
|
|
|
|
|
|
|
|
for app in apps:
|
|
|
|
app_node = app.as_tree_node(tree_id, k8s_as_tree=True)
|
|
|
|
tree_nodes.append(app_node)
|
|
|
|
return tree_nodes
|