from rest_framework import viewsets, generics, permissions from rest_framework.response import Response from django.contrib.auth import get_user_model from .models import VM from .serializers import VMSerializer, OpenNebulaVMSerializer class RawVMViewSet(viewsets.ModelViewSet): queryset = VM.objects.all() serializer_class = VMSerializer permission_classes = [permissions.IsAdminUser] class VMViewSet(viewsets.ModelViewSet): permission_classes = [permissions.IsAuthenticated] def list(self, request): queryset = VM.objects.filter(owner=request.user) serializer = OpenNebulaVMSerializer(queryset, many=True) return Response(serializer.data) def retrieve(self, request, pk=None): queryset = VM.objects.filter(owner=request.user) user = get_object_or_404(queryset, pk=pk) serializer = OpenNebulaVMSerializer(queryset) return Response(serializer.data)