Add some sample VMs

This commit is contained in:
Nico Schottelius 2020-08-02 00:55:07 +02:00
commit e563780142
5 changed files with 129 additions and 49 deletions

View file

@ -224,7 +224,7 @@ class PaymentMethod(models.Model):
class BillingAddress(models.Model):
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
organization = models.CharField(max_length=100)
organization = models.CharField(max_length=100, blank=True, null=True)
name = models.CharField(max_length=100)
street = models.CharField(max_length=100)
city = models.CharField(max_length=50)
@ -241,17 +241,8 @@ class BillingAddress(models.Model):
]
@staticmethod
def get_addresses_for(user):
return BillingAddress.objects.filter(owner=user)
@classmethod
def get_preferred_address_for(cls, user):
addresses = cls.get_addresses_for(user)
if len(addresses) == 0:
return None
else:
# TODO: allow user to set primary/preferred address
return addresses[0]
def get_address_for(user):
return BillingAddress.objects.get(owner=user, active=True)
def __str__(self):
return "{} - {}, {}, {} {}, {}".format(
@ -608,8 +599,9 @@ class Product(UncloudModel):
order = models.ForeignKey(Order,
on_delete=models.CASCADE,
editable=False,
editable=True,
null=True)
# FIXME: editable=True -> is in the admin, but also editable in DRF
status = models.CharField(max_length=32,
choices=UncloudStatus.choices,
@ -618,25 +610,48 @@ class Product(UncloudModel):
# Default period for all products
default_recurring_period = RecurringPeriod.PER_30D
# Used to save records.
def save(self, *args, **kwargs):
# _state.adding is switched to false after super(...) call.
being_created = self._state.adding
# First time saving - create an order
def create_or_update_order(self, when_to_start=None):
if not when_to_start:
when_to_start = timezone.now()
if not self.order:
billing_address = BillingAddress.get_preferred_address_for(self.owner)
print(billing_address)
billing_address = BillingAddress.get_address_for(self.owner)
if not billing_address:
raise ValidationError("Cannot order without a billing address")
# FIXME: allow user to choose recurring_period
self.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)
recurring_price=self.recurring_price,
description=str(self))
else:
previous_order = self.order
when_to_end = when_to_start - datetime.timedelta(seconds=1)
new_order = Order.objects.create(owner=self.owner,
billing_address=self.order.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),
replaces=self.order)
self.order.end_date = when_to_end
self.order.save()
self.order = new_order
def save(self, *args, **kwargs):
# Create order the first time a product is created
if self._state.adding:
self.create_or_update_order()
super().save(*args, **kwargs)