diff --git a/src/country_workspace/workspaces/admin/transformer.py b/src/country_workspace/workspaces/admin/transformer.py index f4412454d..b7754c5ce 100644 --- a/src/country_workspace/workspaces/admin/transformer.py +++ b/src/country_workspace/workspaces/admin/transformer.py @@ -1,15 +1,71 @@ -from django.contrib import admin +from admin_extra_buttons.decorators import button +from django import forms +from django.contrib import admin, messages from django.core.cache import cache +from django.db import models from django.db.models import QuerySet from django.forms import ModelForm -from django.http import HttpRequest +from django.http import HttpRequest, HttpResponse, HttpResponseRedirect +from django.shortcuts import render +from django.urls import reverse +from django.utils.translation import gettext_lazy as _ +from strategy_field.utils import fqn +from country_workspace.models import AsyncJob, Batch from country_workspace.state import state from country_workspace.workspaces.models import CountryTransformer from country_workspace.workspaces.options import WorkspaceModelAdmin from country_workspace.workspaces.sites import workspace +class RunTransformerForm(forms.Form): + class ApplyToOptions(models.TextChoices): + HOUSEHOLDS = "households", _("Households only") + INDIVIDUALS = "individuals", _("Individuals only") + BOTH = "both", _("Households and Individuals") + + batch = forms.ModelChoiceField( + queryset=Batch.objects.none(), + label=_("Batch"), + help_text=_("Select an existing batch to update records before pushing to HOPE."), + ) + apply_to = forms.ChoiceField( + label=_("Apply formula to"), + choices=ApplyToOptions.choices, + help_text=_("Choose which record type should be updated by this formula."), + ) + + def __init__(self, *args: object, **kwargs: object) -> None: + office = kwargs.pop("office", None) + program = kwargs.pop("program", None) + super().__init__(*args, **kwargs) + + qs = Batch.objects.order_by("-import_date") + if office: + qs = qs.filter(country_office=office) + if program: + qs = qs.filter(program=program) + self.fields["batch"].queryset = qs.select_related("program") + + if not program: + self.fields["apply_to"].choices = [ + (self.ApplyToOptions.INDIVIDUALS, self.ApplyToOptions.INDIVIDUALS.label), + (self.ApplyToOptions.BOTH, self.ApplyToOptions.BOTH.label), + ] + return + + if program.is_master_detail: + self.fields["apply_to"].choices = [ + (self.ApplyToOptions.HOUSEHOLDS, self.ApplyToOptions.HOUSEHOLDS.label), + (self.ApplyToOptions.INDIVIDUALS, self.ApplyToOptions.INDIVIDUALS.label), + (self.ApplyToOptions.BOTH, self.ApplyToOptions.BOTH.label), + ] + else: + self.fields["apply_to"].choices = [ + (self.ApplyToOptions.INDIVIDUALS, self.ApplyToOptions.INDIVIDUALS.label), + ] + + @admin.register(CountryTransformer, site=workspace) class CountryTransformerAdmin(WorkspaceModelAdmin): list_display = ("name", "description", "created_by", "created_at") @@ -63,6 +119,72 @@ def delete_queryset(self, request: HttpRequest, queryset: QuerySet[CountryTransf super().delete_queryset(request, queryset) self._invalidate_transformer_cache() + @button( + label="Run Formula on Existing Records", + change_form=True, + html_attrs={"title": "Run this formula in Country Workspace without rule commits"}, + ) + def run_on_existing_records(self, request: HttpRequest, pk: str) -> HttpResponse: + obj = self.get_object(request, pk) + if not obj: + return HttpResponse("Transformer not found", status=404) + + if request.method == "POST" and "apply" in request.POST: + form = RunTransformerForm(request.POST, office=state.tenant, program=state.program) + if form.is_valid(): + batch = form.cleaned_data["batch"] + apply_to = form.cleaned_data["apply_to"] + if not request.user.has_perm("country_workspace.reprocess_batch", batch.program): # type: ignore[attr-defined] + self.message_user( + request, + _("You do not have permission to run formulas on this batch."), + messages.ERROR, + ) + return HttpResponseRedirect(self.get_change_url(request, obj)) + + config: dict[str, int] = {"batch_id": batch.pk} + if batch.program.is_master_detail and apply_to in ( + RunTransformerForm.ApplyToOptions.HOUSEHOLDS, + RunTransformerForm.ApplyToOptions.BOTH, + ): + config["household_transformer_id"] = obj.pk + if apply_to in ( + RunTransformerForm.ApplyToOptions.INDIVIDUALS, + RunTransformerForm.ApplyToOptions.BOTH, + ): + config["individual_transformer_id"] = obj.pk + + job = AsyncJob.objects.create( + description=f"Run formula '{obj.name}' on batch {batch.name}", + type=AsyncJob.JobType.TASK, + owner=request.user, + action=fqn("country_workspace.workspaces.admin.batch.reprocessing.reprocess_batch"), + program=batch.program, + batch=batch, + config=config, + ) + job.queue() + + self.message_user( + request, + _("Formula execution has been scheduled for the selected batch."), + messages.SUCCESS, + ) + return HttpResponseRedirect(reverse("workspace:workspaces_countrybatch_changelist")) + + self.message_user(request, _("Please correct the errors below."), messages.ERROR) + else: + form = RunTransformerForm(office=state.tenant, program=state.program) + + context = self.get_common_context( + request, + pk=pk, + title=_("Run Formula on Existing Records"), + form=form, + transformer=obj, + ) + return render(request, "workspace/admin_extra_buttons/run_transformer_form.html", context) + def _invalidate_transformer_cache(self) -> None: """Invalidate cache keys related to transformers.""" if state.tenant: diff --git a/src/country_workspace/workspaces/templates/workspace/admin_extra_buttons/run_transformer_form.html b/src/country_workspace/workspaces/templates/workspace/admin_extra_buttons/run_transformer_form.html new file mode 100644 index 000000000..c5439be26 --- /dev/null +++ b/src/country_workspace/workspaces/templates/workspace/admin_extra_buttons/run_transformer_form.html @@ -0,0 +1,69 @@ +{% extends 'workspace/_base.html' %} +{% load i18n %} +{% block breadcrumbs %} +{% endblock breadcrumbs %} +{% block content %} +
+
+
+

+ {% translate 'Run Formula on Existing Records' %} +

+
+

+ {% blocktranslate with transformer_name=transformer.name %} + Formula: {{ transformer_name }} + {% endblocktranslate %} +

+

+ {% translate "This executes the selected formula directly in Country Workspace and updates existing records before they are pushed to HOPE." %} +

+

+ {% translate "No admin rule creation, rule commits, or custom code execution is required." %} +

+
+ {% csrf_token %} + {% if form.errors %} +
+

+ {% translate "Please correct the errors below." %} +

+
+ {% endif %} +
+ {% for field in form %} +
+
+ + {{ field }} + {% if field.help_text %} +

+ {{ field.help_text }} +

+ {% endif %} + {% if field.errors %} +
    + {% for error in field.errors %} +
  • + {{ error }} +
  • + {% endfor %} +
+ {% endif %} +
+
+ {% endfor %} +
+
+ + + +
+
+
+
+
+
+{% endblock content %} diff --git a/tests/workspace/admin/test_transformer.py b/tests/workspace/admin/test_transformer.py index 047500210..65d5ef40f 100644 --- a/tests/workspace/admin/test_transformer.py +++ b/tests/workspace/admin/test_transformer.py @@ -3,9 +3,10 @@ import pytest from django.contrib.auth.models import User from django.http import HttpRequest +from django.http.response import HttpResponse from country_workspace.state import state -from country_workspace.workspaces.admin.transformer import CountryTransformerAdmin +from country_workspace.workspaces.admin.transformer import CountryTransformerAdmin, RunTransformerForm from country_workspace.workspaces.models import CountryTransformer @@ -111,3 +112,206 @@ def test_invalidate_transformer_cache_no_tenant(self, mock_cache, transformer_ad state.tenant = None transformer_admin._invalidate_transformer_cache() mock_cache.delete.assert_not_called() + + +class TestRunTransformerForm: + def _build_mock_queryset(self) -> MagicMock: + qs = MagicMock() + qs.filter.return_value = qs + qs.select_related.return_value = qs + qs.all.return_value = qs + return qs + + @patch("country_workspace.workspaces.admin.transformer.Batch.objects") + def test_choices_without_program(self, mock_batch_objects): + qs = self._build_mock_queryset() + mock_batch_objects.order_by.return_value = qs + + form = RunTransformerForm() + + choices = [choice[0] for choice in form.fields["apply_to"].choices] + assert choices == [ + RunTransformerForm.ApplyToOptions.INDIVIDUALS, + RunTransformerForm.ApplyToOptions.BOTH, + ] + + @patch("country_workspace.workspaces.admin.transformer.Batch.objects") + def test_batch_queryset_filters_by_office(self, mock_batch_objects): + qs = self._build_mock_queryset() + mock_batch_objects.order_by.return_value = qs + office = MagicMock() + + RunTransformerForm(office=office) + + qs.filter.assert_any_call(country_office=office) + + @patch("country_workspace.workspaces.admin.transformer.Batch.objects") + def test_choices_master_detail_program(self, mock_batch_objects): + qs = self._build_mock_queryset() + mock_batch_objects.order_by.return_value = qs + program = MagicMock(is_master_detail=True) + + form = RunTransformerForm(program=program) + + choices = [choice[0] for choice in form.fields["apply_to"].choices] + assert choices == [ + RunTransformerForm.ApplyToOptions.HOUSEHOLDS, + RunTransformerForm.ApplyToOptions.INDIVIDUALS, + RunTransformerForm.ApplyToOptions.BOTH, + ] + + @patch("country_workspace.workspaces.admin.transformer.Batch.objects") + def test_choices_non_master_detail_program(self, mock_batch_objects): + qs = self._build_mock_queryset() + mock_batch_objects.order_by.return_value = qs + program = MagicMock(is_master_detail=False) + + form = RunTransformerForm(program=program) + + choices = [choice[0] for choice in form.fields["apply_to"].choices] + assert choices == [RunTransformerForm.ApplyToOptions.INDIVIDUALS] + + +class TestRunOnExistingRecords: + def _build_request(self, method: str = "GET", post_data: dict | None = None, has_perm: bool = True) -> MagicMock: + request = MagicMock(spec=HttpRequest) + request.method = method + request.POST = post_data or {} + request.user = MagicMock(spec=User) + request.user.has_perm.return_value = has_perm + return request + + def test_returns_404_when_transformer_not_found(self, transformer_admin): + request = self._build_request() + with patch( + "country_workspace.workspaces.admin.transformer.CountryTransformerAdmin.get_object", + return_value=None, + ): + response = transformer_admin.run_on_existing_records(transformer_admin, request, "123") + assert response.status_code == 404 + + def test_get_renders_form(self, transformer_admin): + request = self._build_request("GET") + transformer = MagicMock(pk=1, name="T1") + state.tenant = None + state.program = None + mock_form = MagicMock() + + with ( + patch( + "country_workspace.workspaces.admin.transformer.CountryTransformerAdmin.get_object", + return_value=transformer, + ), + patch("country_workspace.workspaces.admin.transformer.RunTransformerForm", return_value=mock_form), + patch( + "country_workspace.workspaces.admin.transformer.render", return_value=HttpResponse("ok") + ) as mock_render, + ): + response = transformer_admin.run_on_existing_records(transformer_admin, request, "1") + + assert response.status_code == 200 + mock_render.assert_called_once() + + def test_post_invalid_form_shows_error(self, transformer_admin): + request = self._build_request("POST", {"apply": "yes"}) + transformer = MagicMock(pk=1, name="T1") + state.tenant = MagicMock() + state.program = MagicMock() + + mock_form = MagicMock() + mock_form.is_valid.return_value = False + + with ( + patch( + "country_workspace.workspaces.admin.transformer.CountryTransformerAdmin.get_object", + return_value=transformer, + ), + patch("country_workspace.workspaces.admin.transformer.RunTransformerForm", return_value=mock_form), + patch.object(transformer_admin, "message_user") as mock_message_user, + patch("country_workspace.workspaces.admin.transformer.render", return_value=HttpResponse("ok")), + ): + transformer_admin.run_on_existing_records(transformer_admin, request, "1") + + mock_message_user.assert_called() + assert "Please correct the errors below." in mock_message_user.call_args.args[1] + + def test_post_valid_without_permission_redirects_to_change(self, transformer_admin): + request = self._build_request("POST", {"apply": "yes"}, has_perm=False) + transformer = MagicMock(pk=1, name="T1") + program = MagicMock(is_master_detail=True) + batch = MagicMock(pk=10, name="Batch 1", program=program) + state.tenant = MagicMock() + state.program = MagicMock() + + mock_form = MagicMock() + mock_form.is_valid.return_value = True + mock_form.cleaned_data = { + "batch": batch, + "apply_to": RunTransformerForm.ApplyToOptions.BOTH, + } + + with ( + patch( + "country_workspace.workspaces.admin.transformer.CountryTransformerAdmin.get_object", + return_value=transformer, + ), + patch( + "country_workspace.workspaces.admin.transformer.RunTransformerForm", return_value=mock_form + ) as mock_form_class, + patch( + "country_workspace.workspaces.admin.transformer.CountryTransformerAdmin.get_change_url", + return_value="/change/", + ) as mock_get_change_url, + patch.object(transformer_admin, "message_user") as mock_message_user, + ): + mock_form_class.ApplyToOptions = RunTransformerForm.ApplyToOptions + response = transformer_admin.run_on_existing_records(transformer_admin, request, "1") + + assert response.status_code == 302 + assert response.url == "/change/" + mock_get_change_url.assert_called_once_with(request, transformer) + assert "do not have permission" in mock_message_user.call_args.args[1] + + def test_post_valid_schedules_job_for_both_in_master_detail(self, transformer_admin): + request = self._build_request("POST", {"apply": "yes"}, has_perm=True) + transformer = MagicMock(pk=99, name="Eligibility Rule") + program = MagicMock(is_master_detail=True) + batch = MagicMock(pk=10, name="Batch 1", program=program) + state.tenant = MagicMock() + state.program = MagicMock() + + mock_form = MagicMock() + mock_form.is_valid.return_value = True + mock_form.cleaned_data = { + "batch": batch, + "apply_to": RunTransformerForm.ApplyToOptions.BOTH, + } + job = MagicMock() + + with ( + patch( + "country_workspace.workspaces.admin.transformer.CountryTransformerAdmin.get_object", + return_value=transformer, + ), + patch( + "country_workspace.workspaces.admin.transformer.RunTransformerForm", return_value=mock_form + ) as mock_form_class, + patch( + "country_workspace.workspaces.admin.transformer.AsyncJob.objects.create", return_value=job + ) as mock_create, + patch("country_workspace.workspaces.admin.transformer.reverse", return_value="/batch/"), + patch.object(transformer_admin, "message_user") as mock_message_user, + ): + mock_form_class.ApplyToOptions = RunTransformerForm.ApplyToOptions + response = transformer_admin.run_on_existing_records(transformer_admin, request, "99") + + assert response.status_code == 302 + assert response.url == "/batch/" + mock_create.assert_called_once() + assert mock_create.call_args.kwargs["config"] == { + "batch_id": 10, + "household_transformer_id": 99, + "individual_transformer_id": 99, + } + job.queue.assert_called_once() + assert "scheduled" in mock_message_user.call_args.args[1].lower()