Wrap VM creation in database transaction

This commit is contained in:
fnux 2020-03-02 09:30:51 +01:00
parent 81bd54116a
commit 9e253d497b

View file

@ -1,3 +1,4 @@
from django.db import transaction
from django.shortcuts import render from django.shortcuts import render
from django.contrib.auth.models import User from django.contrib.auth.models import User
@ -27,10 +28,10 @@ class VMProductViewSet(ProductViewSet):
def get_queryset(self): def get_queryset(self):
return VMProduct.objects.filter(owner=self.request.user) return VMProduct.objects.filter(owner=self.request.user)
# Use a database transaction so that we do not get half-created structure
# if something goes wrong.
@transaction.atomic
def create(self, request): def create(self, request):
# TODO: what if something blows-up midway?
# => need a transaction
# Create base order. # Create base order.
order = Order.objects.create( order = Order.objects.create(
recurring_period=RecurringPeriod.PER_MONTH, recurring_period=RecurringPeriod.PER_MONTH,