Fix a few errors on preview billing rework

Another WIP commit to sync with laptop, do not forget to rebase!
This commit is contained in:
fnux 2020-03-01 12:23:04 +01:00
parent e319d1d151
commit be2b0a8855
4 changed files with 25 additions and 8 deletions

View file

@ -5,6 +5,8 @@ from rest_framework.viewsets import GenericViewSet
from django.db.models import Q from django.db.models import Q
from .models import Bill, Payment, PaymentMethod, Order from .models import Bill, Payment, PaymentMethod, Order
from django.utils import timezone from django.utils import timezone
from django.core.exceptions import ObjectDoesNotExist
from dateutil.relativedelta import relativedelta
def sum_amounts(entries): def sum_amounts(entries):
return reduce(lambda acc, entry: acc + entry.amount, entries, 0) return reduce(lambda acc, entry: acc + entry.amount, entries, 0)

View file

@ -0,0 +1,21 @@
# Generated by Django 3.0.3 on 2020-02-29 14:59
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('uncloud_pay', '0010_merge_20200228_1303'),
]
operations = [
migrations.RemoveField(
model_name='order',
name='one_time_price',
),
migrations.RemoveField(
model_name='order',
name='recurring_price',
),
]

View file

@ -10,7 +10,7 @@ from uncloud_vm.models import VMProduct
class BillSerializer(serializers.ModelSerializer): class BillSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Bill model = Bill
fields = ['owner', 'amount', 'due_date', 'creation_date', fields = ['owner', 'total', 'due_date', 'creation_date',
'starting_date', 'ending_date'] 'starting_date', 'ending_date']
class PaymentSerializer(serializers.ModelSerializer): class PaymentSerializer(serializers.ModelSerializer):

View file

@ -31,21 +31,15 @@ class VMProductViewSet(ProductViewSet):
# Create base order. # Create base order.
order = Order.objects.create( order = Order.objects.create(
recurring_period=RecurringPeriod.PER_MONTH, recurring_period=RecurringPeriod.PER_MONTH,
recurring_price=0,
one_time_price=0,
owner=request.user owner=request.user
) )
order.save()
# Create VM. # 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)
vm = serializer.save(owner=request.user, order=order) 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)