Skip to content

Commit

Permalink
Merge pull request #1135 from jefmoura/419-update-budget-signals
Browse files Browse the repository at this point in the history
Fix Budget signal wasn't calculating correctly
  • Loading branch information
ybekdemir authored Aug 17, 2018
2 parents 7d0f2cb + eb615ab commit 815c817
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
20 changes: 10 additions & 10 deletions workflow/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,22 +330,22 @@ def sync_delete_track_workflowlevel1(sender, instance, **kwargs):


# BUDGET SIGNALS
@receiver(signals.post_save, sender=Budget)
@receiver(signals.pre_save, sender=Budget)
def save_budget_value_from_wfl2(sender, instance, *args, **kwargs):
if instance.workflowlevel2 is not None:
wflvl2 = instance.workflowlevel2

if kwargs.get('created'):
# Sum the new values
wflvl2.total_estimated_budget += instance.proposed_value
wflvl2.actual_cost += instance.actual_value
else:
# Subtract the old values
try:
old_budget = Budget.objects.get(id=instance.id)
# Subtract the old values
wflvl2.total_estimated_budget -= old_budget.proposed_value
wflvl2.actual_cost -= old_budget.actual_value

wflvl2.save()
except Budget.DoesNotExist:
pass
finally:
# Sum the new values
wflvl2.total_estimated_budget += instance.proposed_value
wflvl2.actual_cost += instance.actual_value
wflvl2.save()


@receiver(signals.post_delete, sender=Budget)
Expand Down
26 changes: 25 additions & 1 deletion workflow/tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ def test_save_budget_value_from_wfl2_create(self):
self.assertEqual(wflvl2.total_estimated_budget, 100.00)
self.assertEqual(wflvl2.actual_cost, 10.00)

def test_save_budget_value_from_wfl2_update(self):
def test_save_budget_value_from_wfl2_create_more(self):
"""
When a budget is updated, the related wfl2s total_estimated_budget
should be updated
Expand All @@ -1011,6 +1011,30 @@ def test_save_budget_value_from_wfl2_update(self):
self.assertEqual(wflvl2.total_estimated_budget, 180.00)
self.assertEqual(wflvl2.actual_cost, 30.00)

def test_save_budget_value_from_wfl2_update(self):
"""
When a budget is updated, the 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)
budget = factories.Budget(proposed_value=100,
actual_value=10,
workflowlevel2=wflvl2)

budget.proposed_value = 150
budget.actual_value = 50
budget.save()

wflvl2 = WorkflowLevel2.objects.get(pk=wflvl2.pk)
self.assertEqual(wflvl2.total_estimated_budget, 150.00)
self.assertEqual(wflvl2.actual_cost, 50.00)


@tag('pkg')
class BudgetDeleteTest(TestCase):
Expand Down

0 comments on commit 815c817

Please sign in to comment.