2020-08-16 15:08:58 +00:00
|
|
|
from typing import List
|
2022-08-22 10:32:33 +00:00
|
|
|
from rest_framework.request import Request
|
2020-08-16 15:08:58 +00:00
|
|
|
|
2022-08-22 10:32:33 +00:00
|
|
|
from common.utils import lazyproperty, timeit
|
2020-08-16 15:08:58 +00:00
|
|
|
from assets.models import Node, Asset
|
2021-02-08 06:59:20 +00:00
|
|
|
from assets.pagination import NodeAssetTreePagination
|
2022-08-22 10:32:33 +00:00
|
|
|
from assets.utils import get_node_from_request, is_query_node_all_assets
|
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
|
|
|
|
|
|
|
|
def get_platform(self, asset: Asset):
|
|
|
|
default = 'file'
|
|
|
|
icon = {'windows', 'linux'}
|
2022-08-24 06:23:42 +00:00
|
|
|
platform = asset.platform.type.lower()
|
2020-08-16 15:08:58 +00:00
|
|
|
if platform in icon:
|
|
|
|
return platform
|
|
|
|
return default
|
|
|
|
|
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):
|
|
|
|
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,
|
|
|
|
'iconSkin': self.get_platform(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': {
|
2020-08-16 15:08:58 +00:00
|
|
|
'id': asset.id,
|
2022-08-11 07:45:03 +00:00
|
|
|
'name': asset.name,
|
2022-09-21 03:17:14 +00:00
|
|
|
'address': asset.address,
|
2020-08-16 15:08:58 +00:00
|
|
|
'protocols': asset.protocols_as_list,
|
2022-08-24 06:23:42 +00:00
|
|
|
'platform': asset.platform.id,
|
2020-11-26 11:18:06 +00:00
|
|
|
'org_name': asset.org_name
|
2020-08-16 15:08:58 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for asset in assets
|
|
|
|
]
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2022-08-22 10:32:33 +00:00
|
|
|
class NodeFilterMixin:
|
2022-08-23 02:23:48 +00:00
|
|
|
# pagination_class = NodeAssetTreePagination
|
2022-08-22 10:32:33 +00:00
|
|
|
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)
|