make price a real property of order

Signed-off-by: Nico Schottelius <nico@nico-notebook.schottelius.org>
This commit is contained in:
Nico Schottelius 2020-08-01 18:27:12 +02:00
parent 64780bfc6c
commit cbd5a08ae7
3 changed files with 68 additions and 17 deletions

View file

@ -0,0 +1,23 @@
# Generated by Django 3.0.6 on 2020-08-01 16:04
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('uncloud_pay', '0003_auto_20200621_1442'),
]
operations = [
migrations.RemoveField(
model_name='order',
name='replaced_by',
),
migrations.AddField(
model_name='order',
name='replaces',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='replaced_by', to='uncloud_pay.Order'),
),
]

View file

@ -0,0 +1,24 @@
# Generated by Django 3.0.6 on 2020-08-01 16:26
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('uncloud_pay', '0004_auto_20200801_1604'),
]
operations = [
migrations.AddField(
model_name='order',
name='one_time_price',
field=models.DecimalField(decimal_places=2, default=0.0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)]),
),
migrations.AddField(
model_name='order',
name='recurring_price',
field=models.DecimalField(decimal_places=2, default=0.0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)]),
),
]

View file

@ -6,6 +6,9 @@ from django.core.validators import MinValueValidator
from django.utils import timezone from django.utils import timezone
from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
import logging import logging
from functools import reduce from functools import reduce
import itertools import itertools
@ -296,13 +299,13 @@ class Order(models.Model):
billing_address = models.ForeignKey(BillingAddress, billing_address = models.ForeignKey(BillingAddress,
on_delete=models.CASCADE) on_delete=models.CASCADE)
description = models.TextField() description = models.TextField()
# TODO: enforce ending_date - starting_date to be larger than recurring_period. # TODO: enforce ending_date - starting_date to be larger than recurring_period.
creation_date = models.DateTimeField(auto_now_add=True) creation_date = models.DateTimeField(auto_now_add=True)
starting_date = models.DateTimeField(default=timezone.now) starting_date = models.DateTimeField(default=timezone.now)
ending_date = models.DateTimeField(blank=True, ending_date = models.DateTimeField(blank=True, null=True)
null=True)
recurring_period = models.IntegerField(choices = RecurringPeriod.choices, recurring_period = models.IntegerField(choices = RecurringPeriod.choices,
default = RecurringPeriod.PER_30D) default = RecurringPeriod.PER_30D)
@ -317,17 +320,17 @@ class Order(models.Model):
decimal_places=AMOUNT_DECIMALS, decimal_places=AMOUNT_DECIMALS,
validators=[MinValueValidator(0)]) validators=[MinValueValidator(0)])
replaced_by = models.ForeignKey('self', replaces = models.ForeignKey('self',
related_name='supersede', related_name='replaced_by',
on_delete=models.PROTECT, on_delete=models.PROTECT,
blank=True, blank=True,
null=True) null=True)
depends_on = models.ForeignKey('self', depends_on = models.ForeignKey('self',
related_name='parent_of', related_name='parent_of',
on_delete=models.PROTECT, on_delete=models.PROTECT,
blank=True, blank=True,
null=True) null=True)
@property @property
def count_billed(self): def count_billed(self):
@ -395,13 +398,14 @@ class Order(models.Model):
def records(self): def records(self):
return OrderRecord.objects.filter(order=self) return OrderRecord.objects.filter(order=self)
@property # these are reald fields!!!
def one_time_price(self): # @property
return reduce(lambda acc, record: acc + record.one_time_price, self.records, 0) # def one_time_price(self):
# return reduce(lambda acc, record: acc + record.one_time_price, self.records, 0)
@property # @property
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)
# Used by uncloud_pay tests. # Used by uncloud_pay tests.
@property @property