perf: add account details api

pull/14712/head
wangruidong 2024-12-12 15:54:27 +08:00 committed by w940853815
parent d380a15490
commit 71abe93fa5
2 changed files with 36 additions and 3 deletions

View File

@ -77,10 +77,12 @@ class GatheredAccountViewSet(OrgBulkModelViewSet):
serializer_classes = {
"default": serializers.GatheredAccountSerializer,
"status": serializers.GatheredAccountActionSerializer,
"details": serializers.GatheredAccountDetailsSerializer
}
rbac_perms = {
"sync_accounts": "assets.add_gatheredaccount",
"status": "assets.change_gatheredaccount",
"details": "assets.view_gatheredaccount"
}
@action(methods=["put"], detail=True, url_path="status")
@ -102,3 +104,10 @@ class GatheredAccountViewSet(OrgBulkModelViewSet):
handler = RiskHandler(asset, username, request=self.request)
handler.handle_delete_remote()
return Response(status=status.HTTP_200_OK)
@action(methods=["get"], detail=True, url_path="details")
def details(self, request, *args, **kwargs):
pk = kwargs.get('pk')
account = get_object_or_404(GatheredAccount, pk=pk)
serializer = self.get_serializer(account.detail)
return Response(data=serializer.data)

View File

@ -1,4 +1,6 @@
from django.shortcuts import get_object_or_404
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers
from accounts.const import AutomationTypes
from accounts.models import GatherAccountsAutomation
@ -12,6 +14,7 @@ __all__ = [
'GatheredAccountSerializer',
'GatheredAccountActionSerializer',
'GatherAccountAutomationSerializer',
'GatheredAccountDetailsSerializer'
]
@ -20,8 +23,8 @@ class GatherAccountAutomationSerializer(BaseAutomationSerializer):
model = GatherAccountsAutomation
read_only_fields = BaseAutomationSerializer.Meta.read_only_fields
fields = (BaseAutomationSerializer.Meta.fields
+ ['is_sync_account', 'check_risk', 'recipients']
+ read_only_fields)
+ ['is_sync_account', 'check_risk', 'recipients']
+ read_only_fields)
extra_kwargs = {
'check_risk': {
'help_text': _('Whether to check the risk of the gathered accounts.'),
@ -36,6 +39,7 @@ class GatherAccountAutomationSerializer(BaseAutomationSerializer):
class AccountAssetSerializer(_AccountAssetSerializer):
class Meta(_AccountAssetSerializer.Meta):
ref_name = "GatheredAccountAssetSerializer"
fields = [f for f in _AccountAssetSerializer.Meta.fields if f != 'auto_config']
@ -48,7 +52,7 @@ class GatheredAccountSerializer(BulkOrgResourceModelSerializer):
'id', 'asset', 'username',
'date_last_login', 'address_last_login',
'remote_present', 'present',
'date_updated', 'status',
'date_updated', 'status', 'detail'
]
read_only_fields = fields
@ -62,3 +66,23 @@ class GatheredAccountSerializer(BulkOrgResourceModelSerializer):
class GatheredAccountActionSerializer(GatheredAccountSerializer):
class Meta(GatheredAccountSerializer.Meta):
read_only_fields = list(set(GatheredAccountSerializer.Meta.read_only_fields) - {'status'})
class GatheredAccountDetailsSerializer(serializers.Serializer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
request = self.context.get('request')
if not request:
return
params = request.query_params
if params.get('format') == 'openapi':
return
pk = request.parser_context['kwargs'].get('pk')
obj = get_object_or_404(GatheredAccount, pk=pk)
details = obj.detail
for key, value in details.items():
if isinstance(value, bool):
self.fields[key] = serializers.BooleanField(label=key, read_only=True)
else:
self.fields[key] = serializers.CharField(label=key, read_only=True)