diff --git a/dashboard/framework/models.py b/dashboard/framework/models.py index f2741cc..8b9a0b1 100644 --- a/dashboard/framework/models.py +++ b/dashboard/framework/models.py @@ -166,6 +166,7 @@ def save(self, *args, **kwargs): from projects.models import ( ProjectObjective, ProjectObjectiveCondition, + Commitment ) # avoids circular import super().save(*args, **kwargs) @@ -178,6 +179,13 @@ def save(self, *args, **kwargs): objective=projectobjective.objective, condition=self, ) + for work_cycle in WorkCycle.objects.all(): + Commitment.objects.get_or_create( + work_cycle=work_cycle, + project=projectobjective.project, + objective=projectobjective.objective, + level=self.level, + ) class Meta: ordering = ["objective__name", "level__value"] diff --git a/dashboard/framework/test_models.py b/dashboard/framework/test_models.py index 0647fff..f79d878 100644 --- a/dashboard/framework/test_models.py +++ b/dashboard/framework/test_models.py @@ -153,3 +153,32 @@ def test_new_objective_means_new_commitments( assert project.commitment_set.count() == 2 assert work_cycle.commitment_set.count() == 2 + + +@pytest.mark.django_db +def test_new_condition_with_new_level_backfills_commitment( + project, objective, condition, work_cycle +): + # A new condition at a new level should create a matching commitment + # for existing rows. + + assert ( + Commitment.objects.filter( + project=project, objective=objective, work_cycle=work_cycle + ).count() + == 1 + ) + + new_level = Level.objects.create(name="test_level_2", value=2) + Condition.objects.create( + name="test_condition_2", objective=objective, level=new_level + ) + + # Expected behaviour: creating a new condition/level backfills + # commitments. + assert Commitment.objects.filter( + project=project, + objective=objective, + work_cycle=work_cycle, + level=new_level, + ).exists()