Commit WIP changes for /order, if needed at any point

This commit is contained in:
fnux 2020-02-27 20:28:48 +01:00
parent 809a55e1dd
commit 38d3a3a5d3
4 changed files with 61 additions and 57 deletions

View file

@ -0,0 +1,45 @@
import uuid
from django.db import models
from django.contrib.auth import get_user_model
from django.utils.translation import gettext_lazy as _
# See https://docs.djangoproject.com/en/dev/ref/models/fields/#field-choices-enum-types
class RecurringPeriod(models.TextChoices):
ONE_TIME = 'ONCE', _('Onetime')
PER_YEAR = 'YEAR', _('Per Year')
PER_MONTH = 'MONTH', _('Per Month')
PER_MINUTE = 'MINUTE', _('Per Minute')
PER_DAY = 'DAY', _('Per Day')
PER_HOUR = 'HOUR', _('Per Hour')
PER_SECOND = 'SECOND', _('Per Second')
class Product(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
owner = models.ForeignKey(get_user_model(),
on_delete=models.CASCADE,
editable=False)
description = ""
status = models.CharField(max_length=256,
choices = (
('pending', 'Pending'),
('being_created', 'Being created'),
('active', 'Active'),
('deleted', 'Deleted')
),
default='pending'
)
order = models.ForeignKey('uncloud_pay.Order',
on_delete=models.CASCADE,
editable=False,
null=True)
@property
def recurring_price(self, recurring_period=RecurringPeriod.PER_MONTH):
pass # To be implemented in child.
class Meta:
abstract = True

View file

@ -1,23 +1,15 @@
import uuid
from django.db import models from django.db import models
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.core.validators import MinValueValidator from django.core.validators import MinValueValidator
from django.utils.translation import gettext_lazy as _
import uuid from .helpers import RecurringPeriod
import uncloud_vm.models as vmmodels
AMOUNT_MAX_DIGITS=10 AMOUNT_MAX_DIGITS=10
AMOUNT_DECIMALS=2 AMOUNT_DECIMALS=2
# See https://docs.djangoproject.com/en/dev/ref/models/fields/#field-choices-enum-types
class RecurringPeriod(models.TextChoices):
ONE_TIME = 'ONCE', _('Onetime')
PER_YEAR = 'YEAR', _('Per Year')
PER_MONTH = 'MONTH', _('Per Month')
PER_MINUTE = 'MINUTE', _('Per Minute')
PER_DAY = 'DAY', _('Per Day')
PER_HOUR = 'HOUR', _('Per Hour')
PER_SECOND = 'SECOND', _('Per Second')
class Bill(models.Model): class Bill(models.Model):
owner = models.ForeignKey(get_user_model(), owner = models.ForeignKey(get_user_model(),
on_delete=models.CASCADE) on_delete=models.CASCADE)
@ -63,8 +55,7 @@ class Order(models.Model):
@property @property
def products(self): def products(self):
# Blows up due to circular dependency... # Blows up due to circular dependency...
# vms = VMProduct.objects.filter(order=self) vms = vmmodels.VMProduct.objects.all() #filter(order=self)
vms = []
return vms return vms
# def amount(self): # def amount(self):
@ -116,36 +107,3 @@ class Payment(models.Model):
), ),
default='unknown') default='unknown')
timestamp = models.DateTimeField(editable=False) timestamp = models.DateTimeField(editable=False)
class Product(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
owner = models.ForeignKey(get_user_model(),
on_delete=models.CASCADE,
editable=False)
description = ""
status = models.CharField(max_length=256,
choices = (
('pending', 'Pending'),
('being_created', 'Being created'),
('active', 'Active'),
('deleted', 'Deleted')
),
default='pending'
)
order = models.ForeignKey(Order,
on_delete=models.CASCADE,
editable=False,
null=True)
@property
def recurring_price(self, recurring_period=RecurringPeriod.PER_MONTH):
pass # To be implemented in child.
class Meta:
abstract = True

View file

@ -23,7 +23,7 @@ class PaymentMethodSerializer(serializers.ModelSerializer):
fields = ['owner', 'primary', 'source', 'description'] fields = ['owner', 'primary', 'source', 'description']
class ProductSerializer(serializers.Serializer): class ProductSerializer(serializers.Serializer):
vms = VMProductSerializer(many=True, required=False) vms = VMProductSerializer(many=True, required=False, queryset=VMProduct.objects.all())
def create(self, validated_data): def create(self, validated_data):
owner = validated_data.pop('owner') owner = validated_data.pop('owner')
@ -31,7 +31,7 @@ class ProductSerializer(serializers.Serializer):
vms = validated_data.pop('vms') vms = validated_data.pop('vms')
for vm in vms: for vm in vms:
VMProduct.objects.create(owner=owner, order=order, **vm) print(VMProduct.objects.create(owner=owner, order=order, **vm))
return True # FIXME: shoudl return created objects return True # FIXME: shoudl return created objects

View file

@ -1,9 +1,10 @@
from django.db import models
from django.contrib.auth import get_user_model
import uuid import uuid
from uncloud_pay.models import Product, RecurringPeriod from django.db import models
import uncloud_pay.models as pay_models from django.contrib.auth import get_user_model
import uncloud_pay.models as paymodels
import uncloud_pay.helpers as payhelpers
class VMHost(models.Model): class VMHost(models.Model):
@ -33,7 +34,7 @@ class VMHost(models.Model):
) )
class VMProduct(Product): class VMProduct(payhelpers.Product):
vmhost = models.ForeignKey(VMHost, vmhost = models.ForeignKey(VMHost,
on_delete=models.CASCADE, on_delete=models.CASCADE,
editable=False, editable=False,
@ -45,8 +46,8 @@ class VMProduct(Product):
ram_in_gb = models.FloatField() ram_in_gb = models.FloatField()
@property @property
def recurring_price(self, recurring_period=RecurringPeriod.PER_MONTH): def recurring_price(self, recurring_period=paymodels.RecurringPeriod.PER_MONTH):
if recurring_period == RecurringPeriod.PER_MONTH: if recurring_period == paymodels.RecurringPeriod.PER_MONTH:
# TODO: move magic numbers in variables # TODO: move magic numbers in variables
return self.cores * 3 + self.ram_in_gb * 2 return self.cores * 3 + self.ram_in_gb * 2
else: else:
@ -79,7 +80,7 @@ class VMNetworkCard(models.Model):
mac_address = models.IntegerField() mac_address = models.IntegerField()
class VMSnapshotProduct(Product): class VMSnapshotProduct(payhelpers.Product):
price_per_gb_ssd = 0.35 price_per_gb_ssd = 0.35
price_per_gb_hdd = 1.5/100 price_per_gb_hdd = 1.5/100