perf: update risk add reopen action

pull/14823/head
ibuler 2025-01-14 18:13:17 +08:00
parent cf8c4ea050
commit 6b661b6aae
2 changed files with 30 additions and 14 deletions

View File

@ -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):

View File

@ -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 = {