One step furter to allow saving of orders w/o explicit recurringperiod

This commit is contained in:
Nico Schottelius 2020-10-06 19:21:37 +02:00
parent 9623a77907
commit c26ff253de
2 changed files with 26 additions and 6 deletions

View file

@ -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'),
),
]

View file

@ -252,7 +252,6 @@ class RecurringPeriod(models.Model):
defaults={ 'duration_seconds': seconds }) defaults={ 'duration_seconds': seconds })
@staticmethod @staticmethod
def secs_to_name(secs): def secs_to_name(secs):
name = "" name = ""
@ -358,8 +357,10 @@ class Product(UncloudModel):
@property @property
def default_recurring_period(self): 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 @classmethod
def populate_db_defaults(cls): def populate_db_defaults(cls):
@ -907,7 +908,7 @@ class Order(models.Model):
if self._state.adding: if self._state.adding:
(self.one_time_price, self.recurring_price) = self.prices (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 self.recurring_period = self.product.default_recurring_period
# FIXME: ensure the recurring period is defined in the product # FIXME: ensure the recurring period is defined in the product
@ -916,7 +917,7 @@ class Order(models.Model):
def __str__(self): def __str__(self):
return f"{self.description} (order={self.id})" return f"Order {self.id} from {self.owner}: {self.product}"
class Bill(models.Model): class Bill(models.Model):
""" """
@ -1263,7 +1264,9 @@ class ProductToRecurringPeriod(models.Model):
constraints = [ constraints = [
models.UniqueConstraint(fields=['product'], models.UniqueConstraint(fields=['product'],
condition=Q(is_default=True), 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): def __str__(self):