Skip to content

Commit ae797b5

Browse files
authored
Merge pull request #256 from American-Institutes-for-Research/HEA-957/Missing_mandatory_field_animals_slaughtered_new
Hea 957/missing mandatory field animals slaughtered new
2 parents 3226735 + 275b035 commit ae797b5

3 files changed

Lines changed: 54 additions & 6 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Generated by Django 5.2.11 on 2026-02-26 07:23
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("baseline", "0027_alter_seasonalactivity_livelihood_zone_baseline"),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name="meatproduction",
15+
name="animals_slaughtered",
16+
field=models.PositiveSmallIntegerField(
17+
blank=True, null=True, verbose_name="Number of animals slaughtered"
18+
),
19+
),
20+
migrations.AlterField(
21+
model_name="meatproduction",
22+
name="carcass_weight",
23+
field=models.FloatField(blank=True, null=True, verbose_name="Carcass weight per animal"),
24+
),
25+
]

apps/baseline/models.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,14 +1723,25 @@ class MeatProduction(LivelihoodActivity):
17231723
"""
17241724

17251725
# Production calculation /validation is `input_quantity` * `item_yield`
1726-
animals_slaughtered = models.PositiveSmallIntegerField(verbose_name=_("Number of animals slaughtered"))
1727-
carcass_weight = models.FloatField(verbose_name=_("Carcass weight per animal"))
1726+
animals_slaughtered = models.PositiveSmallIntegerField(
1727+
verbose_name=_("Number of animals slaughtered"), null=True, blank=True
1728+
)
1729+
carcass_weight = models.FloatField(verbose_name=_("Carcass weight per animal"), null=True, blank=True)
1730+
1731+
def clean(self):
1732+
super().clean()
1733+
# If animals were slaughtered, carcass weight must be recorded
1734+
if self.animals_slaughtered and self.animals_slaughtered > 0 and self.carcass_weight is None:
1735+
raise ValidationError(_("Carcass weight is required when animals slaughtered"))
17281736

17291737
def validate_quantity_produced(self):
1730-
if self.quantity_produced and self.quantity_produced != self.animals_slaughtered * self.carcass_weight:
1731-
raise ValidationError(
1732-
_("Quantity Produced for a Meat Production must be animals slaughtered multiplied by carcass weight")
1733-
)
1738+
if self.quantity_produced is not None and self.animals_slaughtered and self.carcass_weight is not None:
1739+
if self.quantity_produced != self.animals_slaughtered * self.carcass_weight:
1740+
raise ValidationError(
1741+
_(
1742+
"Quantity Produced for a Meat Production must be animals slaughtered multiplied by carcass weight"
1743+
)
1744+
)
17341745

17351746
class Meta:
17361747
verbose_name = LivelihoodStrategyType.MEAT_PRODUCTION.label

pipelines/assets/livelihood_activity.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,18 @@ def get_instances_from_dataframe(
943943
# to the list, provided that it has at least one Livelihood Activity where there is some income,
944944
# expediture or consumption. This excludes empty activities that only contain attributes for,
945945
# for example, 'type_of_milk_sold_or_other_uses'.
946+
# For MeatProduction, if animals_slaughtered is null/0 or carcass_weight is null/0, then a
947+
# percentage_kcals of 0 is not meaningful (it results from the BSS formula evaluating to 0
948+
# because there are no animals or no carcass weight). Set it to None so that the activity is
949+
# treated as empty and not saved to the database.
950+
if livelihood_strategy and livelihood_strategy["strategy_type"] == "MeatProduction":
951+
for livelihood_activity in livelihood_activities_for_strategy:
952+
if (
953+
not livelihood_activity.get("animals_slaughtered")
954+
or not livelihood_activity.get("carcass_weight")
955+
) and livelihood_activity.get("percentage_kcals") == 0:
956+
livelihood_activity["percentage_kcals"] = None
957+
946958
# Also ignore any livelihood activities that don't have a Wealth Category component to the Wealth Group
947959
# natural key. These are from blank columns between Wealth Category groups in the BSS, which sometimes
948960
# contain data where values or formulae have been copied across all the columns in a row.

0 commit comments

Comments
 (0)