2022-11-08 09:54:51 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from rest_framework import status, mixins, viewsets
|
2022-12-27 07:27:33 +00:00
|
|
|
from rest_framework.response import Response
|
2022-11-08 09:54:51 +00:00
|
|
|
|
2023-02-16 07:53:45 +00:00
|
|
|
from accounts.models import AutomationExecution
|
2023-02-20 10:00:29 +00:00
|
|
|
from accounts.tasks import execute_account_automation_task
|
2022-11-08 09:54:51 +00:00
|
|
|
from assets import serializers
|
2023-01-16 11:02:09 +00:00
|
|
|
from assets.models import BaseAutomation
|
2022-11-08 09:54:51 +00:00
|
|
|
from common.const.choices import Trigger
|
2022-12-27 07:27:33 +00:00
|
|
|
from orgs.mixins import generics
|
2022-11-08 09:54:51 +00:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'AutomationAssetsListApi', 'AutomationRemoveAssetApi',
|
2022-12-22 12:09:20 +00:00
|
|
|
'AutomationAddAssetApi', 'AutomationNodeAddRemoveApi',
|
2022-12-27 07:27:33 +00:00
|
|
|
'AutomationExecutionViewSet',
|
2022-11-08 09:54:51 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class AutomationAssetsListApi(generics.ListAPIView):
|
2023-01-16 11:02:09 +00:00
|
|
|
model = BaseAutomation
|
2022-11-08 09:54:51 +00:00
|
|
|
serializer_class = serializers.AutomationAssetsSerializer
|
|
|
|
filter_fields = ("name", "address")
|
|
|
|
search_fields = filter_fields
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
pk = self.kwargs.get('pk')
|
2023-01-16 11:02:09 +00:00
|
|
|
return get_object_or_404(self.model, pk=pk)
|
2022-11-08 09:54:51 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
instance = self.get_object()
|
|
|
|
assets = instance.get_all_assets().only(
|
|
|
|
*self.serializer_class.Meta.only_fields
|
|
|
|
)
|
|
|
|
return assets
|
|
|
|
|
|
|
|
|
|
|
|
class AutomationRemoveAssetApi(generics.RetrieveUpdateAPIView):
|
|
|
|
model = BaseAutomation
|
|
|
|
serializer_class = serializers.UpdateAssetSerializer
|
|
|
|
|
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
instance = self.get_object()
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
|
|
|
|
if not serializer.is_valid():
|
|
|
|
return Response({'error': serializer.errors})
|
|
|
|
|
|
|
|
assets = serializer.validated_data.get('assets')
|
|
|
|
if assets:
|
|
|
|
instance.assets.remove(*tuple(assets))
|
|
|
|
return Response({'msg': 'ok'})
|
|
|
|
|
|
|
|
|
|
|
|
class AutomationAddAssetApi(generics.RetrieveUpdateAPIView):
|
|
|
|
model = BaseAutomation
|
|
|
|
serializer_class = serializers.UpdateAssetSerializer
|
|
|
|
|
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
instance = self.get_object()
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
if serializer.is_valid():
|
|
|
|
assets = serializer.validated_data.get('assets')
|
|
|
|
if assets:
|
|
|
|
instance.assets.add(*tuple(assets))
|
|
|
|
return Response({"msg": "ok"})
|
|
|
|
else:
|
|
|
|
return Response({"error": serializer.errors})
|
|
|
|
|
|
|
|
|
|
|
|
class AutomationNodeAddRemoveApi(generics.RetrieveUpdateAPIView):
|
|
|
|
model = BaseAutomation
|
2023-01-16 11:02:09 +00:00
|
|
|
serializer_class = serializers.UpdateNodeSerializer
|
2022-11-08 09:54:51 +00:00
|
|
|
|
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
action_params = ['add', 'remove']
|
|
|
|
action = request.query_params.get('action')
|
|
|
|
if action not in action_params:
|
|
|
|
err_info = _("The parameter 'action' must be [{}]".format(','.join(action_params)))
|
|
|
|
return Response({"error": err_info})
|
|
|
|
|
|
|
|
instance = self.get_object()
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
if serializer.is_valid():
|
|
|
|
nodes = serializer.validated_data.get('nodes')
|
|
|
|
if nodes:
|
|
|
|
# eg: plan.nodes.add(*tuple(assets))
|
|
|
|
getattr(instance.nodes, action)(*tuple(nodes))
|
|
|
|
return Response({"msg": "ok"})
|
|
|
|
else:
|
|
|
|
return Response({"error": serializer.errors})
|
|
|
|
|
|
|
|
|
|
|
|
class AutomationExecutionViewSet(
|
|
|
|
mixins.CreateModelMixin, mixins.ListModelMixin,
|
|
|
|
mixins.RetrieveModelMixin, viewsets.GenericViewSet
|
|
|
|
):
|
|
|
|
search_fields = ('trigger',)
|
|
|
|
filterset_fields = ('trigger', 'automation_id')
|
|
|
|
serializer_class = serializers.AutomationExecutionSerializer
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
tp: str
|
|
|
|
|
2022-11-08 09:54:51 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
queryset = AutomationExecution.objects.all()
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
automation = serializer.validated_data.get('automation')
|
2023-02-20 10:00:29 +00:00
|
|
|
task = execute_account_automation_task.delay(
|
2023-02-19 07:54:33 +00:00
|
|
|
pid=str(automation.pk), trigger=Trigger.manual, tp=self.tp
|
2022-11-08 09:54:51 +00:00
|
|
|
)
|
|
|
|
return Response({'task': task.id}, status=status.HTTP_201_CREATED)
|