From 3cdabaf8835afefe7473b094aa82dcedb9856c26 Mon Sep 17 00:00:00 2001 From: BaiJiangJie <32935519+BaiJiangJie@users.noreply.github.com> Date: Fri, 26 Jul 2019 16:16:06 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=20=E7=B3=BB=E7=BB=9F=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E8=B5=84=E4=BA=A7=E7=AE=A1=E7=90=86=E9=A1=B5=E9=9D=A2=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E7=9B=B8=E5=85=B3=E7=9A=84=E6=89=80=E6=9C=89=E8=B5=84?= =?UTF-8?q?=E4=BA=A7=20(#3038)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [Update] 系统用户资产管理页面获取相关的所有资产 * [Update] 系统用户资产管理页面获取相关的所有资产2 --- apps/assets/api/asset_user.py | 2 +- apps/assets/models/asset.py | 2 +- apps/assets/models/user.py | 16 ++++++++++++++++ apps/assets/signals_handler.py | 20 ++++++++++---------- apps/assets/tasks.py | 6 +++--- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/apps/assets/api/asset_user.py b/apps/assets/api/asset_user.py index 38d820349..b916e43ec 100644 --- a/apps/assets/api/asset_user.py +++ b/apps/assets/api/asset_user.py @@ -81,7 +81,7 @@ class AssetUserViewSet(IDInCacheFilterMixin, BulkModelViewSet): manager = AssetUserManager() if system_user_id: system_user = get_object_or_404(SystemUser, id=system_user_id) - assets = system_user.assets.all() + assets = system_user.get_all_assets() username = system_user.username elif admin_user_id: admin_user = get_object_or_404(AdminUser, id=admin_user_id) diff --git a/apps/assets/models/asset.py b/apps/assets/models/asset.py index 2161113e2..4a0816419 100644 --- a/apps/assets/models/asset.py +++ b/apps/assets/models/asset.py @@ -12,7 +12,6 @@ from django.core.cache import cache from django.db import models from django.utils.translation import ugettext_lazy as _ -from .user import AdminUser, SystemUser from .utils import Connectivity from orgs.mixins import OrgModelMixin, OrgManager @@ -320,6 +319,7 @@ class Asset(ProtocolsMixin, NodesRelationMixin, OrgModelMixin): @classmethod def generate_fake(cls, count=100): + from .user import AdminUser, SystemUser from random import seed, choice from django.db import IntegrityError from .node import Node diff --git a/apps/assets/models/user.py b/apps/assets/models/user.py index bbd808b80..0f6278f30 100644 --- a/apps/assets/models/user.py +++ b/apps/assets/models/user.py @@ -4,12 +4,15 @@ import logging +from functools import reduce from django.db import models +from django.db.models import Q from django.utils.translation import ugettext_lazy as _ from django.core.validators import MinValueValidator, MaxValueValidator from common.utils import get_signer from .base import AssetUser +from .asset import Asset __all__ = ['AdminUser', 'SystemUser'] @@ -144,6 +147,19 @@ class SystemUser(AssetUser): return False, matched_cmd return True, None + def get_all_assets(self): + args = [Q(systemuser=self)] + pattern = set() + nodes_keys = self.nodes.all().values_list('key', flat=True) + for key in nodes_keys: + pattern.add(r'^{0}$|^{0}:'.format(key)) + pattern = '|'.join(list(pattern)) + if pattern: + args.append(Q(nodes__key__regex=pattern)) + args = reduce(lambda x, y: x | y, args) + assets = Asset.objects.filter(args).distinct() + return assets + class Meta: ordering = ['name'] unique_together = [('name', 'org_id')] diff --git a/apps/assets/signals_handler.py b/apps/assets/signals_handler.py index b2316d7d0..70eecbd10 100644 --- a/apps/assets/signals_handler.py +++ b/apps/assets/signals_handler.py @@ -57,16 +57,16 @@ def on_system_user_update(sender, instance=None, created=True, **kwargs): push_system_user_to_assets.delay(instance, assets) -@receiver(m2m_changed, sender=SystemUser.nodes.through) -def on_system_user_nodes_change(sender, instance=None, **kwargs): - if instance and kwargs["action"] == "post_add": - logger.info("System user `{}` nodes update signal received".format(instance)) - 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.nodes.through) +# def on_system_user_nodes_change(sender, instance=None, **kwargs): +# if instance and kwargs["action"] == "post_add": +# logger.info("System user `{}` nodes update signal received".format(instance)) +# 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): diff --git a/apps/assets/tasks.py b/apps/assets/tasks.py index dbd6808f1..8d057f897 100644 --- a/apps/assets/tasks.py +++ b/apps/assets/tasks.py @@ -347,7 +347,7 @@ def test_system_user_connectivity_util(system_user, assets, task_name): @shared_task def test_system_user_connectivity_manual(system_user): task_name = _("Test system user connectivity: {}").format(system_user) - assets = system_user.get_related_assets() + assets = system_user.get_all_assets() return test_system_user_connectivity_util(system_user, assets, task_name) @@ -367,7 +367,7 @@ def test_system_user_connectivity_period(): system_users = SystemUser.objects.all() for system_user in system_users: task_name = _("Test system user connectivity period: {}").format(system_user) - assets = system_user.get_related_assets() + assets = system_user.get_all_assets() test_system_user_connectivity_util(system_user, assets, task_name) @@ -513,7 +513,7 @@ def push_system_user_util(system_user, assets, task_name): @shared_task def push_system_user_to_assets_manual(system_user): - assets = system_user.get_related_assets() + assets = system_user.get_all_assets() task_name = _("Push system users to assets: {}").format(system_user.name) return push_system_user_util(system_user, assets, task_name=task_name)