Source code for festivalgrid.api.viewsets

from rest_framework.authentication import SessionAuthentication, BasicAuthentication, TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from rest_framework import viewsets
from rest_framework import routers
from rest_framework import status
from rest_framework import parsers
from rest_framework import response
from rest_framework.decorators import action

from ..models import Node, Connection, Device, ImageLayer
from . import serializers


[docs]class NodeViewSet(viewsets.ModelViewSet): queryset = Node.objects.all().order_by('name') serializer_class = serializers.NodeSerializer
[docs]class ConnectionViewSet(viewsets.ModelViewSet): queryset = Connection.objects.all() serializer_class = serializers.ConnectionSerializer
[docs]class DeviceViewSet(viewsets.ModelViewSet): queryset = Device.objects.all().order_by('name') serializer_class = serializers.DeviceSerializer
[docs]class ImageLayerViewSet(viewsets.ModelViewSet): queryset = ImageLayer.objects.all() serializer_class = serializers.ImageLayerSerializer
[docs] @action(detail=True, methods=['PUT'], serializer_class=serializers.ImageLayerUploadSerializer, parser_classes=[parsers.MultiPartParser], ) def image(self, request): obj = self.get_object() serializer = self.serializer_class(obj, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return response.Response(serializer.data) return response.Response(serializer.errors, status.HTTP_400_BAD_REQUEST)
[docs]class StatsViewSet(viewsets.ViewSet): serializer_class = serializers.StatsSerializer
[docs] def list(self, request): stats = {'nodes': Node.objects.all()} serializer = serializers.StatsSerializer(instance=stats) return Response(serializer.data)
[docs]class APIRootView(APIView): authentication_classes = (SessionAuthentication, BasicAuthentication, TokenAuthentication) permission_classes = (IsAuthenticated,)
[docs] def get(self, request): return Response({})
router = routers.DefaultRouter() router.register('node', NodeViewSet) router.register('connection', ConnectionViewSet) router.register('device', DeviceViewSet) router.register('imagelayer', ImageLayerViewSet) router.register('stats', StatsViewSet, basename="stats")