Move Order.add_record to save hook in abstract Product

This commit is contained in:
fnux 2020-03-09 17:25:02 +01:00
parent ff133e81b7
commit a7e9f3c09d
3 changed files with 14 additions and 31 deletions

View file

@ -476,13 +476,6 @@ class Order(models.Model):
def recurring_price(self): def recurring_price(self):
return reduce(lambda acc, record: acc + record.recurring_price, self.records, 0) return reduce(lambda acc, record: acc + record.recurring_price, self.records, 0)
def add_record(self, one_time_price, recurring_price, description):
OrderRecord.objects.create(order=self,
one_time_price=one_time_price,
recurring_price=recurring_price,
description=description)
class OrderRecord(models.Model): class OrderRecord(models.Model):
""" """
Order records store billing informations for products: the actual product Order records store billing informations for products: the actual product
@ -543,6 +536,20 @@ class Product(UncloudModel):
# Default period for all products # Default period for all products
default_recurring_period = RecurringPeriod.PER_MONTH default_recurring_period = RecurringPeriod.PER_MONTH
# Used to save records.
def save(self, *args, **kwargs):
# _state.adding is switched to false after super(...) call.
being_created = self._state.adding
super(Product, self).save(*args, **kwargs)
# Make sure we only create records on creation.
if being_created:
record = OrderRecord(
one_time_price=self.one_time_price,
recurring_price=self.recurring_price(self.recurring_period),
description=self.description)
self.order.orderrecord_set.add(record, bulk=False)
@property @property
def recurring_price(self, recurring_period=RecurringPeriod.PER_MONTH): def recurring_price(self, recurring_period=RecurringPeriod.PER_MONTH):

View file

@ -127,11 +127,6 @@ class VMProductViewSet(ProductViewSet):
# Create VM. # Create VM.
vm = serializer.save(owner=request.user, order=order) vm = serializer.save(owner=request.user, order=order)
# Add Product record to order (VM is mutable, allows to keep history in order).
# XXX: Move this to some kind of on_create hook in parent Product class?
order.add_record(vm.one_time_price,
vm.recurring_price(order.recurring_period), vm.description)
return Response(serializer.data) return Response(serializer.data)
@ -215,9 +210,4 @@ class DCLCreateVMProductViewSet(ProductViewSet):
# Create VM. # Create VM.
vm = serializer.save(owner=request.user, order=order) vm = serializer.save(owner=request.user, order=order)
# Add Product record to order (VM is mutable, allows to keep history in order).
# XXX: Move this to some kind of on_create hook in parent Product class?
order.add_record(vm.one_time_price,
vm.recurring_price(order.recurring_period), vm.description)
return Response(serializer.data) return Response(serializer.data)

View file

@ -38,24 +38,10 @@ class MatrixServiceProductViewSet(ProductViewSet):
vm_data['order'] = order vm_data['order'] = order
vm = VMProduct.objects.create(**vm_data) vm = VMProduct.objects.create(**vm_data)
# XXX: Move this to some kind of on_create hook in parent
# Product class?
order.add_record(
vm.one_time_price,
vm.recurring_price(order.recurring_period),
vm.description)
# Create service. # Create service.
service = serializer.save( service = serializer.save(
order=order, order=order,
owner=self.request.user, owner=self.request.user,
vm=vm) vm=vm)
# XXX: Move this to some kind of on_create hook in parent
# Product class?
order.add_record(
service.one_time_price,
service.recurring_price(order.recurring_period),
service.description)
return Response(serializer.data) return Response(serializer.data)