feat: 收集账号 可选同步表

This commit is contained in:
feng
2023-03-23 18:57:22 +08:00
committed by Jiangjie.Bai
parent 66d368f882
commit 1ac2fec13f
5 changed files with 79 additions and 32 deletions

View File

@@ -1,14 +1,13 @@
# -*- coding: utf-8 -*-
#
from django.utils.translation import ugettext_lazy as _
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.response import Response
from accounts import serializers
from accounts.const import Source, AutomationTypes
from accounts.const import AutomationTypes
from accounts.filters import GatheredAccountFilterSet
from accounts.models import GatherAccountsAutomation, Account
from accounts.models import GatherAccountsAutomation
from accounts.models import GatheredAccount
from orgs.mixins.api import OrgBulkModelViewSet
from .base import AutomationExecutionViewSet
@@ -56,21 +55,5 @@ class GatheredAccountViewSet(OrgBulkModelViewSet):
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)
self.model.sync_accounts(gathered_accounts)
return Response(status=status.HTTP_201_CREATED)