From 362cfb733cb17fc6dd0ea4500d1de2f527a76162 Mon Sep 17 00:00:00 2001 From: Bai Date: Tue, 20 Dec 2022 16:13:44 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BF=AE=E6=94=B9=20su-from-accounts?= =?UTF-8?q?=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/api/account/account.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/apps/assets/api/account/account.py b/apps/assets/api/account/account.py index aa22201b1..ebcfb1f09 100644 --- a/apps/assets/api/account/account.py +++ b/apps/assets/api/account/account.py @@ -6,7 +6,7 @@ from rest_framework.generics import CreateAPIView, ListAPIView from orgs.mixins.api import OrgBulkModelViewSet from common.mixins import RecordViewLogMixin -from assets.models import Account +from assets.models import Account, Asset from assets.filters import AccountFilterSet from assets.tasks import verify_accounts_connectivity from assets import serializers @@ -30,10 +30,18 @@ class AccountViewSet(OrgBulkModelViewSet): 'su_from_accounts': 'assets.view_account', } - @action(methods=['get'], detail=True, url_path='su-from-accounts') + @action(methods=['get'], detail=False, url_path='su-from-accounts') def su_from_accounts(self, request, *args, **kwargs): - account = get_object_or_404(Account, pk=self.kwargs['pk']) - accounts = account.get_su_from_accounts() + account_id = request.query_params.get('account') + asset_id = request.query_params.get('asset') + if account_id: + account = get_object_or_404(Account, pk=account_id) + accounts = account.get_su_from_accounts() + elif asset_id: + asset = get_object_or_404(Asset, pk=asset_id) + accounts = asset.accounts.all() + else: + accounts = [] serializer = serializers.AccountSerializer(accounts, many=True) return Response(data=serializer.data)