From 751347436637222a4b84e76e6bf74d4fedac36c6 Mon Sep 17 00:00:00 2001 From: ibuler Date: Mon, 17 Oct 2016 17:28:07 +0800 Subject: [PATCH] Finish terminal app --- apps/terminal/api.py | 41 ++++++++++++++++++++++++++++++++++++ apps/terminal/models.py | 2 +- apps/terminal/serializers.py | 2 +- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/apps/terminal/api.py b/apps/terminal/api.py index 7232ad5ea..dae1dea17 100644 --- a/apps/terminal/api.py +++ b/apps/terminal/api.py @@ -2,7 +2,10 @@ # from rest_framework.generics import ListCreateAPIView +from rest_framework.views import APIView, Response +from rest_framework.permissions import AllowAny +from common.utils import unsign, get_object_or_none from .models import Terminal from .serializers import TerminalSerializer @@ -10,5 +13,43 @@ from .serializers import TerminalSerializer class TerminalApi(ListCreateAPIView): queryset = Terminal.objects.all() serializer_class = TerminalSerializer + 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: + return Response(data={'data': {'name': name, 'ip': terminal.ip}, + 'msg': 'Success'}, + status=200) + else: + return Response(data={'data': {'name': name, 'ip': terminal.ip}, + 'msg': 'Need admin accept or active it'}, + status=204) + + 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: + return Response(data={'error': 'Secrete key invalid'}, status=401) + + +# class TerminalRegister(APIView): +# def post(self, request, format='json'): +# return Response(data={'hello': request.META.get('REMOTE_ADDR')}) +# name = unsign(request.data.get('name', '')) +# if name: +# terminal = get_object_or_none(Terminal, name=name) +# if terminal: +# return Response(data={'name': name, 'ip': terminal.ip}, status=200) +# else: +# ip = request.Meta.get('X-Real-IP') +# Terminal.objects.create(name=name, ip=request.META.host) + diff --git a/apps/terminal/models.py b/apps/terminal/models.py index fe429ad27..efc47edf5 100644 --- a/apps/terminal/models.py +++ b/apps/terminal/models.py @@ -11,7 +11,7 @@ class Terminal(models.Model): ('S', 'SSH Terminal'), ('WT', 'Web Terminal') ) - name = models.CharField(max_length=30, verbose_name=_('Name')) + name = models.CharField(max_length=30, unique=True, verbose_name=_('Name')) ip = models.GenericIPAddressField(verbose_name=_('From ip')) is_active = models.BooleanField(default=False, verbose_name=_('Is active')) is_bound_ip = models.BooleanField(default=False, verbose_name=_('Is bound ip')) diff --git a/apps/terminal/serializers.py b/apps/terminal/serializers.py index 8f786abcc..d2986dff6 100644 --- a/apps/terminal/serializers.py +++ b/apps/terminal/serializers.py @@ -9,7 +9,7 @@ from .models import Terminal class TerminalSerializer(serializers.ModelSerializer): class Meta: model = Terminal - fields = ['name', 'ip', 'type', 'url', 'comment', 'is_active', + fields = ['name', 'ip', 'type', 'url', 'comment', 'is_active', 'is_accepted', 'get_type_display']