2020-08-16 15:08:58 +00:00
|
|
|
from typing import List
|
2022-11-30 03:28:51 +00:00
|
|
|
|
2022-08-22 10:32:33 +00:00
|
|
|
from rest_framework.request import Request
|
2020-08-16 15:08:58 +00:00
|
|
|
|
2024-01-02 08:11:56 +00:00
|
|
|
from assets.models import Node, Platform, Protocol
|
2022-08-22 10:32:33 +00:00
|
|
|
from assets.utils import get_node_from_request, is_query_node_all_assets
|
2022-11-30 03:28:51 +00:00
|
|
|
from common.utils import lazyproperty, timeit
|
2020-08-16 15:08:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SerializeToTreeNodeMixin:
|
2023-04-04 03:28:51 +00:00
|
|
|
request: Request
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def is_sync(self):
|
|
|
|
sync_paths = ['/api/v1/perms/users/self/nodes/all-with-assets/tree/']
|
|
|
|
for p in sync_paths:
|
|
|
|
if p == self.request.path:
|
|
|
|
return True
|
|
|
|
return False
|
2021-02-05 05:29:29 +00:00
|
|
|
|
|
|
|
@timeit
|
2020-08-16 15:08:58 +00:00
|
|
|
def serialize_nodes(self, nodes: List[Node], with_asset_amount=False):
|
|
|
|
if with_asset_amount:
|
|
|
|
def _name(node: Node):
|
|
|
|
return '{} ({})'.format(node.value, node.assets_amount)
|
|
|
|
else:
|
|
|
|
def _name(node: Node):
|
|
|
|
return node.value
|
2023-04-04 03:28:51 +00:00
|
|
|
|
|
|
|
def _open(node):
|
|
|
|
if not self.is_sync:
|
|
|
|
# 异步加载资产树时,默认展开节点
|
|
|
|
return True
|
|
|
|
if not node.parent_key:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
data = [
|
|
|
|
{
|
|
|
|
'id': node.key,
|
|
|
|
'name': _name(node),
|
|
|
|
'title': _name(node),
|
|
|
|
'pId': node.parent_key,
|
2023-08-18 07:43:06 +00:00
|
|
|
'isParent': True,
|
2023-04-04 03:28:51 +00:00
|
|
|
'open': _open(node),
|
2020-08-16 15:08:58 +00:00
|
|
|
'meta': {
|
2021-08-17 08:13:19 +00:00
|
|
|
'data': {
|
2020-08-16 15:08:58 +00:00
|
|
|
"id": node.id,
|
|
|
|
"key": node.key,
|
|
|
|
"value": node.value,
|
|
|
|
},
|
|
|
|
'type': 'node'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for node in nodes
|
|
|
|
]
|
|
|
|
return data
|
|
|
|
|
2023-04-21 03:18:04 +00:00
|
|
|
@lazyproperty
|
|
|
|
def support_types(self):
|
|
|
|
from assets.const import AllTypes
|
|
|
|
return AllTypes.get_types_values(exclude_custom=True)
|
|
|
|
|
|
|
|
def get_icon(self, asset):
|
2023-11-15 09:00:12 +00:00
|
|
|
if asset.category == 'device':
|
|
|
|
return 'switch'
|
2023-04-21 03:18:04 +00:00
|
|
|
if asset.type in self.support_types:
|
|
|
|
return asset.type
|
|
|
|
else:
|
|
|
|
return 'file'
|
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
@timeit
|
2024-01-02 08:11:56 +00:00
|
|
|
def serialize_assets(self, assets, node_key=None, get_pid=None):
|
|
|
|
if not get_pid and not node_key:
|
|
|
|
get_pid = lambda asset, platform: getattr(asset, 'parent_key', '')
|
|
|
|
|
2023-07-26 11:15:52 +00:00
|
|
|
sftp_asset_ids = Protocol.objects.filter(name='sftp') \
|
|
|
|
.values_list('asset_id', flat=True)
|
2024-01-02 08:11:56 +00:00
|
|
|
sftp_asset_ids = set(sftp_asset_ids)
|
|
|
|
platform_map = {p.id: p for p in Platform.objects.all()}
|
|
|
|
|
|
|
|
data = []
|
2024-01-10 05:37:55 +00:00
|
|
|
root_assets_count = 0
|
2024-01-02 08:11:56 +00:00
|
|
|
for asset in assets:
|
|
|
|
platform = platform_map.get(asset.platform_id)
|
|
|
|
if not platform:
|
|
|
|
continue
|
|
|
|
pid = node_key or get_pid(asset, platform)
|
2024-01-10 05:37:55 +00:00
|
|
|
if not pid:
|
2024-01-02 08:11:56 +00:00
|
|
|
continue
|
2024-01-10 05:37:55 +00:00
|
|
|
# 根节点最多显示 1000 个资产
|
|
|
|
if pid.isdigit():
|
|
|
|
if root_assets_count > 1000:
|
|
|
|
continue
|
|
|
|
root_assets_count += 1
|
2024-01-02 08:11:56 +00:00
|
|
|
data.append({
|
2020-08-16 15:08:58 +00:00
|
|
|
'id': str(asset.id),
|
2022-08-11 07:45:03 +00:00
|
|
|
'name': asset.name,
|
2024-01-02 08:11:56 +00:00
|
|
|
'title': f'{asset.address}\n{asset.comment}'.strip(),
|
|
|
|
'pId': pid,
|
2020-08-16 15:08:58 +00:00
|
|
|
'isParent': False,
|
|
|
|
'open': False,
|
2024-01-02 08:11:56 +00:00
|
|
|
'iconSkin': self.get_icon(platform),
|
2020-10-13 07:50:11 +00:00
|
|
|
'chkDisabled': not asset.is_active,
|
2020-08-16 15:08:58 +00:00
|
|
|
'meta': {
|
|
|
|
'type': 'asset',
|
2021-08-17 08:13:19 +00:00
|
|
|
'data': {
|
2024-01-02 08:11:56 +00:00
|
|
|
'platform_type': platform.type,
|
2022-11-30 07:24:32 +00:00
|
|
|
'org_name': asset.org_name,
|
2023-07-26 11:15:52 +00:00
|
|
|
'sftp': asset.id in sftp_asset_ids,
|
2023-04-04 03:48:56 +00:00
|
|
|
'name': asset.name,
|
|
|
|
'address': asset.address
|
2020-08-16 15:08:58 +00:00
|
|
|
},
|
|
|
|
}
|
2024-01-02 08:11:56 +00:00
|
|
|
})
|
2020-08-16 15:08:58 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
2022-08-22 10:32:33 +00:00
|
|
|
class NodeFilterMixin:
|
|
|
|
request: Request
|
2020-08-16 15:08:58 +00:00
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def is_query_node_all_assets(self):
|
2020-09-29 07:35:39 +00:00
|
|
|
return is_query_node_all_assets(self.request)
|
2020-08-16 15:08:58 +00:00
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def node(self):
|
2022-08-22 10:32:33 +00:00
|
|
|
return get_node_from_request(self.request)
|