From 8a2212bebe5f33f5ff919e2d0716eb75423a91b9 Mon Sep 17 00:00:00 2001 From: yilmazbekdemir Date: Fri, 13 Jul 2018 13:31:38 +0200 Subject: [PATCH] Close #392 - update workflow total_budget when related wfl2 budget deleted --- workflow/signals.py | 11 +++++++++- workflow/tests/test_signals.py | 40 ++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/workflow/signals.py b/workflow/signals.py index 5b4428356..2b1162d92 100644 --- a/workflow/signals.py +++ b/workflow/signals.py @@ -15,7 +15,7 @@ from tola import DEMO_BRANCH, utils, track_sync as tsync from tola.management.commands.loadinitialdata import DEFAULT_WORKFLOW_LEVEL_1S -from workflow.models import (Dashboard, Organization, TolaUser, +from workflow.models import (Dashboard, Organization, TolaUser, Budget, WorkflowLevel1, WorkflowTeam, WorkflowLevel2, ROLE_ORGANIZATION_ADMIN, ROLE_PROGRAM_ADMIN, ROLE_PROGRAM_TEAM, ROLE_VIEW_ONLY, @@ -328,3 +328,12 @@ def sync_save_track_workflowlevel1(sender, instance, **kwargs): def sync_delete_track_workflowlevel1(sender, instance, **kwargs): if settings.TOLA_TRACK_SYNC_ENABLED: tsync.delete_instance(instance) + + +#BUDGET SIGNALS +@receiver(signals.post_delete, sender=Budget) +def delete_budget_value_from_wfl2(sender, instance, *args, **kwargs): + wflvl2 = instance.workflowlevel2 + wflvl2.total_estimated_budget -= instance.proposed_value + wflvl2.actual_cost -= instance.actual_value + wflvl2.save() diff --git a/workflow/tests/test_signals.py b/workflow/tests/test_signals.py index ad530e90e..9b46fd22d 100644 --- a/workflow/tests/test_signals.py +++ b/workflow/tests/test_signals.py @@ -5,6 +5,7 @@ from chargebee.models import Subscription from django.core import mail from django.test import TestCase, override_settings, tag +from django.db.models import Sum from mock import Mock, patch import factories @@ -12,8 +13,8 @@ from tola.management.commands.loadinitialdata import DEFAULT_WORKFLOW_LEVEL_1S from workflow.models import (Dashboard, Organization, WorkflowTeam, ROLE_PROGRAM_ADMIN, ROLE_ORGANIZATION_ADMIN, - ROLE_VIEW_ONLY,ROLE_PROGRAM_TEAM, WorkflowLevel1, - DEFAULT_PROGRAM_NAME) + ROLE_VIEW_ONLY, ROLE_PROGRAM_TEAM, WorkflowLevel1, + WorkflowLevel2, DEFAULT_PROGRAM_NAME) @tag('pkg') @@ -936,3 +937,38 @@ def test_sync_save_delete_disabled(self, mock_tsync): wfl1.delete() self.assertFalse(mock_tsync.delete_instance.called) + + +@tag('pkg') +class BudgetUpdateTest(TestCase): + def setUp(self): + self.organization = factories.Organization() + self.user = factories.User() + self.tola_user = factories.TolaUser(organization=self.organization) + + def test_delete_budget_value_from_wfl2(self): + '''When budget deleted, then related wfl2s total_estimated_budget + should be updated''' + + wflvl1 = factories.WorkflowLevel1(name='WorkflowLevel1', + organization=self.organization) + wflvl2 = factories.WorkflowLevel2(name='WorkflowLevel2', + workflowlevel1=wflvl1, + total_estimated_budget=0, + actual_cost=0) + factories.Budget(proposed_value=100, + actual_value=10, + workflowlevel2=wflvl2) + + budget_2 = factories.Budget(proposed_value=80, + actual_value=20, + workflowlevel2=wflvl2) + + wflvl2 = WorkflowLevel2.objects.get(pk=wflvl2.pk) + self.assertEqual(wflvl2.total_estimated_budget, 180.00) + self.assertEqual(wflvl2.actual_cost, 30.00) + + budget_2.delete() + wflvl2 = WorkflowLevel2.objects.get(pk=wflvl2.pk) + self.assertEqual(wflvl2.total_estimated_budget, 100.00) + self.assertEqual(wflvl2.actual_cost, 10.00)