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-12-14 08:00:59 +00:00
|
|
|
from assets.tasks import push_system_user_to_assets
|
|
|
|
|
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 信号,
|
|
|
|
无法更新节点下所有资产的管理用户的问题 """
|
2021-12-14 08:00:59 +00:00
|
|
|
need_push_asset_ids = []
|
2021-09-24 03:45:39 +00:00
|
|
|
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
|
|
|
|
)
|
2021-12-14 08:00:59 +00:00
|
|
|
if created:
|
|
|
|
need_push_asset_ids.append(asset.id)
|
2021-10-28 02:56:39 +00:00
|
|
|
# # 不再自动更新资产管理用户,只允许用户手动指定。
|
2021-09-24 03:45:39 +00:00
|
|
|
# 只要关联都需要更新资产的管理用户
|
2021-10-28 02:56:39 +00:00
|
|
|
# instance.update_asset_admin_user_if_need()
|
2021-12-14 08:00:59 +00:00
|
|
|
|
|
|
|
if need_push_asset_ids:
|
|
|
|
push_system_user_to_assets.delay(system_user.id, need_push_asset_ids)
|