Changeset View
Changeset View
Standalone View
Standalone View
looper/migrations/0047_fix_order_paid_at.py
| # Generated by Django 2.1.2 on 2018-10-16 13:27 | # Generated by Django 2.1.2 on 2018-10-16 13:27 | ||||
| from typing import Any | |||||
| from django.conf import settings | from django.conf import settings | ||||
| from django.db import migrations | from django.db import migrations | ||||
| from django.db.transaction import atomic | from django.db.transaction import atomic | ||||
| @atomic | @atomic | ||||
| def fix_order_paid_at(apps, schema_editor): | def fix_order_paid_at(apps: Any, schema_editor: Any) -> None: | ||||
| Order = apps.get_model('looper', 'Order') | Order = apps.get_model('looper', 'Order') | ||||
| Transaction = apps.get_model('looper', 'Transaction') | Transaction = apps.get_model('looper', 'Transaction') | ||||
| for order in Order.objects.filter(status='paid', paid_at__isnull=True): | for order in Order.objects.filter(status='paid', paid_at__isnull=True): | ||||
| try: | try: | ||||
| transaction = order.transaction_set.latest() | transaction = order.transaction_set.latest() | ||||
| except Transaction.DoesNotExist: | except Transaction.DoesNotExist: | ||||
| order.paid_at = order.updated_at | order.paid_at = order.updated_at | ||||
| else: | else: | ||||
| order.paid_at = transaction.created_at | order.paid_at = transaction.created_at | ||||
| order.save(update_fields={'paid_at'}) | order.save(update_fields={'paid_at'}) | ||||
| def noop(apps, schema_editor): | def noop(apps: Any, schema_editor: Any) -> None: | ||||
| pass | pass | ||||
| class Migration(migrations.Migration): | class Migration(migrations.Migration): | ||||
| dependencies = [ | dependencies = [ | ||||
| migrations.swappable_dependency(settings.AUTH_USER_MODEL), | migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||||
| ('looper', '0046_c2u_paymentmethod_unique'), | ('looper', '0046_c2u_paymentmethod_unique'), | ||||
| ] | ] | ||||
| operations = [ | operations = [ | ||||
| migrations.RunPython(fix_order_paid_at, noop), | migrations.RunPython(fix_order_paid_at, noop), | ||||
| ] | ] | ||||