Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a command to update the paid date. #417

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)}"))
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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),
]
18 changes: 4 additions & 14 deletions commcare_connect/opportunity/utils/completed_work.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
Loading