From 4b67d6925e28e14f4dc7d5d2ea33c512c951af3a Mon Sep 17 00:00:00 2001 From: xinwen Date: Tue, 8 Dec 2020 17:29:22 +0800 Subject: [PATCH] =?UTF-8?q?feat(asset):=20api=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=8E=A8=E9=80=81=E7=B3=BB=E7=BB=9F=E7=94=A8=E6=88=B7=E5=88=B0?= =?UTF-8?q?=E5=A4=9A=E4=B8=AA=E8=B5=84=E4=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/api/system_user.py | 24 +++++++++++++++--------- apps/assets/serializers/system_user.py | 4 ++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/apps/assets/api/system_user.py b/apps/assets/api/system_user.py index 59be0fa68..70bbe376e 100644 --- a/apps/assets/api/system_user.py +++ b/apps/assets/api/system_user.py @@ -3,7 +3,8 @@ from django.shortcuts import get_object_or_404 from rest_framework.response import Response from common.utils import get_logger -from common.permissions import IsOrgAdmin, IsOrgAdminOrAppUser, IsAppUser +from common.permissions import IsOrgAdmin, IsOrgAdminOrAppUser +from common.drf.filters import CustomFilter from orgs.mixins.api import OrgBulkModelViewSet from orgs.mixins import generics from orgs.utils import tmp_to_org @@ -12,7 +13,7 @@ from .. import serializers from ..serializers import SystemUserWithAuthInfoSerializer from ..tasks import ( push_system_user_to_assets_manual, test_system_user_connectivity_manual, - push_system_user_a_asset_manual, + push_system_user_to_assets ) @@ -82,18 +83,18 @@ class SystemUserTaskApi(generics.CreateAPIView): permission_classes = (IsOrgAdmin,) serializer_class = serializers.SystemUserTaskSerializer - def do_push(self, system_user, asset=None): - if asset is None: + def do_push(self, system_user, assets_id=None): + if assets_id is None: task = push_system_user_to_assets_manual.delay(system_user) else: username = self.request.query_params.get('username') - task = push_system_user_a_asset_manual.delay( - system_user, asset, username=username + task = push_system_user_to_assets.delay( + system_user.id, assets_id, username=username ) return task @staticmethod - def do_test(system_user, asset=None): + def do_test(system_user): task = test_system_user_connectivity_manual.delay(system_user) return task @@ -104,11 +105,16 @@ class SystemUserTaskApi(generics.CreateAPIView): def perform_create(self, serializer): action = serializer.validated_data["action"] asset = serializer.validated_data.get('asset') + assets = serializer.validated_data.get('assets') or [] + system_user = self.get_object() if action == 'push': - task = self.do_push(system_user, asset) + assets = [asset] if asset else assets + assets_id = [asset.id for asset in assets] + assets_id = assets_id if assets_id else None + task = self.do_push(system_user, assets_id) else: - task = self.do_test(system_user, asset) + task = self.do_test(system_user) data = getattr(serializer, '_data', {}) data["task"] = task.id setattr(serializer, '_data', data) diff --git a/apps/assets/serializers/system_user.py b/apps/assets/serializers/system_user.py index a4f6d933c..fb8d4df97 100644 --- a/apps/assets/serializers/system_user.py +++ b/apps/assets/serializers/system_user.py @@ -257,4 +257,8 @@ class SystemUserTaskSerializer(serializers.Serializer): asset = serializers.PrimaryKeyRelatedField( queryset=Asset.objects, allow_null=True, required=False, write_only=True ) + assets = serializers.PrimaryKeyRelatedField( + queryset=Asset.objects, allow_null=True, required=False, write_only=True, + many=True + ) task = serializers.CharField(read_only=True)