2020-02-23 13:07:37 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.shortcuts import get_object_or_404
|
2020-02-25 19:53:12 +00:00
|
|
|
|
|
|
|
from rest_framework import viewsets, permissions
|
2020-02-23 13:07:37 +00:00
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2020-02-25 21:01:55 +00:00
|
|
|
from .models import VMHost, VMProduct
|
2020-02-27 10:59:28 +00:00
|
|
|
from .serializers import VMHostSerializer, VMProductSerializer, VMSnapshotProductSerializer
|
2020-02-23 13:07:37 +00:00
|
|
|
|
2020-02-25 19:53:12 +00:00
|
|
|
class VMHostViewSet(viewsets.ModelViewSet):
|
|
|
|
serializer_class = VMHostSerializer
|
|
|
|
queryset = VMHost.objects.all()
|
|
|
|
permission_classes = [permissions.IsAdminUser]
|
2020-02-25 21:01:55 +00:00
|
|
|
|
|
|
|
class VMProductViewSet(viewsets.ModelViewSet):
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
serializer_class = VMProductSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return VMProduct.objects.filter(owner=self.request.user)
|
|
|
|
|
|
|
|
def create(self, request):
|
|
|
|
serializer = VMProductSerializer(data=request.data, context={'request': request})
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
serializer.save(owner=request.user)
|
|
|
|
|
|
|
|
return Response(serializer.data)
|
2020-02-27 10:36:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VMSnapshotProductViewSet(viewsets.ModelViewSet):
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
serializer_class = VMSnapshotProductSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return VMSnapshotProduct.objects.filter(owner=self.request.user)
|
|
|
|
|
|
|
|
def create(self, request):
|
|
|
|
serializer = VMProductSerializer(data=request.data, context={'request': request})
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
serializer.save(owner=request.user)
|
|
|
|
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
|
|
|
|
class VMSnapshotProductView(viewsets.ViewSet):
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
def list(self, request):
|
|
|
|
queryset = VMSnapshotProduct.objects.filter(owner=request.user)
|
2020-02-27 10:59:28 +00:00
|
|
|
serializer = VMSnapshotProductSerializer(queryset, many=True, context={'request': request})
|
2020-02-27 10:36:50 +00:00
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
def retrieve(self, request, pk=None):
|
|
|
|
queryset = VMSnapshotProduct.objects.filter(owner=request.user)
|
|
|
|
vm = get_object_or_404(queryset, pk=pk)
|
2020-02-27 10:59:28 +00:00
|
|
|
serializer = VMSnapshotProductSerializer(vm, context={'request': request})
|
2020-02-27 10:36:50 +00:00
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
def create(self, request):
|
|
|
|
print(request.data)
|
|
|
|
serializer = VMSnapshotCreateSerializer(data=request.data)
|
|
|
|
|
|
|
|
serializer.gb_ssd = 12
|
|
|
|
serializer.gb_hdd = 120
|
|
|
|
print("F")
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
|
|
|
print(serializer)
|
|
|
|
print("A")
|
|
|
|
serializer.save()
|
|
|
|
print("B")
|
|
|
|
|
|
|
|
|
|
|
|
# snapshot = VMSnapshotProduct(owner=request.user,
|
|
|
|
# **serialzer.data)
|
|
|
|
|
|
|
|
return Response(serializer.data)
|