diff --git a/uncloud_pay/admin.py b/uncloud_pay/admin.py index 190550e..421b9b7 100644 --- a/uncloud_pay/admin.py +++ b/uncloud_pay/admin.py @@ -12,9 +12,17 @@ from django.template.loader import render_to_string from uncloud_pay.models import Bill, Order, BillRecord, BillingAddress + class BillRecordInline(admin.TabularInline): model = Bill.bill_records.through +# AT some point in the future: expose REPLACED and orders that depend on us +# class OrderInline(admin.TabularInline): +# model = Order +# fk_name = "replaces" +# class OrderAdmin(admin.ModelAdmin): +# inlines = [ OrderInline ] + class BillAdmin(admin.ModelAdmin): inlines = [ BillRecordInline ] @@ -69,6 +77,7 @@ class BillAdmin(admin.ModelAdmin): admin.site.register(Bill, BillAdmin) +#admin.site.register(Order, OrderAdmin) admin.site.register(Order) admin.site.register(BillRecord) admin.site.register(BillingAddress) diff --git a/uncloud_pay/management/commands/add-opennebula-vm-orders.py b/uncloud_pay/management/commands/add-opennebula-vm-orders.py index d90c0ad..504c5d6 100644 --- a/uncloud_pay/management/commands/add-opennebula-vm-orders.py +++ b/uncloud_pay/management/commands/add-opennebula-vm-orders.py @@ -7,6 +7,7 @@ from datetime import datetime, timedelta from uncloud_pay.models import * #import opennebula.models as one from uncloud_vm.models import * +import sys def vm_price_2020(cpu=1, ram=2, v6only=False): if v6only: @@ -49,15 +50,23 @@ class Command(BaseCommand): # 25206 - vm25206 = VMProduct.objects.create(name="OpenNebula 25206", + vm25206 = VMProduct(name="OpenNebula 25206", cores=1, ram_in_gb=4, owner=user) - vm25206_ssd = VMDiskProduct.objects.create(vm=vm25206, + vm25206.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3))) + vm25206.save() + + vm25206_ssd = VMDiskProduct(vm=vm25206, owner=user, size_in_gb=30) + vm25206_ssd.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3))) + vm25206_ssd.save() + + sys.exit(0) + vm25206.cores = 2 vm25206.ram_in_gb = 8 vm25206.save() @@ -107,6 +116,7 @@ class Command(BaseCommand): vm25208_ssd.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,8,5))) + # 25207 vm25207 = VMProduct.objects.create(name="OpenNebula 25207", cores=1, ram_in_gb=4, diff --git a/uncloud_pay/models.py b/uncloud_pay/models.py index 781cd95..1de8061 100644 --- a/uncloud_pay/models.py +++ b/uncloud_pay/models.py @@ -581,6 +581,16 @@ class Product(UncloudModel): # Default period for all products default_recurring_period = RecurringPeriod.PER_30D + def create_order_at(self, when_to_start, *args, **kwargs): + billing_address = BillingAddress.get_address_for(self.owner) + + order = Order.objects.create(owner=self.owner, + billing_address=billing_address, + starting_date=when_to_start, + one_time_price=self.one_time_price, + recurring_period=self.default_recurring_period, + recurring_price=self.recurring_price, + description=str(self)) def create_or_update_order(self, when_to_start=None): if not when_to_start: @@ -620,8 +630,8 @@ class Product(UncloudModel): def save(self, *args, **kwargs): - # Create order the first time a product is created - if self._state.adding: + # Create order if there is none already + if not self.order: self.create_or_update_order() super().save(*args, **kwargs)