2017-12-29 15:53:45 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2018-04-08 12:02:40 +00:00
|
|
|
from collections import defaultdict
|
2018-12-17 03:49:57 +00:00
|
|
|
from django.db.models.signals import post_save, m2m_changed, post_delete
|
2017-12-29 15:53:45 +00:00
|
|
|
from django.dispatch import receiver
|
|
|
|
|
|
|
|
from common.utils import get_logger
|
2019-06-13 10:58:43 +00:00
|
|
|
from common.decorator import on_transaction_commit
|
2019-03-18 02:15:33 +00:00
|
|
|
from .models import Asset, SystemUser, Node, AuthBook
|
|
|
|
from .tasks import (
|
|
|
|
update_assets_hardware_info_util,
|
|
|
|
test_asset_connectivity_util,
|
|
|
|
push_system_user_to_assets
|
|
|
|
)
|
2017-12-29 15:53:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
def update_asset_hardware_info_on_created(asset):
|
|
|
|
logger.debug("Update asset `{}` hardware info".format(asset))
|
|
|
|
update_assets_hardware_info_util.delay([asset])
|
|
|
|
|
|
|
|
|
|
|
|
def test_asset_conn_on_created(asset):
|
2018-12-18 09:28:45 +00:00
|
|
|
logger.debug("Test asset `{}` connectivity".format(asset))
|
|
|
|
test_asset_connectivity_util.delay([asset])
|
2017-12-29 15:53:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Asset, dispatch_uid="my_unique_identifier")
|
2019-06-13 10:58:43 +00:00
|
|
|
@on_transaction_commit
|
2018-02-25 10:08:00 +00:00
|
|
|
def on_asset_created_or_update(sender, instance=None, created=False, **kwargs):
|
|
|
|
if created:
|
2018-01-10 07:15:29 +00:00
|
|
|
logger.info("Asset `{}` create signal received".format(instance))
|
2019-06-13 10:58:43 +00:00
|
|
|
|
|
|
|
# 获取资产硬件信息
|
2017-12-29 15:53:45 +00:00
|
|
|
update_asset_hardware_info_on_created(instance)
|
|
|
|
test_asset_conn_on_created(instance)
|
2018-02-09 03:12:40 +00:00
|
|
|
|
2018-12-17 03:49:57 +00:00
|
|
|
# 过期节点资产数量
|
|
|
|
nodes = instance.nodes.all()
|
|
|
|
Node.expire_nodes_assets_amount(nodes)
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=Asset, dispatch_uid="my_unique_identifier")
|
|
|
|
def on_asset_delete(sender, instance=None, **kwargs):
|
|
|
|
# 过期节点资产数量
|
|
|
|
nodes = instance.nodes.all()
|
|
|
|
Node.expire_nodes_assets_amount(nodes)
|
|
|
|
|
2018-02-09 03:12:40 +00:00
|
|
|
|
|
|
|
@receiver(post_save, sender=SystemUser, dispatch_uid="my_unique_identifier")
|
|
|
|
def on_system_user_update(sender, instance=None, created=True, **kwargs):
|
|
|
|
if instance and not created:
|
2018-04-08 12:02:40 +00:00
|
|
|
logger.info("System user `{}` update signal received".format(instance))
|
|
|
|
assets = instance.assets.all()
|
|
|
|
push_system_user_to_assets.delay(instance, assets)
|
2018-02-09 03:12:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(m2m_changed, sender=SystemUser.nodes.through)
|
2018-04-08 12:02:40 +00:00
|
|
|
def on_system_user_nodes_change(sender, instance=None, **kwargs):
|
|
|
|
if instance and kwargs["action"] == "post_add":
|
2019-06-19 08:45:14 +00:00
|
|
|
logger.info("System user `{}` nodes update signal received".format(instance))
|
2018-04-08 12:02:40 +00:00
|
|
|
assets = set()
|
|
|
|
nodes = kwargs['model'].objects.filter(pk__in=kwargs['pk_set'])
|
|
|
|
for node in nodes:
|
|
|
|
assets.update(set(node.get_all_assets()))
|
|
|
|
instance.assets.add(*tuple(assets))
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(m2m_changed, sender=SystemUser.assets.through)
|
|
|
|
def on_system_user_assets_change(sender, instance=None, **kwargs):
|
2018-02-09 03:12:40 +00:00
|
|
|
if instance and kwargs["action"] == "post_add":
|
2018-04-08 12:02:40 +00:00
|
|
|
assets = kwargs['model'].objects.filter(pk__in=kwargs['pk_set'])
|
2018-12-10 02:11:54 +00:00
|
|
|
push_system_user_to_assets.delay(instance, assets)
|
2018-02-09 03:12:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(m2m_changed, sender=Asset.nodes.through)
|
|
|
|
def on_asset_node_changed(sender, instance=None, **kwargs):
|
2018-12-25 08:53:52 +00:00
|
|
|
logger.debug("Asset nodes change signal received")
|
2018-05-29 05:19:58 +00:00
|
|
|
if isinstance(instance, Asset):
|
2018-12-25 08:53:52 +00:00
|
|
|
if kwargs['action'] == 'pre_remove':
|
|
|
|
nodes = kwargs['model'].objects.filter(pk__in=kwargs['pk_set'])
|
|
|
|
Node.expire_nodes_assets_amount(nodes)
|
2018-05-29 05:19:58 +00:00
|
|
|
if kwargs['action'] == 'post_add':
|
|
|
|
nodes = kwargs['model'].objects.filter(pk__in=kwargs['pk_set'])
|
2018-12-17 03:49:57 +00:00
|
|
|
Node.expire_nodes_assets_amount(nodes)
|
2018-05-29 05:19:58 +00:00
|
|
|
system_users_assets = defaultdict(set)
|
|
|
|
system_users = SystemUser.objects.filter(nodes__in=nodes)
|
|
|
|
# 清理节点缓存
|
|
|
|
for system_user in system_users:
|
|
|
|
system_users_assets[system_user].update({instance})
|
|
|
|
for system_user, assets in system_users_assets.items():
|
|
|
|
system_user.assets.add(*tuple(assets))
|
2018-02-09 03:12:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(m2m_changed, sender=Asset.nodes.through)
|
|
|
|
def on_node_assets_changed(sender, instance=None, **kwargs):
|
2018-05-29 05:19:58 +00:00
|
|
|
if isinstance(instance, Node):
|
2018-12-25 08:53:52 +00:00
|
|
|
logger.debug("Node assets change signal {} received".format(instance))
|
2018-12-17 03:49:57 +00:00
|
|
|
# 当节点和资产关系发生改变时,过期资产数量缓存
|
|
|
|
instance.expire_assets_amount()
|
2018-02-09 03:12:40 +00:00
|
|
|
assets = kwargs['model'].objects.filter(pk__in=kwargs['pk_set'])
|
2018-05-29 05:19:58 +00:00
|
|
|
if kwargs['action'] == 'post_add':
|
|
|
|
# 重新关联系统用户和资产的关系
|
|
|
|
system_users = SystemUser.objects.filter(nodes=instance)
|
|
|
|
for system_user in system_users:
|
|
|
|
system_user.assets.add(*tuple(assets))
|
2018-08-14 07:59:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Node)
|
|
|
|
def on_node_update_or_created(sender, instance=None, created=False, **kwargs):
|
|
|
|
if instance and not created:
|
|
|
|
instance.expire_full_value()
|
2019-03-18 02:15:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=AuthBook)
|
|
|
|
def on_auth_book_created(sender, instance=None, created=False, **kwargs):
|
|
|
|
if created:
|
|
|
|
logger.debug('Receive create auth book object signal.')
|
|
|
|
instance.set_version_and_latest()
|