Skip to content

Commit 80fdb64

Browse files
committed
Minor fixes for stored LivelihoodZoneBaseline._annual_kcals_cost - see HEA-1072
1 parent ca9565e commit 80fdb64

3 files changed

Lines changed: 9 additions & 4 deletions

File tree

apps/baseline/models.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ class Language(models.TextChoices):
348348
verbose_name=_("Currency"),
349349
help_text=_("Default currency for income or expenditure from Livelihood Activities within this Baseline."),
350350
)
351-
# Derived country field - always set by calculate_fields()
351+
# Derived field - always set by calculate_fields()
352352
_annual_kcals_cost = models.FloatField(
353353
blank=True,
354354
null=True,
@@ -459,6 +459,11 @@ def save(self, *args, **kwargs):
459459
exclude=[field.name for field in self._meta.fields if isinstance(field, models.ForeignKey)],
460460
validate_unique=False,
461461
)
462+
463+
# Make sure that _annual_kcals_cost is included in the update_fields if the update_fields argument is provided.
464+
update_fields = kwargs.get("update_fields")
465+
if update_fields is not None:
466+
kwargs["update_fields"] = set(update_fields) | {"_annual_kcals_cost"}
462467
super().save(*args, **kwargs)
463468

464469
def natural_key(self):

apps/baseline/signals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def refresh_baseline_after_livelihood_activity_change(sender, instance, **kwargs
7575
"""
7676
Refresh the LivelihoodZoneBaseline when a FoodPurchase BaselineLivelihoodActivity is changed.
7777
78-
This forces the recalculation of the annual_kcals_cost for the Baseline. The calculation depends on Baseline\
78+
This forces the recalculation of the annual_kcals_cost for the Baseline. The calculation depends on Baseline
7979
FoodPurchase activities so we only need to trigger the refresh for Activities that meet those criteria.
8080
8181
Django doesn't fire signals for subclasses of a model, so we need to listen to LivelihoodActivity and

apps/baseline/tests/test_models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def test_baseline_livelihood_activity_change_resaves_baseline_on_commit(self):
108108
self.baseline.refresh_from_db()
109109
self.assertAlmostEqual(self.baseline.annual_kcals_cost, self.get_expected_annual_kcals_cost())
110110

111-
def test_livelihood_activity_superclass_resaves_baseline_on_commit(self):
111+
def test_baseline_livelihood_activity_superclass_resaves_baseline_on_commit(self):
112112
with self.captureOnCommitCallbacks(execute=True):
113113
activity = BaselineLivelihoodActivity.objects.get(pk=self.main_staple_activity.pk)
114114
activity.price = 4
@@ -119,7 +119,7 @@ def test_livelihood_activity_superclass_resaves_baseline_on_commit(self):
119119
self.main_staple_activity.refresh_from_db()
120120
self.assertAlmostEqual(self.baseline.annual_kcals_cost, self.get_expected_annual_kcals_cost())
121121

122-
def test_baseline_livelihood_activity_superclass_resaves_baseline_on_commit(self):
122+
def test_livelihood_activity_superclass_resaves_baseline_on_commit(self):
123123
with self.captureOnCommitCallbacks(execute=True):
124124
activity = LivelihoodActivity.objects.get(pk=self.main_staple_activity.pk)
125125
activity.price = 5

0 commit comments

Comments
 (0)