From 6b661b6aae400b4a1396b4d76ba5296a3381d442 Mon Sep 17 00:00:00 2001 From: ibuler Date: Tue, 14 Jan 2025 18:13:17 +0800 Subject: [PATCH] perf: update risk add reopen action --- .../accounts/api/automations/check_account.py | 3 +- apps/accounts/risk_handlers.py | 41 +++++++++++++------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/apps/accounts/api/automations/check_account.py b/apps/accounts/api/automations/check_account.py index 34e921c62..a63ec68e7 100644 --- a/apps/accounts/api/automations/check_account.py +++ b/apps/accounts/api/automations/check_account.py @@ -130,7 +130,8 @@ class AccountRiskViewSet(OrgBulkModelViewSet): data = handler.handle(act, risk) if not data: data = {"message": "Success"} - return Response(data) + s = serializers.AccountRiskSerializer(instance=data) + return Response(data=s.data) class CheckAccountEngineViewSet(JMSModelViewSet): diff --git a/apps/accounts/risk_handlers.py b/apps/accounts/risk_handlers.py index f6370494a..a10eb0973 100644 --- a/apps/accounts/risk_handlers.py +++ b/apps/accounts/risk_handlers.py @@ -1,18 +1,21 @@ from django.utils import timezone from django.utils.translation import gettext_lazy as _ +from rest_framework.serializers import ValidationError from accounts.const import AutomationTypes, Source from accounts.models import ( GatheredAccount, AccountRisk, SecretType, - AutomationExecution, RiskChoice, Account + AutomationExecution, + RiskChoice ) from common.const import ConfirmOrIgnore from common.utils import random_string TYPE_CHOICES = [ ("ignore", _("Ignore")), + ("reopen", _("Reopen")), ("disable_remote", _("Disable remote")), ("delete_remote", _("Delete remote")), ("delete_both", _("Delete remote")), @@ -32,23 +35,28 @@ class RiskHandler: def handle(self, tp, risk=""): self.risk = risk attr = f"handle_{tp}" - if hasattr(self, attr): - ret = getattr(self, attr)() - self.update_risk_if_need(tp) - return ret - else: - raise ValueError(f"Invalid risk type: {tp}") + + if not hasattr(self, attr): + raise ValidationError(f"Invalid risk type: {tp}") + + getattr(self, attr)() + risk = self.update_risk_if_need(tp) + return risk def update_risk_if_need(self, tp): r = self.get_risk() if not r: return - status = ( - ConfirmOrIgnore.ignored if tp == "ignore" else ConfirmOrIgnore.confirmed - ) + if tp == "ignore": + status = ConfirmOrIgnore.ignored + elif tp == "reopen": + status = ConfirmOrIgnore.pending + else: + status = ConfirmOrIgnore.confirmed r.details.append({**self.process_detail, "action": tp, "status": status}) r.status = status r.save() + return r def get_risk(self): r = AccountRisk.objects.filter(asset=self.asset, username=self.username) @@ -57,19 +65,26 @@ class RiskHandler: return r.first() def handle_ignore(self): - GatheredAccount.objects.filter(asset=self.asset, username=self.username).update(status=ConfirmOrIgnore.ignored) - self.risk = 'ignored' + (GatheredAccount.objects + .filter(asset=self.asset, username=self.username) + .update(status=ConfirmOrIgnore.ignored)) + + def handle_reopen(self): + pass def handle_review(self): pass @property def process_detail(self): - return { + detail = { "datetime": timezone.now().isoformat(), "type": "process", "processor": str(self.request.user), } + if self.request and self.request.data and self.request.data.get('comment'): + detail['comment'] = self.request.data['comment'] + return detail def handle_add_account(self): data = {