Skip to content

Commit cad9284

Browse files
committed
Address validation issues on LivelihoodActivity models for ReliefGiftOther,OtherCashIncome and WildFoodGathering see HEA-984
1 parent 0382ddd commit cad9284

2 files changed

Lines changed: 48 additions & 12 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Generated by Django 5.2.11 on 2026-03-20 03:23
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("baseline", "0028_alter_meatproduction_animals_slaughtered_and_more"),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name="livelihoodactivity",
15+
name="quantity_consumed",
16+
field=models.FloatField(blank=True, null=True, verbose_name="Quantity Consumed"),
17+
),
18+
migrations.AlterField(
19+
model_name="livelihoodactivity",
20+
name="quantity_other_uses",
21+
field=models.FloatField(blank=True, null=True, verbose_name="Quantity Other Uses"),
22+
),
23+
migrations.AlterField(
24+
model_name="livelihoodactivity",
25+
name="quantity_produced",
26+
field=models.FloatField(blank=True, null=True, verbose_name="Quantity Produced"),
27+
),
28+
migrations.AlterField(
29+
model_name="livelihoodactivity",
30+
name="quantity_purchased",
31+
field=models.FloatField(blank=True, null=True, verbose_name="Quantity Purchased"),
32+
),
33+
migrations.AlterField(
34+
model_name="livelihoodactivity",
35+
name="quantity_sold",
36+
field=models.FloatField(blank=True, null=True, verbose_name="Quantity Sold/Exchanged"),
37+
),
38+
]

apps/baseline/models.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,13 +1172,13 @@ class LivelihoodActivity(common_models.Model):
11721172
wealth_group = models.ForeignKey(WealthGroup, on_delete=models.CASCADE, help_text=_("Wealth Group"))
11731173

11741174
# Also used for the quantity received for the PaymentInKind and ReliefGiftsOther Livelihood Strategies
1175-
quantity_produced = models.PositiveIntegerField(blank=True, null=True, verbose_name=_("Quantity Produced"))
1176-
quantity_purchased = models.PositiveIntegerField(blank=True, null=True, verbose_name=_("Quantity Purchased"))
1177-
quantity_sold = models.PositiveIntegerField(blank=True, null=True, verbose_name=_("Quantity Sold/Exchanged"))
1178-
quantity_other_uses = models.PositiveIntegerField(blank=True, null=True, verbose_name=_("Quantity Other Uses"))
1175+
quantity_produced = models.FloatField(blank=True, null=True, verbose_name=_("Quantity Produced"))
1176+
quantity_purchased = models.FloatField(blank=True, null=True, verbose_name=_("Quantity Purchased"))
1177+
quantity_sold = models.FloatField(blank=True, null=True, verbose_name=_("Quantity Sold/Exchanged"))
1178+
quantity_other_uses = models.FloatField(blank=True, null=True, verbose_name=_("Quantity Other Uses"))
11791179
# Can normally be calculated / validated as `quantity_produced + quantity_purchased - quantity_sold - quantity_other_uses` # NOQA: E501
11801180
# but there are exceptions, such as MilkProduction which also stores MilkProduction.quantity_butter_production
1181-
quantity_consumed = models.PositiveIntegerField(blank=True, null=True, verbose_name=_("Quantity Consumed"))
1181+
quantity_consumed = models.FloatField(blank=True, null=True, verbose_name=_("Quantity Consumed"))
11821182

11831183
price = models.FloatField(
11841184
blank=True,
@@ -1356,19 +1356,17 @@ def validate_quantity_consumed(self):
13561356
)
13571357

13581358
# Check if the actual quantity_consumed matches the expected quantity_consumed
1359-
if self.quantity_consumed and self.quantity_consumed != expected_quantity_consumed:
1359+
if self.quantity_consumed and round(self.quantity_consumed, 6) != round(expected_quantity_consumed, 6):
13601360
if quantity_butter_production:
13611361
message = "Quantity consumed for Milk Production must be quantity produced + quantity purchased - quantity sold - quantity used for butter production - quantity used for other things" # NOQA: E501
13621362
else:
13631363
message = "Quantity consumed for a Livelihood Activity must be quantity produced + quantity purchased - quantity sold - quantity used for other things" # NOQA: E501
13641364
raise ValidationError(_(message))
13651365

13661366
def validate_income(self):
1367-
income = self.income or 0
1368-
quantity_sold = self.quantity_sold or 0
1369-
price = self.price or 0
1370-
if self.income and income != quantity_sold * price:
1371-
raise ValidationError(_("Income for a Livelihood Activity must be quantity sold multiplied by price"))
1367+
if self.income and self.quantity_sold is not None and self.price is not None:
1368+
if self.income != self.quantity_sold * self.price:
1369+
raise ValidationError(_("Income for a Livelihood Activity must be quantity sold multiplied by price"))
13721370

13731371
def validate_expenditure(self):
13741372
"""
@@ -2049,7 +2047,7 @@ def validate_income(self):
20492047
"Quantity produced for Other Cash Income must be payment per time * number of people * labor per month * months per year" # NOQA: E501
20502048
)
20512049
)
2052-
if self.income is not None and self.payment_per_time is not None and self.times_per_year is not None:
2050+
elif self.income is not None and self.payment_per_time is not None and self.times_per_year is not None:
20532051
if self.income != self.payment_per_time * self.times_per_year:
20542052
raise ValidationError(_("Income for 'Other Cash Income' must be payment per time * times per year"))
20552053

0 commit comments

Comments
 (0)