Projet

Général

Profil

0002-pricing-export-import-agenda-pricing-with-billing-da.patch

Lauréline Guérin, 29 juillet 2022 21:57

Télécharger (4,89 ko)

Voir les différences:

Subject: [PATCH 02/14] pricing: export/import agenda pricing with billing
 dates (#67675)

 lingo/pricing/models.py             | 22 ++++++++++++++
 tests/pricing/test_import_export.py | 47 ++++++++++++++++++++++++++++-
 2 files changed, 68 insertions(+), 1 deletion(-)
lingo/pricing/models.py
367 367
            'date_end': self.date_end.strftime('%Y-%m-%d'),
368 368
            'pricing_data': self.pricing_data,
369 369
            'agendas': [a.slug for a in self.agendas.all()],
370
            'billing_dates': [bd.export_json() for bd in self.billingdates.all()],
370 371
        }
371 372

  
372 373
    @classmethod
373 374
    def import_json(cls, data, overwrite=False):
374 375
        data = copy.deepcopy(data)
375 376
        agenda_slugs = data.pop('agendas', None) or []
377
        billing_dates = data.pop('billing_dates', None) or []
376 378
        data = clean_import_data(cls, data)
377 379
        agendas = []
378 380
        for agenda_slug in agenda_slugs:
......
389 391
        if overwrite and not created:
390 392
            agenda_pricing.agendas.clear()
391 393
        agenda_pricing.agendas.add(*agendas)
394

  
395
        if overwrite and not created:
396
            agenda_pricing.billingdates.all().delete()
397
        for billing_date in billing_dates:
398
            billing_date['agenda_pricing'] = agenda_pricing
399
            BillingDate.import_json(billing_date)
400

  
392 401
        return created, agenda_pricing
393 402

  
394 403
    @staticmethod
......
673 682
    agenda_pricing = models.ForeignKey(AgendaPricing, on_delete=models.CASCADE, related_name='billingdates')
674 683
    date_start = models.DateField(_('Billing start date'))
675 684
    label = models.CharField(_('Label'), max_length=150)
685

  
686
    def export_json(self):
687
        return {
688
            'date_start': self.date_start.strftime('%Y-%m-%d'),
689
            'label': self.label,
690
        }
691

  
692
    @classmethod
693
    def import_json(cls, data):
694
        data = clean_import_data(cls, data)
695
        cls.objects.update_or_create(
696
            agenda_pricing=data['agenda_pricing'], date_start=data['date_start'], defaults=data
697
        )
tests/pricing/test_import_export.py
12 12
from django.utils.encoding import force_bytes
13 13

  
14 14
from lingo.agendas.models import Agenda, CheckType, CheckTypeGroup
15
from lingo.pricing.models import AgendaPricing, Criteria, CriteriaCategory, Pricing, PricingCriteriaCategory
15
from lingo.pricing.models import (
16
    AgendaPricing,
17
    BillingDate,
18
    Criteria,
19
    CriteriaCategory,
20
    Pricing,
21
    PricingCriteriaCategory,
22
)
16 23
from lingo.pricing.utils import import_site
17 24
from lingo.utils.misc import AgendaImportError
18 25

  
......
163 170
    assert agenda_pricing.pricing_data == {'foo': 'bar'}
164 171

  
165 172

  
173
def test_import_export_agenda_pricing_with_billing_dates(app):
174
    pricing = Pricing.objects.create(label='Foo')
175
    agenda_pricing = AgendaPricing.objects.create(
176
        label='Bar',
177
        pricing=pricing,
178
        date_start=datetime.date(year=2021, month=9, day=1),
179
        date_end=datetime.date(year=2021, month=10, day=1),
180
    )
181
    BillingDate.objects.create(
182
        agenda_pricing=agenda_pricing,
183
        date_start=datetime.date(year=2021, month=9, day=1),
184
        label='Period 1',
185
    )
186
    BillingDate.objects.create(
187
        agenda_pricing=agenda_pricing,
188
        date_start=datetime.date(year=2021, month=9, day=15),
189
        label='Period 2',
190
    )
191

  
192
    output = get_output_of_command('export_pricing_config')
193

  
194
    import_site(data={}, clean=True)
195
    assert Pricing.objects.count() == 0
196
    assert AgendaPricing.objects.count() == 0
197
    assert BillingDate.objects.count() == 0
198
    data = json.loads(output)
199

  
200
    import_site(data, overwrite=True)
201
    agenda_pricing = AgendaPricing.objects.latest('pk')
202
    assert agenda_pricing.billingdates.count() == 2
203
    billing_date1 = agenda_pricing.billingdates.all()[0]
204
    assert billing_date1.date_start == datetime.date(year=2021, month=9, day=1)
205
    assert billing_date1.label == 'Period 1'
206
    billing_date2 = agenda_pricing.billingdates.all()[1]
207
    assert billing_date2.date_start == datetime.date(year=2021, month=9, day=15)
208
    assert billing_date2.label == 'Period 2'
209

  
210

  
166 211
def test_import_export_agenda_with_check_types(app):
167 212
    group = CheckTypeGroup.objects.create(label='foo')
168 213
    agenda = Agenda.objects.create(label='Foo Bar', check_type_group=group)
169
-