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-27 11:09:29 +00:00
|
|
|
from .models import VMHost, VMProduct, VMSnapshotProduct
|
2020-02-27 19:58:12 +00:00
|
|
|
from uncloud_pay.models import Order, RecurringPeriod
|
2020-02-27 10:59:28 +00:00
|
|
|
from .serializers import VMHostSerializer, VMProductSerializer, VMSnapshotProductSerializer
|
2020-02-28 13:57:45 +00:00
|
|
|
from uncloud_pay.helpers import ProductViewSet
|
2020-02-23 13:07:37 +00:00
|
|
|
|
2020-02-27 14:29:15 +00:00
|
|
|
|
2020-02-27 11:31:20 +00:00
|
|
|
import datetime
|
|
|
|
|
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
|
|
|
|
2020-02-27 11:02:41 +00:00
|
|
|
|
2020-02-28 13:57:45 +00:00
|
|
|
class VMProductViewSet(ProductViewSet):
|
2020-02-25 21:01:55 +00:00
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
serializer_class = VMProductSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return VMProduct.objects.filter(owner=self.request.user)
|
|
|
|
|
|
|
|
def create(self, request):
|
2020-02-27 19:58:12 +00:00
|
|
|
# Create base order.
|
|
|
|
order = Order.objects.create(
|
|
|
|
recurring_period=RecurringPeriod.PER_MONTH,
|
|
|
|
recurring_price=0,
|
|
|
|
one_time_price=0,
|
|
|
|
owner=request.user
|
|
|
|
)
|
|
|
|
|
|
|
|
# Create VM.
|
2020-02-25 21:01:55 +00:00
|
|
|
serializer = VMProductSerializer(data=request.data, context={'request': request})
|
|
|
|
serializer.is_valid(raise_exception=True)
|
2020-02-27 19:58:12 +00:00
|
|
|
vm = serializer.save(owner=request.user, order=order)
|
|
|
|
|
|
|
|
# FIXME: commit everything (VM + order) at once.
|
|
|
|
order.recurring_price = vm.recurring_price(order.recurring_period)
|
|
|
|
order.one_time_price = 0
|
|
|
|
order.save()
|
2020-02-25 21:01:55 +00:00
|
|
|
|
|
|
|
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):
|
2020-02-27 11:31:20 +00:00
|
|
|
serializer = VMSnapshotProductSerializer(data=request.data, context={'request': request})
|
2020-02-27 10:36:50 +00:00
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
2020-02-27 11:02:41 +00:00
|
|
|
# Create order
|
2020-02-27 11:31:20 +00:00
|
|
|
now = datetime.datetime.now()
|
|
|
|
order = Order(owner=request.user,
|
|
|
|
creation_date=now,
|
|
|
|
starting_date=now,
|
|
|
|
recurring_price=20,
|
|
|
|
one_time_price=0,
|
|
|
|
recurring_period="per_month")
|
|
|
|
order.save()
|
|
|
|
|
|
|
|
# FIXME: calculate the gb_* values
|
|
|
|
serializer.save(owner=request.user,
|
|
|
|
order=order,
|
|
|
|
gb_ssd=12,
|
|
|
|
gb_hdd=20)
|
2020-02-27 10:36:50 +00:00
|
|
|
|
|
|
|
return Response(serializer.data)
|