forked from uncloud/uncloud
51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
from django.shortcuts import render
|
|
|
|
from django.contrib.auth.models import User
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from rest_framework import viewsets, permissions
|
|
from rest_framework.response import Response
|
|
|
|
from .models import VMHost, VMProduct, VMSnapshotProduct
|
|
from uncloud_pay.models import Order
|
|
|
|
from .serializers import VMHostSerializer, VMProductSerializer, VMSnapshotProductSerializer
|
|
|
|
class VMHostViewSet(viewsets.ModelViewSet):
|
|
serializer_class = VMHostSerializer
|
|
queryset = VMHost.objects.all()
|
|
permission_classes = [permissions.IsAdminUser]
|
|
|
|
|
|
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)
|
|
|
|
|
|
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)
|
|
|
|
# Create order
|
|
#order = Order()
|
|
|
|
serializer.save(owner=request.user)
|
|
|
|
return Response(serializer.data)
|