2016-10-16 14:12:13 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2017-02-06 15:13:27 +00:00
|
|
|
from collections import OrderedDict
|
2016-11-10 18:13:13 +00:00
|
|
|
from django.core.cache import cache
|
|
|
|
from django.conf import settings
|
2016-12-25 09:44:39 +00:00
|
|
|
import copy
|
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-12-25 09:44:39 +00:00
|
|
|
from rest_framework.decorators import api_view
|
2016-10-16 14:12:13 +00:00
|
|
|
|
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
|
2016-12-27 16:28:52 +00:00
|
|
|
from .hands import IsSuperUserOrAppUser, IsAppUser, User
|
2016-12-25 09:44:39 +00:00
|
|
|
from common.utils import get_object_or_none
|
2016-12-19 17:19:50 +00:00
|
|
|
|
2016-10-16 14:12:13 +00:00
|
|
|
|
2016-12-25 09:44:39 +00:00
|
|
|
class TerminalRegisterView(ListCreateAPIView):
|
2016-12-21 16:36:31 +00:00
|
|
|
queryset = Terminal.objects.all()
|
|
|
|
serializer_class = TerminalSerializer
|
|
|
|
permission_classes = (AllowAny,)
|
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
2016-12-25 09:44:39 +00:00
|
|
|
name = request.data.get('name', '')
|
2017-01-20 05:59:53 +00:00
|
|
|
remote_addr = request.META.get('X-Real-IP') or \
|
|
|
|
request.META.get('REMOTE_ADDR')
|
|
|
|
serializer = self.serializer_class(
|
|
|
|
data={'name': name, 'remote_addr': remote_addr})
|
2016-12-25 09:44:39 +00:00
|
|
|
|
|
|
|
if get_object_or_none(Terminal, name=name):
|
2017-01-20 05:59:53 +00:00
|
|
|
return Response({'msg': 'Already register, Need '
|
|
|
|
'administrator active it'}, status=200)
|
2016-12-25 09:44:39 +00:00
|
|
|
|
|
|
|
if serializer.is_valid():
|
|
|
|
terminal = serializer.save()
|
|
|
|
app_user, access_key = terminal.create_related_app_user()
|
2017-02-06 15:13:27 +00:00
|
|
|
data = OrderedDict()
|
2017-01-20 05:59:53 +00:00
|
|
|
data['terminal'] = copy.deepcopy(serializer.data)
|
2016-12-25 09:44:39 +00:00
|
|
|
data['user'] = app_user.to_json()
|
|
|
|
data['access_key_id'] = access_key.id
|
|
|
|
data['access_key_secret'] = access_key.secret
|
|
|
|
return Response(data, status=201)
|
|
|
|
else:
|
2016-12-27 16:28:52 +00:00
|
|
|
data = {'msg': 'Not valid', 'detail': ';'.join(serializer.errors)}
|
|
|
|
return Response(data, status=400)
|
2016-12-21 16:36:31 +00:00
|
|
|
|
2016-12-25 09:44:39 +00:00
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
return Response('', status=404)
|
2016-12-21 16:36:31 +00:00
|
|
|
|
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-12-25 09:44:39 +00:00
|
|
|
permission_classes = (IsSuperUserOrAppUser,)
|
2016-10-17 09:28:07 +00:00
|
|
|
|
2016-11-10 18:13:13 +00:00
|
|
|
def create(self, request, *args, **kwargs):
|
2016-12-25 09:44:39 +00:00
|
|
|
return Response({'msg': 'Use register view except that'}, status=404)
|
2016-10-24 11:32:53 +00:00
|
|
|
|
2017-01-20 05:59:53 +00:00
|
|
|
# def destroy(self, request, *args, **kwargs):
|
|
|
|
# instance = self.get_object()
|
|
|
|
# if instance.user is not None:
|
|
|
|
# instance.user.delete()
|
|
|
|
# return super(TerminalViewSet, self).destroy(request, *args, **kwargs)
|
2017-01-08 05:35:59 +00:00
|
|
|
|
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
|
2016-12-27 16:28:52 +00:00
|
|
|
permission_classes = (IsAppUser,)
|
2016-11-13 14:34:38 +00:00
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
2016-12-27 16:28:52 +00:00
|
|
|
terminal = request.user.terminal
|
2016-11-13 14:34:38 +00:00
|
|
|
TerminalHeatbeat.objects.create(terminal=terminal)
|
2016-12-27 16:28:52 +00:00
|
|
|
return Response({'msg': 'Success'}, status=201)
|