Browse Source

Add heatbeat

pull/530/head
ibuler 8 years ago
parent
commit
961abad14b
  1. 29
      apps/terminal/api.py
  2. 5
      apps/terminal/hands.py
  3. 2
      apps/terminal/models.py
  4. 8
      apps/terminal/serializers.py
  5. 1
      apps/terminal/urls.py

29
apps/terminal/api.py

@ -1,13 +1,14 @@
# -*- 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,)

5
apps/terminal/hands.py

@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
#
from users.backends import IsSuperUserOrTerminalUser

2
apps/terminal/models.py

@ -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'

8
apps/terminal/serializers.py

@ -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

1
apps/terminal/urls.py

@ -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'),
]

Loading…
Cancel
Save