From 5f3f0440840839de8d8ba72768d3671f6f6c1b6a Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Fri, 24 Mar 2017 23:27:55 -0300 Subject: [PATCH] Add revert function to migration --- kfet/migrations/0049_auto_20170325_0110.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/kfet/migrations/0049_auto_20170325_0110.py b/kfet/migrations/0049_auto_20170325_0110.py index 0620dd7a..29d15676 100644 --- a/kfet/migrations/0049_auto_20170325_0110.py +++ b/kfet/migrations/0049_auto_20170325_0110.py @@ -6,12 +6,22 @@ from django.db import migrations, models def adapt_operation_types(apps, schema_editor): Operation = apps.get_model("kfet", "Operation") + edits = Operation.objects.filter( + is_checkout=False, + type__in=['withdraw', 'deposit']) - for ope in Operation.objects.all(): - if (not ope.is_checkout and - ope.type in ['deposit', 'withdraw']): - ope.type = 'edit' - ope.save() + for ope in edits: + ope.type = 'edit' + ope.save() + + +def revert_operation_types(apps, schema_editor): + Operation = apps.get_model("kfet", "Operation") + + for ope in Operation.objects.filter(type='edit'): + ope.type = 'deposit' if ope.amount > 0 else 'withdraw' + ope.is_checkout = False + ope.save() class Migration(migrations.Migration): @@ -26,7 +36,7 @@ class Migration(migrations.Migration): name='type', field=models.CharField(choices=[('purchase', 'Achat'), ('deposit', 'Charge'), ('withdraw', 'Retrait'), ('initial', 'Initial'), ('edit', 'Édition')], max_length=8), ), - migrations.RunPython(adapt_operation_types), + migrations.RunPython(adapt_operation_types, revert_operation_types), migrations.RemoveField( model_name='operation', name='is_checkout',