From f6c5c35a2c75aa09e86d3ccf3e556c1ea764a8c8 Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Thu, 23 Mar 2023 15:24:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B4=A6=E5=8F=B7=E6=94=B6=E9=9B=86?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E5=90=8C=E6=AD=A5=E8=B4=A6=E5=8F=B7=20(#1005?= =?UTF-8?q?1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: feng <1304903146@qq.com> --- .../api/automations/gather_accounts.py | 43 +++++++++++-------- apps/perms/serializers/permission.py | 2 +- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/apps/accounts/api/automations/gather_accounts.py b/apps/accounts/api/automations/gather_accounts.py index 3abca94dd..f4420f919 100644 --- a/apps/accounts/api/automations/gather_accounts.py +++ b/apps/accounts/api/automations/gather_accounts.py @@ -6,10 +6,9 @@ from rest_framework.decorators import action from rest_framework.response import Response from accounts import serializers -from accounts.const import AutomationTypes -from accounts.const import Source +from accounts.const import Source, AutomationTypes from accounts.filters import GatheredAccountFilterSet -from accounts.models import GatherAccountsAutomation +from accounts.models import GatherAccountsAutomation, Account from accounts.models import GatheredAccount from orgs.mixins.api import OrgBulkModelViewSet from .base import AutomationExecutionViewSet @@ -50,22 +49,28 @@ class GatheredAccountViewSet(OrgBulkModelViewSet): 'default': serializers.GatheredAccountSerializer, } rbac_perms = { - 'sync_account': 'assets.add_gatheredaccount', + 'sync_accounts': 'assets.add_gatheredaccount', } - @action(methods=['post'], detail=True, url_path='sync') - def sync_account(self, request, *args, **kwargs): - gathered_account = super().get_object() - asset = gathered_account.asset - username = gathered_account.username - accounts = asset.accounts.filter(username=username) - - if accounts.exists(): - accounts.update(source=Source.COLLECTED) - else: - asset.accounts.model.objects.create( - asset=asset, username=username, - name=f'{username}-{_("Collected")}', - source=Source.COLLECTED - ) + @action(methods=['post'], detail=False, url_path='sync-accounts') + def sync_accounts(self, request, *args, **kwargs): + gathered_account_ids = request.data.get('gathered_account_ids') + gathered_accounts = self.model.objects.filter(id__in=gathered_account_ids) + account_objs = [] + exists_accounts = Account.objects.none() + for gathered_account in gathered_accounts: + asset_id = gathered_account.asset_id + username = gathered_account.username + accounts = Account.objects.filter(asset_id=asset_id, username=username) + if accounts.exists(): + exists_accounts |= accounts + else: + account_objs.append( + Account( + asset_id=asset_id, username=username, + name=f'{username}-{_("Collected")}', + source=Source.COLLECTED + )) + exists_accounts.update(source=Source.COLLECTED) + Account.objects.bulk_create(account_objs) return Response(status=status.HTTP_201_CREATED) diff --git a/apps/perms/serializers/permission.py b/apps/perms/serializers/permission.py index 9260267cb..5fc19be29 100644 --- a/apps/perms/serializers/permission.py +++ b/apps/perms/serializers/permission.py @@ -109,7 +109,7 @@ class AssetPermissionSerializer(BulkOrgResourceModelSerializer): if condition in username_secret_type_dict: continue account_data = {key: getattr(template, key) for key in account_attribute} - account_data['name'] = f"{account_data['name']}-clone" + account_data['name'] = f"{account_data['name']}-{_('Account template')}" need_create_accounts.append(Account(**{'asset_id': asset.id, **account_data})) return Account.objects.bulk_create(need_create_accounts)