2024-07-24 10:23:12 +00:00
|
|
|
from django.db.models import Count
|
2023-04-13 11:02:04 +00:00
|
|
|
from rest_framework import generics
|
|
|
|
from rest_framework import serializers
|
|
|
|
from rest_framework.decorators import action
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2022-12-19 03:35:50 +00:00
|
|
|
from assets.const import AllTypes
|
2023-06-30 07:54:06 +00:00
|
|
|
from assets.models import Platform, Node, Asset, PlatformProtocol
|
2024-07-24 10:23:12 +00:00
|
|
|
from assets.serializers import PlatformSerializer, PlatformProtocolSerializer, PlatformListSerializer
|
2023-02-22 12:30:43 +00:00
|
|
|
from common.api import JMSModelViewSet
|
2023-04-13 11:02:04 +00:00
|
|
|
from common.permissions import IsValidUser
|
2023-02-22 12:30:43 +00:00
|
|
|
from common.serializers import GroupedChoiceSerializer
|
2022-04-02 10:35:46 +00:00
|
|
|
|
2023-06-30 07:54:06 +00:00
|
|
|
__all__ = ['AssetPlatformViewSet', 'PlatformAutomationMethodsApi', 'PlatformProtocolViewSet']
|
2022-04-02 10:35:46 +00:00
|
|
|
|
|
|
|
|
2022-04-28 14:54:18 +00:00
|
|
|
class AssetPlatformViewSet(JMSModelViewSet):
|
2022-04-02 10:35:46 +00:00
|
|
|
queryset = Platform.objects.all()
|
2022-04-28 14:54:18 +00:00
|
|
|
serializer_classes = {
|
|
|
|
'default': PlatformSerializer,
|
2024-07-24 10:23:12 +00:00
|
|
|
'list': PlatformListSerializer,
|
2023-06-30 07:54:06 +00:00
|
|
|
'categories': GroupedChoiceSerializer,
|
2022-04-28 14:54:18 +00:00
|
|
|
}
|
2023-09-17 04:50:31 +00:00
|
|
|
filterset_fields = ['name', 'category', 'type']
|
2022-04-02 10:35:46 +00:00
|
|
|
search_fields = ['name']
|
2024-04-08 08:31:47 +00:00
|
|
|
ordering = ['-internal', 'name']
|
2022-04-28 14:54:18 +00:00
|
|
|
rbac_perms = {
|
2022-05-05 08:18:05 +00:00
|
|
|
'categories': 'assets.view_platform',
|
2022-08-29 07:50:25 +00:00
|
|
|
'type_constraints': 'assets.view_platform',
|
2023-04-13 11:02:04 +00:00
|
|
|
'ops_methods': 'assets.view_platform',
|
2023-06-30 07:54:06 +00:00
|
|
|
'filter_nodes_assets': 'assets.view_platform',
|
2022-04-28 14:54:18 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 03:35:50 +00:00
|
|
|
def get_queryset(self):
|
2024-01-03 08:14:27 +00:00
|
|
|
# 因为没有走分页逻辑,所以需要这里 prefetch
|
2024-07-24 10:23:12 +00:00
|
|
|
queryset = super().get_queryset().annotate(assets_amount=Count('assets')).prefetch_related(
|
|
|
|
'protocols', 'automation', 'labels', 'labels__label'
|
2024-01-02 08:11:56 +00:00
|
|
|
)
|
2023-04-10 02:57:44 +00:00
|
|
|
queryset = queryset.filter(type__in=AllTypes.get_types_values())
|
2022-12-19 03:35:50 +00:00
|
|
|
return queryset
|
|
|
|
|
2022-10-27 10:34:25 +00:00
|
|
|
def get_object(self):
|
|
|
|
pk = self.kwargs.get('pk', '')
|
|
|
|
if pk.isnumeric():
|
|
|
|
return super().get_object()
|
|
|
|
return self.get_queryset().get(name=pk)
|
|
|
|
|
2022-04-02 10:35:46 +00:00
|
|
|
def check_object_permissions(self, request, obj):
|
|
|
|
if request.method.lower() in ['delete', 'put', 'patch'] and obj.internal:
|
|
|
|
self.permission_denied(
|
|
|
|
request, message={"detail": "Internal platform"}
|
|
|
|
)
|
|
|
|
return super().check_object_permissions(request, obj)
|
2023-04-13 11:02:04 +00:00
|
|
|
|
|
|
|
@action(methods=['post'], detail=False, url_path='filter-nodes-assets')
|
|
|
|
def filter_nodes_assets(self, request, *args, **kwargs):
|
|
|
|
node_ids = request.data.get('node_ids', [])
|
2023-08-28 07:43:45 +00:00
|
|
|
asset_ids = set(request.data.get('asset_ids', []))
|
|
|
|
platform_ids = set(request.data.get('platform_ids', []))
|
|
|
|
|
|
|
|
if node_ids:
|
|
|
|
nodes = Node.objects.filter(id__in=node_ids)
|
|
|
|
node_asset_ids = Node.get_nodes_all_assets(*nodes).values_list('id', flat=True)
|
|
|
|
asset_ids |= set(node_asset_ids)
|
|
|
|
|
|
|
|
if asset_ids:
|
|
|
|
_platform_ids = Asset.objects \
|
|
|
|
.filter(id__in=set(asset_ids)) \
|
|
|
|
.values_list('platform_id', flat=True)
|
|
|
|
platform_ids |= set(_platform_ids)
|
2023-04-13 11:02:04 +00:00
|
|
|
platforms = Platform.objects.filter(id__in=platform_ids)
|
|
|
|
serializer = self.get_serializer(platforms, many=True)
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
|
2023-06-30 07:54:06 +00:00
|
|
|
class PlatformProtocolViewSet(JMSModelViewSet):
|
|
|
|
queryset = PlatformProtocol.objects.all()
|
|
|
|
serializer_class = PlatformProtocolSerializer
|
|
|
|
filterset_fields = ['name', 'platform__name']
|
|
|
|
rbac_perms = {
|
|
|
|
'*': 'assets.add_platform'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-13 11:02:04 +00:00
|
|
|
class PlatformAutomationMethodsApi(generics.ListAPIView):
|
|
|
|
permission_classes = (IsValidUser,)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def automation_methods():
|
|
|
|
return AllTypes.get_automation_methods()
|
|
|
|
|
|
|
|
def generate_serializer_fields(self):
|
|
|
|
data = self.automation_methods()
|
|
|
|
fields = {
|
2023-04-20 06:05:00 +00:00
|
|
|
i['id']: i['params_serializer'](label=i['name'])
|
2023-04-13 11:02:04 +00:00
|
|
|
if i['params_serializer'] else None
|
|
|
|
for i in data
|
|
|
|
}
|
|
|
|
return fields
|
|
|
|
|
|
|
|
def get_serializer_class(self):
|
|
|
|
fields = self.generate_serializer_fields()
|
|
|
|
serializer_name = 'AutomationMethodsSerializer'
|
|
|
|
return type(serializer_name, (serializers.Serializer,), fields)
|
|
|
|
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
data = self.generate_serializer_fields()
|
|
|
|
serializer = self.get_serializer(data)
|
|
|
|
return Response(serializer.data)
|