[BREAKING] make Order a stand-alone version

I think that while the idea of an Orderrecord is good, we might get
away / have a simpler implementation if we only use orders and
reference them where needed.

I saved the previous Order model for easy rollback, if my assumption
is wrong.
This commit is contained in:
Nico Schottelius 2020-05-02 22:48:05 +02:00
parent 028f1ebe6e
commit c835c874d5
2 changed files with 110 additions and 2 deletions

View file

@ -0,0 +1,47 @@
# Generated by Django 3.0.5 on 2020-05-02 20:47
from django.conf import settings
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import uuid
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('uncloud_pay', '0008_auto_20200502_1921'),
]
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)]),
),
migrations.AddField(
model_name='order',
name='replaced_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='uncloud_pay.Order'),
),
migrations.CreateModel(
name='OrderTimothee',
fields=[
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('creation_date', models.DateTimeField(auto_now_add=True)),
('starting_date', models.DateTimeField(default=django.utils.timezone.now)),
('ending_date', models.DateTimeField(blank=True, null=True)),
('recurring_period', models.CharField(choices=[('ONCE', 'Onetime'), ('YEAR', 'Per Year'), ('MONTH', 'Per Month'), ('WEEK', 'Per Week'), ('DAY', 'Per Day'), ('HOUR', 'Per Hour'), ('MINUTE', 'Per Minute'), ('SECOND', 'Per Second')], default='MONTH', max_length=32)),
('bill', models.ManyToManyField(blank=True, editable=False, to='uncloud_pay.Bill')),
('billing_address', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.BillingAddress')),
('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]

View file

@ -809,6 +809,64 @@ class Order(models.Model):
editable=False)
billing_address = models.ForeignKey(BillingAddress, on_delete=models.CASCADE)
# TODO: enforce ending_date - starting_date to be larger than recurring_period.
creation_date = models.DateTimeField(auto_now_add=True)
starting_date = models.DateTimeField(default=timezone.now)
ending_date = models.DateTimeField(blank=True,
null=True)
bill = models.ManyToManyField(Bill,
editable=False,
blank=True)
recurring_period = models.CharField(max_length=32,
choices = RecurringPeriod.choices,
default = RecurringPeriod.PER_MONTH)
one_time_price = models.DecimalField(default=0.0,
max_digits=AMOUNT_MAX_DIGITS,
decimal_places=AMOUNT_DECIMALS,
validators=[MinValueValidator(0)])
recurring_price = models.DecimalField(default=0.0,
max_digits=AMOUNT_MAX_DIGITS,
decimal_places=AMOUNT_DECIMALS,
validators=[MinValueValidator(0)])
replaced_by = models.ForeignKey('self',
on_delete=models.PROTECT,
blank=True,
null=True)
# Trigger initial bill generation at order creation.
def save(self, *args, **kwargs):
if self.ending_date and self.ending_date < self.starting_date:
raise ValidationError("End date cannot be before starting date")
super().save(*args, **kwargs)
Bill.generate_for(self.starting_date.year, self.starting_date.month, self.owner)
# Used by uncloud_pay tests.
@property
def bills(self):
return Bill.objects.filter(order=self)
def __str__(self):
return "Order {} created at {}, {}->{}, recurring period {}. Price one time {}, recurring {}".format(
self.uuid, self.creation_date,
self.starting_date, self.ending_date,
self.recurring_period,
self.one_time_price,
self.recurring_price)
class OrderTimothee(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)
billing_address = models.ForeignKey(BillingAddress, on_delete=models.CASCADE)
# TODO: enforce ending_date - starting_date to be larger than recurring_period.
creation_date = models.DateTimeField(auto_now_add=True)
starting_date = models.DateTimeField(default=timezone.now)
@ -856,10 +914,13 @@ class Order(models.Model):
description=description)
def __str__(self):
return "Order {} created at {}, {}->{}, recurring period {}".format(
return "Order {} created at {}, {}->{}, recurring period {}. Price one time {}, recurring {}".format(
self.uuid, self.creation_date,
self.starting_date, self.ending_date,
self.recurring_period)
self.recurring_period,
self.one_time_price,
self.recurring_price)
class OrderRecord(models.Model):