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
|
|
|
|
2023-07-26 11:15:52 +00:00
|
|
|
from assets.models import Node, 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
|
2023-05-11 02:15:40 +00:00
|
|
|
def serialize_assets(self, assets, node_key=None, pid=None):
|
2020-08-16 15:08:58 +00:00
|
|
|
if node_key is None:
|
|
|
|
get_pid = lambda asset: getattr(asset, 'parent_key', '')
|
|
|
|
else:
|
|
|
|
get_pid = lambda asset: node_key
|
2023-07-26 11:15:52 +00:00
|
|
|
sftp_asset_ids = Protocol.objects.filter(name='sftp') \
|
|
|
|
.values_list('asset_id', flat=True)
|
|
|
|
sftp_asset_ids = list(sftp_asset_ids)
|
2020-08-16 15:08:58 +00:00
|
|
|
data = [
|
|
|
|
{
|
|
|
|
'id': str(asset.id),
|
2022-08-11 07:45:03 +00:00
|
|
|
'name': asset.name,
|
2023-07-26 11:15:52 +00:00
|
|
|
'title': f'{asset.address}\n{asset.comment}',
|
2023-05-11 02:15:40 +00:00
|
|
|
'pId': pid or get_pid(asset),
|
2020-08-16 15:08:58 +00:00
|
|
|
'isParent': False,
|
|
|
|
'open': False,
|
2023-04-21 03:18:04 +00:00
|
|
|
'iconSkin': self.get_icon(asset),
|
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': {
|
2022-12-07 15:55:56 +00:00
|
|
|
'platform_type': asset.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
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for asset in assets
|
|
|
|
]
|
|
|
|
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)
|