jumpserver/apps/terminal/api.py

47 lines
1.9 KiB
Python
Raw Normal View History

2016-10-16 14:12:13 +00:00
# -*- coding: utf-8 -*-
#
2016-10-18 15:49:04 +00:00
from rest_framework.generics import ListCreateAPIView, CreateAPIView
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-10-17 09:28:07 +00:00
from common.utils import unsign, get_object_or_none
2016-10-18 15:49:04 +00:00
from .models import Terminal, TerminalHeatbeat
from .serializers import TerminalSerializer, TerminalHeatbeatSerializer
from .hands import IsSuperUserOrTerminalUser
2016-10-16 14:12:13 +00:00
class TerminalApi(ListCreateAPIView):
queryset = Terminal.objects.all()
serializer_class = TerminalSerializer
2016-10-17 09:28:07 +00:00
permission_classes = (AllowAny,)
def post(self, request, *args, **kwargs):
name = unsign(request.data.get('name', ''))
if name:
terminal = get_object_or_none(Terminal, name=name)
if terminal:
if terminal.is_accepted and terminal.is_active:
2016-10-18 15:49:04 +00:00
return Response(data={'data': {'name': name, 'id': terminal.id},
2016-10-17 09:28:07 +00:00
'msg': 'Success'},
status=200)
else:
return Response(data={'data': {'name': name, 'ip': terminal.ip},
'msg': 'Need admin accept or active it'},
2016-10-18 11:28:36 +00:00
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)
return Response(data={'data': {'name': name, 'ip': terminal.ip},
'msg': 'Need admin accept or active it'},
status=204)
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-10-18 15:49:04 +00:00
class TerminalHeatbeatApi(CreateAPIView):
model = TerminalHeatbeat
serializer_class = TerminalHeatbeatSerializer
permission_classes = (IsSuperUserOrTerminalUser,)