pricing: M2M between AgendaPricing and Agenda (#67196)
This commit is contained in:
parent
6b32c5f2cb
commit
c694ee5e67
|
@ -52,7 +52,7 @@ class Agenda(models.Model):
|
|||
def export_json(self):
|
||||
return {
|
||||
'slug': self.slug,
|
||||
'pricings': [x.export_json() for x in self.agendapricing_set.all()],
|
||||
'pricings': [x.export_json() for x in self.old_agendapricings.all()],
|
||||
'check_type_group': self.check_type_group.slug if self.check_type_group else None,
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('agendas', '0003_check_type_group'),
|
||||
('pricing', '0004_criteria_default'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='agendapricing',
|
||||
name='agendas',
|
||||
field=models.ManyToManyField(to='agendas.Agenda', related_name='agendapricings'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='agendapricing',
|
||||
name='agenda',
|
||||
field=models.ForeignKey(
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name='old_agendapricings',
|
||||
to='agendas.Agenda',
|
||||
),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,37 @@
|
|||
import copy
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def forwards(apps, schema_editor):
|
||||
AgendaPricing = apps.get_model('pricing', 'AgendaPricing')
|
||||
for agenda_pricing in AgendaPricing.objects.all():
|
||||
agenda_pricing.agendas.set([agenda_pricing.agenda])
|
||||
|
||||
|
||||
def backwards(apps, schema_editor):
|
||||
AgendaPricing = apps.get_model('pricing', 'AgendaPricing')
|
||||
for agenda_pricing in AgendaPricing.objects.all():
|
||||
if agenda_pricing.agendas.count() == 0:
|
||||
agenda_pricing.delete()
|
||||
if agenda_pricing.agendas.count() == 1:
|
||||
agenda_pricing.agenda = agenda_pricing.agendas.first()
|
||||
agenda_pricing.save()
|
||||
else:
|
||||
for agenda in agenda_pricing.agendas.all():
|
||||
new_agenda_pricing = copy.deepcopy(agenda_pricing)
|
||||
new_agenda_pricing.pk = None
|
||||
new_agenda_pricing.agenda_id = agenda.pk
|
||||
new_agenda_pricing.save()
|
||||
agenda_pricing.delete()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('pricing', '0005_agenda_pricing_m2m'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(forwards, reverse_code=backwards),
|
||||
]
|
|
@ -336,7 +336,8 @@ class PricingMatrix:
|
|||
|
||||
|
||||
class AgendaPricing(models.Model):
|
||||
agenda = models.ForeignKey(Agenda, on_delete=models.CASCADE)
|
||||
agenda = models.ForeignKey(Agenda, on_delete=models.CASCADE, null=True, related_name='old_agendapricings')
|
||||
agendas = models.ManyToManyField(Agenda, related_name='agendapricings')
|
||||
pricing = models.ForeignKey(Pricing, on_delete=models.CASCADE)
|
||||
date_start = models.DateField()
|
||||
date_end = models.DateField()
|
||||
|
@ -410,7 +411,7 @@ class AgendaPricing(models.Model):
|
|||
def get_agenda_pricing(agenda, event):
|
||||
start_datetime = datetime.datetime.fromisoformat(event['start_datetime'])
|
||||
try:
|
||||
return agenda.agendapricing_set.get(
|
||||
return agenda.old_agendapricings.get(
|
||||
date_start__lte=start_datetime,
|
||||
date_end__gt=start_datetime,
|
||||
)
|
||||
|
|
|
@ -128,8 +128,8 @@ def test_import_export_agenda_with_pricing(app):
|
|||
|
||||
agenda = Agenda.objects.create(label='Foo Bar')
|
||||
import_site(data, overwrite=True)
|
||||
assert agenda.agendapricing_set.count() == 1
|
||||
agenda_pricing = agenda.agendapricing_set.get()
|
||||
assert agenda.old_agendapricings.count() == 1
|
||||
agenda_pricing = agenda.old_agendapricings.get()
|
||||
assert agenda_pricing.agenda == agenda
|
||||
assert agenda_pricing.pricing == pricing
|
||||
assert agenda_pricing.date_start == datetime.date(year=2021, month=9, day=1)
|
||||
|
@ -137,7 +137,7 @@ def test_import_export_agenda_with_pricing(app):
|
|||
|
||||
# again
|
||||
import_site(data)
|
||||
assert agenda.agendapricing_set.count() == 1
|
||||
assert agenda.old_agendapricings.count() == 1
|
||||
agenda_pricing = AgendaPricing.objects.get(pk=agenda_pricing.pk)
|
||||
assert agenda_pricing.agenda == agenda
|
||||
assert agenda_pricing.pricing == pricing
|
||||
|
@ -151,7 +151,7 @@ def test_import_export_agenda_with_pricing(app):
|
|||
}
|
||||
)
|
||||
import_site(data)
|
||||
assert agenda.agendapricing_set.count() == 2
|
||||
assert agenda.old_agendapricings.count() == 2
|
||||
agenda_pricing = AgendaPricing.objects.latest('pk')
|
||||
assert agenda_pricing.agenda == agenda
|
||||
assert agenda_pricing.pricing == pricing
|
||||
|
|
Loading…
Reference in New Issue