uncloud-mravi/uncloud/opennebula/views.py

28 lines
936 B
Python
Raw Normal View History

2020-02-23 14:33:26 +00:00
from rest_framework import viewsets, generics, permissions
from rest_framework.response import Response
from django.contrib.auth import get_user_model
2020-02-23 14:33:26 +00:00
from .models import VM
from .serializers import VMSerializer, OpenNebulaVMSerializer
2020-02-23 14:33:26 +00:00
class RawVMViewSet(viewsets.ModelViewSet):
queryset = VM.objects.all()
serializer_class = VMSerializer
permission_classes = [permissions.IsAdminUser]
2020-02-23 14:33:26 +00:00
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)