pricing: export/import agenda pricing with billing dates (#67675)
This commit is contained in:
parent
23f5f70a8a
commit
fd54026499
|
@ -367,12 +367,14 @@ class AgendaPricing(models.Model):
|
|||
'date_end': self.date_end.strftime('%Y-%m-%d'),
|
||||
'pricing_data': self.pricing_data,
|
||||
'agendas': [a.slug for a in self.agendas.all()],
|
||||
'billing_dates': [bd.export_json() for bd in self.billingdates.all()],
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def import_json(cls, data, overwrite=False):
|
||||
data = copy.deepcopy(data)
|
||||
agenda_slugs = data.pop('agendas', None) or []
|
||||
billing_dates = data.pop('billing_dates', None) or []
|
||||
data = clean_import_data(cls, data)
|
||||
agendas = []
|
||||
for agenda_slug in agenda_slugs:
|
||||
|
@ -389,6 +391,13 @@ class AgendaPricing(models.Model):
|
|||
if overwrite and not created:
|
||||
agenda_pricing.agendas.clear()
|
||||
agenda_pricing.agendas.add(*agendas)
|
||||
|
||||
if overwrite and not created:
|
||||
agenda_pricing.billingdates.all().delete()
|
||||
for billing_date in billing_dates:
|
||||
billing_date['agenda_pricing'] = agenda_pricing
|
||||
BillingDate.import_json(billing_date)
|
||||
|
||||
return created, agenda_pricing
|
||||
|
||||
@staticmethod
|
||||
|
@ -673,3 +682,16 @@ class BillingDate(models.Model):
|
|||
agenda_pricing = models.ForeignKey(AgendaPricing, on_delete=models.CASCADE, related_name='billingdates')
|
||||
date_start = models.DateField(_('Billing start date'))
|
||||
label = models.CharField(_('Label'), max_length=150)
|
||||
|
||||
def export_json(self):
|
||||
return {
|
||||
'date_start': self.date_start.strftime('%Y-%m-%d'),
|
||||
'label': self.label,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def import_json(cls, data):
|
||||
data = clean_import_data(cls, data)
|
||||
cls.objects.update_or_create(
|
||||
agenda_pricing=data['agenda_pricing'], date_start=data['date_start'], defaults=data
|
||||
)
|
||||
|
|
|
@ -12,7 +12,14 @@ from django.core.management import call_command
|
|||
from django.utils.encoding import force_bytes
|
||||
|
||||
from lingo.agendas.models import Agenda, CheckType, CheckTypeGroup
|
||||
from lingo.pricing.models import AgendaPricing, Criteria, CriteriaCategory, Pricing, PricingCriteriaCategory
|
||||
from lingo.pricing.models import (
|
||||
AgendaPricing,
|
||||
BillingDate,
|
||||
Criteria,
|
||||
CriteriaCategory,
|
||||
Pricing,
|
||||
PricingCriteriaCategory,
|
||||
)
|
||||
from lingo.pricing.utils import import_site
|
||||
from lingo.utils.misc import AgendaImportError
|
||||
|
||||
|
@ -163,6 +170,44 @@ def test_import_export_agenda_pricing(app):
|
|||
assert agenda_pricing.pricing_data == {'foo': 'bar'}
|
||||
|
||||
|
||||
def test_import_export_agenda_pricing_with_billing_dates(app):
|
||||
pricing = Pricing.objects.create(label='Foo')
|
||||
agenda_pricing = AgendaPricing.objects.create(
|
||||
label='Bar',
|
||||
pricing=pricing,
|
||||
date_start=datetime.date(year=2021, month=9, day=1),
|
||||
date_end=datetime.date(year=2021, month=10, day=1),
|
||||
)
|
||||
BillingDate.objects.create(
|
||||
agenda_pricing=agenda_pricing,
|
||||
date_start=datetime.date(year=2021, month=9, day=1),
|
||||
label='Period 1',
|
||||
)
|
||||
BillingDate.objects.create(
|
||||
agenda_pricing=agenda_pricing,
|
||||
date_start=datetime.date(year=2021, month=9, day=15),
|
||||
label='Period 2',
|
||||
)
|
||||
|
||||
output = get_output_of_command('export_pricing_config')
|
||||
|
||||
import_site(data={}, clean=True)
|
||||
assert Pricing.objects.count() == 0
|
||||
assert AgendaPricing.objects.count() == 0
|
||||
assert BillingDate.objects.count() == 0
|
||||
data = json.loads(output)
|
||||
|
||||
import_site(data, overwrite=True)
|
||||
agenda_pricing = AgendaPricing.objects.latest('pk')
|
||||
assert agenda_pricing.billingdates.count() == 2
|
||||
billing_date1 = agenda_pricing.billingdates.all()[0]
|
||||
assert billing_date1.date_start == datetime.date(year=2021, month=9, day=1)
|
||||
assert billing_date1.label == 'Period 1'
|
||||
billing_date2 = agenda_pricing.billingdates.all()[1]
|
||||
assert billing_date2.date_start == datetime.date(year=2021, month=9, day=15)
|
||||
assert billing_date2.label == 'Period 2'
|
||||
|
||||
|
||||
def test_import_export_agenda_with_check_types(app):
|
||||
group = CheckTypeGroup.objects.create(label='foo')
|
||||
agenda = Agenda.objects.create(label='Foo Bar', check_type_group=group)
|
||||
|
|
Loading…
Reference in New Issue