diff --git a/apps/applications/api.py b/apps/applications/api.py index 6a6b348e6..968d32d2e 100644 --- a/apps/applications/api.py +++ b/apps/applications/api.py @@ -9,11 +9,12 @@ from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIV from rest_framework import viewsets from rest_framework.views import APIView, Response from rest_framework.permissions import AllowAny +from django.shortcuts import get_object_or_404 from rest_framework.decorators import api_view from .models import Terminal, TerminalHeatbeat from .serializers import TerminalSerializer, TerminalHeatbeatSerializer -from .hands import IsSuperUserOrAppUser, IsAppUser, User +from .hands import IsSuperUserOrAppUser, IsAppUser, User, ProxyLog from common.utils import get_object_or_none @@ -64,6 +65,9 @@ class TerminalViewSet(viewsets.ModelViewSet): # instance.user.delete() # return super(TerminalViewSet, self).destroy(request, *args, **kwargs) +tasks = OrderedDict() +# tasks = {1: [{'name': 'kill_proxy', 'proxy_log_id': 23}]} + class TerminalHeatbeatViewSet(viewsets.ModelViewSet): queryset = TerminalHeatbeat.objects.all() @@ -73,4 +77,24 @@ class TerminalHeatbeatViewSet(viewsets.ModelViewSet): def create(self, request, *args, **kwargs): terminal = request.user.terminal TerminalHeatbeat.objects.create(terminal=terminal) - return Response({'msg': 'Success'}, status=201) + task = tasks.get(terminal.name) + tasks[terminal.name] = [] + return Response({'msg': 'Success', + 'tasks': task,}, + status=201) + + +class TerminateConnectionView(APIView): + def post(self, request, *args, **kwargs): + proxy_log_id = request.data.get('proxy_log_id') + proxy_log = get_object_or_404(ProxyLog, id=proxy_log_id) + terminal_id = proxy_log.terminal + if terminal_id in tasks: + tasks[terminal_id].append({'name': 'kill_proxy', + 'proxy_log_id': proxy_log_id}) + else: + tasks[terminal_id] = [{'name': 'kill_proxy', + 'proxy_log_id': proxy_log_id}] + + print(tasks) + return Response({'msg': 'get it'}) diff --git a/apps/applications/urls/api_urls.py b/apps/applications/urls/api_urls.py index 58815b774..ed0e42c32 100644 --- a/apps/applications/urls/api_urls.py +++ b/apps/applications/urls/api_urls.py @@ -14,7 +14,10 @@ router.register(r'v1/terminal/heatbeat', api.TerminalHeatbeatViewSet, 'terminal- router.register(r'v1/terminal', api.TerminalViewSet, 'terminal') urlpatterns = [ - url(r'^v1/terminal/register/$', api.TerminalRegisterView.as_view(), name='terminal-register'), + url(r'^v1/terminal/register/$', api.TerminalRegisterView.as_view(), + name='terminal-register'), + url(r'^v1/terminate/connection/$', api.TerminateConnectionView.as_view(), + name='terminate-connection') # url(r'^v1/terminal/heatbeat/$', api.TestHeatbeat.as_view()) ] diff --git a/apps/audits/api.py b/apps/audits/api.py index 6932d07ea..302fc3a69 100644 --- a/apps/audits/api.py +++ b/apps/audits/api.py @@ -14,7 +14,6 @@ from . import models, serializers from .hands import IsSuperUserOrAppUser, IsAppUser -# Todo: 忘记当时为何不和ProxyLogViewSet复用了 class ProxyLogReceiveView(generics.CreateAPIView): queryset = models.ProxyLog.objects.all() serializer_class = serializers.ProxyLogSerializer diff --git a/apps/audits/templates/audits/proxy_log_list.html b/apps/audits/templates/audits/proxy_log_list.html index 42e0ef097..e89e19a85 100644 --- a/apps/audits/templates/audits/proxy_log_list.html +++ b/apps/audits/templates/audits/proxy_log_list.html @@ -64,6 +64,7 @@