Move Order.add_record to save hook in abstract Product

This commit is contained in:
fnux 2020-03-09 17:25:02 +01:00
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):
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):
"""
Order records store billing informations for products: the actual product
@ -543,6 +536,20 @@ class Product(UncloudModel):
# Default period for all products
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
def recurring_price(self, recurring_period=RecurringPeriod.PER_MONTH):