Commit 961abad1 authored by ibuler's avatar ibuler

Add heatbeat

parent 17ade287
# -*- coding: utf-8 -*-
#
from rest_framework.generics import ListCreateAPIView
from rest_framework.generics import ListCreateAPIView, CreateAPIView
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
from .models import Terminal, TerminalHeatbeat
from .serializers import TerminalSerializer, TerminalHeatbeatSerializer
from .hands import IsSuperUserOrTerminalUser
class TerminalApi(ListCreateAPIView):
......@@ -21,7 +22,7 @@ class TerminalApi(ListCreateAPIView):
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},
return Response(data={'data': {'name': name, 'id': terminal.id},
'msg': 'Success'},
status=200)
else:
......@@ -36,20 +37,10 @@ class TerminalApi(ListCreateAPIView):
'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)
return Response(data={'msg': 'Secrete key invalid'}, status=401)
class TerminalHeatbeatApi(CreateAPIView):
model = TerminalHeatbeat
serializer_class = TerminalHeatbeatSerializer
permission_classes = (IsSuperUserOrTerminalUser,)
# -*- coding: utf-8 -*-
#
from users.backends import IsSuperUserOrTerminalUser
......@@ -41,7 +41,7 @@ class Terminal(models.Model):
class TerminalHeatbeat(models.Model):
terminal = models.ForeignKey(Terminal, on_delete=models.CASCADE)
date_timestamp = models.IntegerField()
date_created = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = 'terminal_heatbeat'
......@@ -3,7 +3,7 @@
from rest_framework import serializers
from .models import Terminal
from .models import Terminal, TerminalHeatbeat
class TerminalSerializer(serializers.ModelSerializer):
......@@ -13,5 +13,11 @@ class TerminalSerializer(serializers.ModelSerializer):
'get_type_display']
class TerminalHeatbeatSerializer(serializers.ModelSerializer):
class Meta:
model = TerminalHeatbeat
fields = ['terminal']
if __name__ == '__main__':
pass
......@@ -15,4 +15,5 @@ urlpatterns = [
urlpatterns += [
url(r'^v1/terminal/$', api.TerminalApi.as_view(), name='terminal-list-create-api'),
url(r'^v1/terminal-heatbeat/$', api.TerminalHeatbeatApi.as_view(), name='terminal-heatbeat-api'),
]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment