jumpserver/apps/terminal/api.py

65 lines
2.3 KiB
Python
Raw Normal View History

2016-10-16 14:12:13 +00:00
# -*- coding: utf-8 -*-
#
2016-11-10 18:13:13 +00:00
from django.core.cache import cache
from django.conf import settings
2016-10-24 11:32:53 +00:00
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView
2016-11-13 14:34:38 +00:00
from rest_framework import viewsets
2016-10-17 09:28:07 +00:00
from rest_framework.views import APIView, Response
from rest_framework.permissions import AllowAny
2016-10-16 14:12:13 +00:00
2016-11-01 09:21:16 +00:00
from common.utils import signer, get_object_or_none
2016-11-13 14:34:38 +00:00
from .models import Terminal, TerminalHeatbeat
2016-10-18 15:49:04 +00:00
from .serializers import TerminalSerializer, TerminalHeatbeatSerializer
from .hands import IsSuperUserOrTerminalUser
2016-10-16 14:12:13 +00:00
2016-11-13 14:34:38 +00:00
class TerminalViewSet(viewsets.ModelViewSet):
2016-10-16 14:12:13 +00:00
queryset = Terminal.objects.all()
serializer_class = TerminalSerializer
2016-10-17 09:28:07 +00:00
permission_classes = (AllowAny,)
2016-11-10 18:13:13 +00:00
def create(self, request, *args, **kwargs):
2016-11-01 09:21:16 +00:00
name = signer.unsign(request.data.get('name', ''))
2016-10-17 09:28:07 +00:00
if name:
terminal = get_object_or_none(Terminal, name=name)
if terminal:
2016-11-10 18:13:13 +00:00
data = {
'data': {'name': name, 'id': terminal.id},
}
2016-10-19 10:33:14 +00:00
if terminal.is_active:
2016-11-10 18:13:13 +00:00
data['msg'] = 'Success'
return Response(data=data, status=200)
2016-10-17 09:28:07 +00:00
else:
2016-11-10 18:13:13 +00:00
data['msg'] = 'Need admin active this terminal'
return Response(data=data, status=203)
2016-10-17 09:28:07 +00:00
else:
ip = request.META.get('X-Real-IP') or request.META.get('REMOTE_ADDR')
terminal = Terminal.objects.create(name=name, ip=ip)
2016-11-10 18:13:13 +00:00
data = {
'data': {'name': name, 'id': terminal.id},
'msg': 'Need admin active this terminal',
}
return Response(data=data, status=201)
2016-10-17 09:28:07 +00:00
else:
2016-10-18 15:49:04 +00:00
return Response(data={'msg': 'Secrete key invalid'}, status=401)
2016-10-16 14:12:13 +00:00
2016-11-13 14:34:38 +00:00
class TerminalHeatbeatApi(ListCreateAPIView):
queryset = TerminalHeatbeat.objects.all()
serializer_class = TerminalHeatbeatSerializer
2016-10-18 15:49:04 +00:00
permission_classes = (IsSuperUserOrTerminalUser,)
2016-10-24 11:32:53 +00:00
2016-11-13 14:34:38 +00:00
class TerminalHeatbeatViewSet(viewsets.ModelViewSet):
queryset = TerminalHeatbeat.objects.all()
serializer_class = TerminalHeatbeatSerializer
permission_classes = (IsSuperUserOrTerminalUser,)
def create(self, request, *args, **kwargs):
terminal = request.user
TerminalHeatbeat.objects.create(terminal=terminal)
return Response({'msg': 'Success'})
2016-11-10 18:13:13 +00:00