2019-09-19 09:27:49 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
|
|
|
from celery import shared_task
|
|
|
|
|
2020-12-22 03:38:31 +00:00
|
|
|
from orgs.utils import tmp_to_root_org
|
2021-09-24 03:45:39 +00:00
|
|
|
from assets.models import AuthBook
|
2020-12-22 03:38:31 +00:00
|
|
|
|
2019-09-19 09:27:49 +00:00
|
|
|
__all__ = ['add_nodes_assets_to_system_users']
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
2020-12-22 03:38:31 +00:00
|
|
|
@tmp_to_root_org()
|
2019-09-19 09:27:49 +00:00
|
|
|
def add_nodes_assets_to_system_users(nodes_keys, system_users):
|
|
|
|
from ..models import Node
|
2021-02-05 05:29:29 +00:00
|
|
|
nodes = Node.objects.filter(key__in=nodes_keys)
|
|
|
|
assets = Node.get_nodes_all_assets(*nodes)
|
2019-09-19 09:27:49 +00:00
|
|
|
for system_user in system_users:
|
2021-09-24 03:45:39 +00:00
|
|
|
""" 解决资产和节点进行关联时,已经关联过的节点不会触发 authbook post_save 信号,
|
|
|
|
无法更新节点下所有资产的管理用户的问题 """
|
|
|
|
for asset in assets:
|
2021-09-24 12:14:23 +00:00
|
|
|
defaults = {'asset': asset, 'systemuser': system_user, 'org_id': asset.org_id}
|
2021-09-24 03:45:39 +00:00
|
|
|
instance, created = AuthBook.objects.update_or_create(
|
|
|
|
defaults=defaults, asset=asset, systemuser=system_user
|
|
|
|
)
|
|
|
|
# 只要关联都需要更新资产的管理用户
|
|
|
|
instance.update_asset_admin_user_if_need()
|