diff --git a/commcare_connect/opportunity/management/commands/update_completed_work_paid_date.py b/commcare_connect/opportunity/management/commands/update_completed_work_paid_date.py new file mode 100644 index 00000000..000d1e4f --- /dev/null +++ b/commcare_connect/opportunity/management/commands/update_completed_work_paid_date.py @@ -0,0 +1,23 @@ +from django.core.management import BaseCommand +from django.db import transaction + +from commcare_connect.opportunity.models import OpportunityAccess +from commcare_connect.opportunity.utils.completed_work import update_work_payment_date + + +class Command(BaseCommand): + help = "Updates paid dates from payments for all opportunity accesses" + + def handle(self, *args, **kwargs): + try: + with transaction.atomic(): + accesses = OpportunityAccess.objects.all() + self.stdout.write("Starting to process to update the paid date...") + + for access in accesses: + update_work_payment_date(access) + + self.stdout.write("Process completed") + + except Exception as e: + self.stdout.write(self.style.ERROR(f"An error occurred: {str(e)}")) diff --git a/commcare_connect/opportunity/migrations/0060_completedwork_payment_date.py b/commcare_connect/opportunity/migrations/0060_completedwork_payment_date.py index c8481f41..d1478347 100644 --- a/commcare_connect/opportunity/migrations/0060_completedwork_payment_date.py +++ b/commcare_connect/opportunity/migrations/0060_completedwork_payment_date.py @@ -1,18 +1,6 @@ # Generated by Django 4.2.5 on 2024-10-07 08:54 -from django.db import migrations, models, transaction - -from commcare_connect.opportunity.utils.completed_work import update_work_payment_date - - -@transaction.atomic -def update_paid_date_from_payments(apps, schema_editor): - OpportunityAccess = apps.get_model("opportunity.OpportunityAccess") - Payment = apps.get_model("opportunity.Payment") - CompletedWork = apps.get_model("opportunity.CompletedWork") - accesses = OpportunityAccess.objects.all() - for access in accesses: - update_work_payment_date(access, Payment, CompletedWork) +from django.db import migrations, models class Migration(migrations.Migration): @@ -26,5 +14,4 @@ class Migration(migrations.Migration): name="payment_date", field=models.DateTimeField(null=True), ), - migrations.RunPython(update_paid_date_from_payments, migrations.RunPython.noop), ] diff --git a/commcare_connect/opportunity/utils/completed_work.py b/commcare_connect/opportunity/utils/completed_work.py index 2050e30b..df2d9906 100644 --- a/commcare_connect/opportunity/utils/completed_work.py +++ b/commcare_connect/opportunity/utils/completed_work.py @@ -44,19 +44,9 @@ def update_status(completed_works, opportunity_access, compute_payment=True): opportunity_access.save() -def update_work_payment_date(access: OpportunityAccess, payment_model=None, completed_work_model=None): - """ - Dynamically assign models to avoid issues with historical models during migrations. - Top-level imports use the current model, which may not match the schema at migration - time. This ensures we use historical models during migrations and current models in normal execution. - """ - payment_model_ref = payment_model or Payment - completed_work_model_ref = completed_work_model or CompletedWork - - payments = payment_model_ref.objects.filter(opportunity_access=access).order_by("date_paid") - completed_works = completed_work_model_ref.objects.filter(opportunity_access=access).order_by( - "status_modified_date" - ) +def update_work_payment_date(access: OpportunityAccess): + payments = Payment.objects.filter(opportunity_access=access).order_by("date_paid") + completed_works = CompletedWork.objects.filter(opportunity_access=access).order_by("status_modified_date") if not payments or not completed_works: return @@ -86,4 +76,4 @@ def update_work_payment_date(access: OpportunityAccess, payment_model=None, comp break if works_to_update: - completed_work_model_ref.objects.bulk_update(works_to_update, ["payment_date"]) + CompletedWork.objects.bulk_update(works_to_update, ["payment_date"])