2018-11-23 02:25:35 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2019-01-16 10:13:16 +00:00
|
|
|
from rest_framework import viewsets, generics
|
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework.response import Response
|
2019-11-15 03:56:51 +00:00
|
|
|
from django.conf import settings
|
2018-11-23 02:25:35 +00:00
|
|
|
|
|
|
|
from common.permissions import IsSuperUser, WithBootstrapToken
|
2019-01-16 10:13:16 +00:00
|
|
|
|
|
|
|
|
2019-03-18 03:34:13 +00:00
|
|
|
from ..models import Terminal
|
|
|
|
from .. import serializers_v2 as serializers
|
2018-11-23 02:25:35 +00:00
|
|
|
|
2019-01-16 10:13:16 +00:00
|
|
|
__all__ = ['TerminalViewSet', 'TerminalRegistrationApi']
|
2018-11-23 02:25:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TerminalViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = Terminal.objects.filter(is_deleted=False)
|
|
|
|
serializer_class = serializers.TerminalSerializer
|
|
|
|
permission_classes = [IsSuperUser]
|
2019-11-15 04:11:58 +00:00
|
|
|
http_method_names = [
|
|
|
|
'get', 'put', 'patch', 'delete', 'head', 'options', 'trace'
|
|
|
|
]
|
2018-11-23 02:25:35 +00:00
|
|
|
|
|
|
|
|
2019-01-16 10:13:16 +00:00
|
|
|
class TerminalRegistrationApi(generics.CreateAPIView):
|
2018-11-23 02:25:35 +00:00
|
|
|
serializer_class = serializers.TerminalRegistrationSerializer
|
|
|
|
permission_classes = [WithBootstrapToken]
|
|
|
|
http_method_names = ['post']
|
2019-01-16 10:13:16 +00:00
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
2019-01-17 10:39:38 +00:00
|
|
|
data = {k: v for k, v in request.data.items()}
|
2019-01-16 10:13:16 +00:00
|
|
|
serializer = serializers.TerminalSerializer(
|
|
|
|
data=data, context={'request': request}
|
|
|
|
)
|
2019-11-15 03:56:51 +00:00
|
|
|
if not settings.SECURITY_SERVICE_ACCOUNT_REGISTRATION:
|
|
|
|
data = {"error": "service account registration disabled"}
|
|
|
|
return Response(data=data, status=status.HTTP_400_BAD_REQUEST)
|
2019-01-16 10:13:16 +00:00
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
terminal = serializer.save()
|
|
|
|
sa_serializer = serializer.sa_serializer_class(instance=terminal.user)
|
|
|
|
data['service_account'] = sa_serializer.data
|
|
|
|
return Response(data, status=status.HTTP_201_CREATED)
|
|
|
|
|