Quickly wire vm creation to orders

This commit is contained in:
fnux 2020-02-27 20:58:12 +01:00
parent b1649a6228
commit ef5e7e8035
4 changed files with 21 additions and 6 deletions

View file

@ -35,13 +35,16 @@ router.register(r'order', payviews.OrderViewSet, basename='order')
router.register(r'payment', payviews.PaymentViewSet, basename='payment') router.register(r'payment', payviews.PaymentViewSet, basename='payment')
router.register(r'payment-method', payviews.PaymentMethodViewSet, basename='payment-methods') router.register(r'payment-method', payviews.PaymentMethodViewSet, basename='payment-methods')
# VMs
router.register(r'vm/vm', vmviews.VMProductViewSet, basename='vm')
# admin/staff urls # admin/staff urls
router.register(r'admin/bill', payviews.AdminBillViewSet, basename='admin/bill') router.register(r'admin/bill', payviews.AdminBillViewSet, basename='admin/bill')
router.register(r'admin/payment', payviews.AdminPaymentViewSet, basename='admin/payment') router.register(r'admin/payment', payviews.AdminPaymentViewSet, basename='admin/payment')
router.register(r'admin/order', payviews.AdminOrderViewSet, basename='admin/order') router.register(r'admin/order', payviews.AdminOrderViewSet, basename='admin/order')
router.register(r'admin/vmhost', vmviews.VMHostViewSet) router.register(r'admin/vmhost', vmviews.VMHostViewSet)
router.register(r'admin/opennebula', oneviews.VMViewSet, basename='opennebula') router.register(r'admin/opennebula', oneviews.VMViewSet, basename='opennebula')
router.register(r'admin/opennebula_raw', oneviews.RawVMViewSet) router.register(r'admin/opennebula_raw', oneviews.RawVMViewSet, basename='opennebula_raw')
urlpatterns = [ urlpatterns = [

View file

@ -43,7 +43,7 @@ class Order(models.Model):
editable=False) editable=False)
creation_date = models.DateTimeField(auto_now_add=True) creation_date = models.DateTimeField(auto_now_add=True)
starting_date = models.DateTimeField() starting_date = models.DateTimeField(auto_now_add=True)
ending_date = models.DateTimeField(blank=True, ending_date = models.DateTimeField(blank=True,
null=True) null=True)

View file

@ -44,7 +44,6 @@ class VMProduct(Product):
cores = models.IntegerField() cores = models.IntegerField()
ram_in_gb = models.FloatField() ram_in_gb = models.FloatField()
@property
def recurring_price(self, recurring_period=RecurringPeriod.PER_MONTH): def recurring_price(self, recurring_period=RecurringPeriod.PER_MONTH):
if recurring_period == RecurringPeriod.PER_MONTH: if recurring_period == RecurringPeriod.PER_MONTH:
# TODO: move magic numbers in variables # TODO: move magic numbers in variables

View file

@ -7,8 +7,7 @@ from rest_framework import viewsets, permissions
from rest_framework.response import Response from rest_framework.response import Response
from .models import VMHost, VMProduct, VMSnapshotProduct from .models import VMHost, VMProduct, VMSnapshotProduct
from uncloud_pay.models import Order from uncloud_pay.models import Order, RecurringPeriod
from .serializers import VMHostSerializer, VMProductSerializer, VMSnapshotProductSerializer from .serializers import VMHostSerializer, VMProductSerializer, VMSnapshotProductSerializer
import datetime import datetime
@ -27,9 +26,23 @@ class VMProductViewSet(viewsets.ModelViewSet):
return VMProduct.objects.filter(owner=self.request.user) return VMProduct.objects.filter(owner=self.request.user)
def create(self, request): def create(self, request):
# Create base order.
order = Order.objects.create(
recurring_period=RecurringPeriod.PER_MONTH,
recurring_price=0,
one_time_price=0,
owner=request.user
)
# Create VM.
serializer = VMProductSerializer(data=request.data, context={'request': request}) serializer = VMProductSerializer(data=request.data, context={'request': request})
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
serializer.save(owner=request.user) 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()
return Response(serializer.data) return Response(serializer.data)