diff --git a/uncloud_pay/migrations/0031_auto_20201006_1655.py b/uncloud_pay/migrations/0031_auto_20201006_1655.py new file mode 100644 index 0000000..e56a4cc --- /dev/null +++ b/uncloud_pay/migrations/0031_auto_20201006_1655.py @@ -0,0 +1,17 @@ +# Generated by Django 3.1 on 2020-10-06 16:55 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('uncloud_pay', '0030_auto_20201006_1640'), + ] + + operations = [ + migrations.AddConstraint( + model_name='producttorecurringperiod', + constraint=models.UniqueConstraint(fields=('product', 'recurring_period'), name='recurring_period_once_per_product'), + ), + ] diff --git a/uncloud_pay/models.py b/uncloud_pay/models.py index 47f1c22..6962601 100644 --- a/uncloud_pay/models.py +++ b/uncloud_pay/models.py @@ -252,7 +252,6 @@ class RecurringPeriod(models.Model): defaults={ 'duration_seconds': seconds }) - @staticmethod def secs_to_name(secs): name = "" @@ -358,8 +357,10 @@ class Product(UncloudModel): @property def default_recurring_period(self): - return RecurringPeriod.objects.get(product=self, - is_default=True) + """ + Return the default recurring Period + """ + return self.recurring_periods.get(producttorecurringperiod__is_default=True) @classmethod def populate_db_defaults(cls): @@ -907,7 +908,7 @@ class Order(models.Model): if self._state.adding: (self.one_time_price, self.recurring_price) = self.prices - if not self.recurring_period: + if self.recurring_period_id is None: self.recurring_period = self.product.default_recurring_period # FIXME: ensure the recurring period is defined in the product @@ -916,7 +917,7 @@ class Order(models.Model): def __str__(self): - return f"{self.description} (order={self.id})" + return f"Order {self.id} from {self.owner}: {self.product}" class Bill(models.Model): """ @@ -1263,7 +1264,9 @@ class ProductToRecurringPeriod(models.Model): constraints = [ models.UniqueConstraint(fields=['product'], condition=Q(is_default=True), - name='one_default_recurring_period_per_product') + name='one_default_recurring_period_per_product'), + models.UniqueConstraint(fields=['product', 'recurring_period'], + name='recurring_period_once_per_product') ] def __str__(self):