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
|
|
|
|
2022-11-30 07:24:32 +00:00
|
|
|
from assets.models import Node, PlatformProtocol
|
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:
|
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
|
|
|
|
data = [
|
|
|
|
{
|
|
|
|
'id': node.key,
|
|
|
|
'name': _name(node),
|
|
|
|
'title': _name(node),
|
|
|
|
'pId': node.parent_key,
|
|
|
|
'isParent': True,
|
2022-08-11 03:07:17 +00:00
|
|
|
'open': True,
|
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
|
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
@timeit
|
2020-08-16 15:08:58 +00:00
|
|
|
def serialize_assets(self, assets, node_key=None):
|
2022-11-30 07:24:32 +00:00
|
|
|
sftp_enabled_platform = PlatformProtocol.objects \
|
|
|
|
.filter(name='ssh', setting__sftp_enabled=True) \
|
|
|
|
.values_list('platform', flat=True).distinct()
|
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
|
|
|
|
|
|
|
|
data = [
|
|
|
|
{
|
|
|
|
'id': str(asset.id),
|
2022-08-11 07:45:03 +00:00
|
|
|
'name': asset.name,
|
2022-09-21 03:17:14 +00:00
|
|
|
'title': asset.address,
|
2020-08-16 15:08:58 +00:00
|
|
|
'pId': get_pid(asset),
|
|
|
|
'isParent': False,
|
|
|
|
'open': False,
|
2022-11-30 03:28:51 +00:00
|
|
|
'iconSkin': asset.type,
|
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,
|
|
|
|
'sftp': asset.platform_id in sftp_enabled_platform,
|
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)
|