Skip to content

Commit

Permalink
Merge pull request #1119 from yilmazbekdemir/issue-392
Browse files Browse the repository at this point in the history
Close #392 - update workflow total_budget when related wfl2 budget deleted
  • Loading branch information
Rafael Muñoz Cárdenas authored Jul 13, 2018
2 parents 7dcd395 + 8a2212b commit 5bea4ee
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
11 changes: 10 additions & 1 deletion workflow/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
40 changes: 38 additions & 2 deletions workflow/tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
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
from tola import DEMO_BRANCH
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')
Expand Down Expand Up @@ -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)

0 comments on commit 5bea4ee

Please sign in to comment.