2021-02-08 06:59:20 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from operator import add, sub
|
2023-02-09 12:48:25 +00:00
|
|
|
|
2024-01-23 08:45:59 +00:00
|
|
|
from django.conf import settings
|
2023-02-09 12:48:25 +00:00
|
|
|
from django.db.models.signals import m2m_changed
|
2021-02-08 06:59:20 +00:00
|
|
|
from django.dispatch import receiver
|
|
|
|
|
2023-02-09 12:48:25 +00:00
|
|
|
from assets.models import Asset, Node
|
2023-02-16 08:51:03 +00:00
|
|
|
from common.const.signals import PRE_CLEAR, POST_ADD, PRE_REMOVE
|
2023-02-09 12:48:25 +00:00
|
|
|
from common.decorators import on_transaction_commit, merge_delay_run
|
2024-01-23 08:45:59 +00:00
|
|
|
from common.signals import django_ready
|
2021-02-08 06:59:20 +00:00
|
|
|
from common.utils import get_logger
|
2024-01-23 08:45:59 +00:00
|
|
|
from orgs.utils import tmp_to_org, tmp_to_root_org
|
2023-02-10 06:44:59 +00:00
|
|
|
from ..tasks import check_node_assets_amount_task
|
2021-02-08 06:59:20 +00:00
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(m2m_changed, sender=Asset.nodes.through)
|
2024-01-02 08:11:56 +00:00
|
|
|
@on_transaction_commit
|
2021-02-08 06:59:20 +00:00
|
|
|
def on_node_asset_change(sender, action, instance, reverse, pk_set, **kwargs):
|
|
|
|
# 不允许 `pre_clear` ,因为该信号没有 `pk_set`
|
|
|
|
# [官网](https://docs.djangoproject.com/en/3.1/ref/signals/#m2m-changed)
|
|
|
|
refused = (PRE_CLEAR,)
|
|
|
|
if action in refused:
|
|
|
|
raise ValueError
|
|
|
|
|
2023-02-16 08:51:03 +00:00
|
|
|
# 这里监听 post_add, pre_remove, 如果pre_add 和 post_remove, 那么 node_ids 就已经获取不到了
|
|
|
|
mapper = {POST_ADD: add, PRE_REMOVE: sub}
|
2021-02-08 06:59:20 +00:00
|
|
|
if action not in mapper:
|
|
|
|
return
|
|
|
|
|
2021-03-17 07:04:34 +00:00
|
|
|
with tmp_to_org(instance.org):
|
|
|
|
if reverse:
|
2023-02-09 12:48:25 +00:00
|
|
|
node_ids = [instance.id]
|
2021-03-17 07:04:34 +00:00
|
|
|
else:
|
2023-02-16 08:51:03 +00:00
|
|
|
node_ids = list(pk_set)
|
2024-01-23 08:45:59 +00:00
|
|
|
update_nodes_assets_amount.delay(node_ids=node_ids)
|
2021-02-08 06:59:20 +00:00
|
|
|
|
|
|
|
|
2024-01-02 08:11:56 +00:00
|
|
|
@merge_delay_run(ttl=30)
|
2023-02-13 11:14:00 +00:00
|
|
|
def update_nodes_assets_amount(node_ids=()):
|
2023-02-16 08:51:03 +00:00
|
|
|
nodes = Node.objects.filter(id__in=node_ids)
|
|
|
|
nodes = Node.get_ancestor_queryset(nodes)
|
2023-02-13 12:04:17 +00:00
|
|
|
logger.debug('Recv asset nodes change signal, recompute node assets amount')
|
2023-02-09 12:48:25 +00:00
|
|
|
logger.info('Update nodes assets amount: {} nodes'.format(len(node_ids)))
|
2023-02-10 06:44:59 +00:00
|
|
|
|
|
|
|
if len(node_ids) > 100:
|
|
|
|
check_node_assets_amount_task.delay()
|
|
|
|
return
|
|
|
|
|
2023-02-09 12:48:25 +00:00
|
|
|
for node in nodes:
|
|
|
|
node.assets_amount = node.get_assets_amount()
|
2021-02-08 06:59:20 +00:00
|
|
|
|
2023-02-09 12:48:25 +00:00
|
|
|
Node.objects.bulk_update(nodes, ['assets_amount'])
|
2024-01-23 08:45:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(django_ready)
|
|
|
|
def set_assets_size_to_setting(sender, **kwargs):
|
|
|
|
from assets.models import Asset
|
|
|
|
try:
|
|
|
|
with tmp_to_root_org():
|
|
|
|
amount = Asset.objects.order_by().count()
|
|
|
|
except:
|
|
|
|
amount = 0
|
|
|
|
|
|
|
|
if amount > 20000:
|
|
|
|
settings.ASSET_SIZE = 'large'
|
|
|
|
elif amount > 2000:
|
|
|
|
settings.ASSET_SIZE = 'medium'
|