2018-11-23 02:25:35 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import logging
|
|
|
|
from rest_framework.views import APIView, Response
|
|
|
|
from rest_framework_bulk import BulkModelViewSet
|
|
|
|
|
|
|
|
from common.utils import get_object_or_none
|
|
|
|
from common.permissions import IsOrgAdminOrAppUser
|
2019-03-18 03:34:13 +00:00
|
|
|
from ..models import Session, Task
|
|
|
|
from .. import serializers
|
2018-11-23 02:25:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
__all__ = ['TaskViewSet', 'KillSessionAPI']
|
|
|
|
logger = logging.getLogger(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
class TaskViewSet(BulkModelViewSet):
|
|
|
|
queryset = Task.objects.all()
|
|
|
|
serializer_class = serializers.TaskSerializer
|
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
|
|
|
|
|
|
|
|
|
|
|
class KillSessionAPI(APIView):
|
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
|
|
|
model = Task
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
validated_session = []
|
|
|
|
for session_id in request.data:
|
|
|
|
session = get_object_or_none(Session, id=session_id)
|
|
|
|
if session and not session.is_finished:
|
|
|
|
validated_session.append(session_id)
|
|
|
|
self.model.objects.create(
|
2021-07-29 06:53:04 +00:00
|
|
|
name="kill_session", args=session.id, terminal=session.terminal,
|
|
|
|
kwargs={
|
2021-07-29 07:02:53 +00:00
|
|
|
'terminated_by': str(request.user)
|
2021-07-29 06:53:04 +00:00
|
|
|
}
|
2018-11-23 02:25:35 +00:00
|
|
|
)
|
|
|
|
return Response({"ok": validated_session})
|