2016-08-09 09:27:37 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
#
|
2020-08-16 15:08:58 +00:00
|
|
|
from django.db.models import Q
|
|
|
|
|
2020-09-29 07:35:39 +00:00
|
|
|
from common.utils import get_logger, dict_get_any, is_uuid, get_object_or_none
|
|
|
|
from common.http import is_true
|
2020-03-12 08:24:38 +00:00
|
|
|
from .models import Asset, Node
|
2016-09-04 09:43:03 +00:00
|
|
|
|
2017-12-06 10:31:51 +00:00
|
|
|
|
2019-06-27 13:43:10 +00:00
|
|
|
logger = get_logger(__file__)
|
2017-12-07 05:01:33 +00:00
|
|
|
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
def check_node_assets_amount():
|
|
|
|
for node in Node.objects.all():
|
|
|
|
assets_amount = Asset.objects.filter(
|
|
|
|
Q(nodes__key__istartswith=f'{node.key}:') | Q(nodes=node)
|
|
|
|
).distinct().count()
|
2019-08-21 12:27:21 +00:00
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
if node.assets_amount != assets_amount:
|
2020-09-27 08:02:44 +00:00
|
|
|
print(f'>>> <Node:{node.key}> wrong assets amount '
|
2020-08-16 15:08:58 +00:00
|
|
|
f'{node.assets_amount} right is {assets_amount}')
|
|
|
|
node.assets_amount = assets_amount
|
|
|
|
node.save()
|
2019-09-26 07:09:10 +00:00
|
|
|
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
def is_asset_exists_in_node(asset_pk, node_key):
|
|
|
|
return Asset.objects.filter(
|
|
|
|
id=asset_pk
|
|
|
|
).filter(
|
|
|
|
Q(nodes__key__istartswith=f'{node_key}:') | Q(nodes__key=node_key)
|
|
|
|
).exists()
|
2020-09-29 07:35:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_query_node_all_assets(request):
|
|
|
|
request = request
|
2020-10-19 08:31:55 +00:00
|
|
|
query_all_arg = request.query_params.get('all', 'true')
|
2020-09-29 07:35:39 +00:00
|
|
|
show_current_asset_arg = request.query_params.get('show_current_asset')
|
|
|
|
if show_current_asset_arg is not None:
|
|
|
|
return not is_true(show_current_asset_arg)
|
|
|
|
return is_true(query_all_arg)
|
|
|
|
|
|
|
|
|
|
|
|
def get_node(request):
|
|
|
|
node_id = dict_get_any(request.query_params, ['node', 'node_id'])
|
|
|
|
if not node_id:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if is_uuid(node_id):
|
|
|
|
node = get_object_or_none(Node, id=node_id)
|
|
|
|
else:
|
|
|
|
node = get_object_or_none(Node, key=node_id)
|
|
|
|
return node
|