From b37e7c4c41f36c5ef06f126a067a95e595553340 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Sat, 8 Jun 2019 15:02:12 +0200 Subject: [PATCH] Migrations --- bda/migrations/0014_attribution_paid_field.py | 31 +++++++++++++++ bda/migrations/0015_move_bda_payment.py | 38 +++++++++++++++++++ .../0016_delete_participant_paid.py | 13 +++++++ 3 files changed, 82 insertions(+) create mode 100644 bda/migrations/0014_attribution_paid_field.py create mode 100644 bda/migrations/0015_move_bda_payment.py create mode 100644 bda/migrations/0016_delete_participant_paid.py diff --git a/bda/migrations/0014_attribution_paid_field.py b/bda/migrations/0014_attribution_paid_field.py new file mode 100644 index 00000000..b5bb6208 --- /dev/null +++ b/bda/migrations/0014_attribution_paid_field.py @@ -0,0 +1,31 @@ +# Generated by Django 2.2 on 2019-06-03 19:13 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [("bda", "0013_merge_20180524_2123")] + + operations = [ + migrations.AddField( + model_name="attribution", + name="paid", + field=models.BooleanField(default=False, verbose_name="Payée"), + ), + migrations.AddField( + model_name="attribution", + name="paymenttype", + field=models.CharField( + blank=True, + choices=[ + ("cash", "Cash"), + ("cb", "CB"), + ("cheque", "Chèque"), + ("autre", "Autre"), + ], + max_length=6, + verbose_name="Moyen de paiement", + ), + ), + ] diff --git a/bda/migrations/0015_move_bda_payment.py b/bda/migrations/0015_move_bda_payment.py new file mode 100644 index 00000000..261cf483 --- /dev/null +++ b/bda/migrations/0015_move_bda_payment.py @@ -0,0 +1,38 @@ +# Generated by Django 2.2 on 2019-06-03 19:30 + +from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist +from django.db import migrations + + +def set_attr_payment(apps, schema_editor): + Attribution = apps.get_model("bda", "Attribution") + for attr in Attribution.objects.all(): + attr.paid = attr.participant.paid + attr.paymenttype = attr.participant.paymenttype + attr.save() + + +def set_participant_payment(apps, schema_editor): + Participant = apps.get_model("bda", "Participant") + for part in Participant.objects.all(): + attr_set = part.attribution_set + part.paid = attr_set.exists() and not attr_set.filter(paid=False).exists() + try: + # S'il n'y a qu'un seul type de paiement, on le set + part.paymenttype = ( + attr_set.values_list("paymenttype", flat=True).distinct().get() + ) + # Sinon, whatever + except (ObjectDoesNotExist, MultipleObjectsReturned) as e: + print(e) + pass + part.save() + + +class Migration(migrations.Migration): + + dependencies = [("bda", "0014_attribution_paid_field")] + + operations = [ + migrations.RunPython(set_attr_payment, set_participant_payment, atomic=True) + ] diff --git a/bda/migrations/0016_delete_participant_paid.py b/bda/migrations/0016_delete_participant_paid.py new file mode 100644 index 00000000..f59d1eb9 --- /dev/null +++ b/bda/migrations/0016_delete_participant_paid.py @@ -0,0 +1,13 @@ +# Generated by Django 2.2 on 2019-06-03 19:30 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [("bda", "0015_move_bda_payment")] + + operations = [ + migrations.RemoveField(model_name="participant", name="paid"), + migrations.RemoveField(model_name="participant", name="paymenttype"), + ]