Self hosted FLOSS fitness/workout, nutrition and weight tracker
Per-metric value limits for measurements
Roland Geider507083bf2572
diff --git a/wger/measurements/api/serializers.py b/wger/measurements/api/serializers.pyindex dc3f371a6..345f3f4a0 100644--- a/wger/measurements/api/serializers.py+++ b/wger/measurements/api/serializers.py@@ -20,6 +20,11 @@ from decimal import Decimalfrom rest_framework import serializers# wger+from wger.measurements.limits import (+ VALUE_DECIMAL_PLACES,+ VALUE_MAX_DIGITS,+ limits_for,+)from wger.measurements.models import (Category,Measurement,@@ -186,12 +191,13 @@ class MeasurementSerializer(serializers.ModelSerializer):Measurement serializer"""- # Manually set the serializer to set the coerce_to_string option+ # Manually set the serializer to set the coerce_to_string option. The upper+ # bound is the technical cap of the column, what a value may actually be is+ # decided by the metric type of its category, see validate()value = serializers.DecimalField(- max_digits=6,- decimal_places=2,- min_value=Decimal(0.0),- max_value=Decimal(5000.0),+ max_digits=VALUE_MAX_DIGITS,+ decimal_places=VALUE_DECIMAL_PLACES,+ min_value=Decimal(0),coerce_to_string=False,)@@ -235,7 +241,7 @@ class MeasurementSerializer(serializers.ModelSerializer):def validate(self, data):"""- Body weight entries only support the weight units of the user profile+ The unit and the range of the value both depend on the metric type"""category = data.get('category') or (self.instance.category if self.instance else None)extra_data = data.get('extra_data', self.instance.extra_data if self.instance else {})@@ -246,4 +252,25 @@ class MeasurementSerializer(serializers.ModelSerializer):raise serializers.ValidationError({'extra_data': 'Body weight entries only support kg and lb as unit'})++ self._validate_value_range(data, category, unit)return data++ def _validate_value_range(self, data, category, unit):+ """+ A value has to be in the range its metric type allows.++ The bound hangs off the category the entry lands in, so it is checked+ here rather than on the field: the same endpoint takes body weights in+ kilograms and daily step counts. Since a category can hold mixed units,+ the entry's own unit decides which of them applies.+ """+ value = data.get('value', self.instance.value if self.instance else None)+ if value is None or category is None:+ return++ limits = limits_for(category.metric_type, unit or category.unit)+ if not limits.min <= value <= limits.max:+ raise serializers.ValidationError(+ {'value': f'Value must be between {limits.min} and {limits.max}'}+ )diff --git a/wger/measurements/limits.py b/wger/measurements/limits.pynew file mode 100644index 000000000..e5207cebc--- /dev/null+++ b/wger/measurements/limits.py@@ -0,0 +1,114 @@+# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License+# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.++# Standard Library+from decimal import Decimal+from typing import NamedTuple++# wger+from wger.measurements.models.category import MetricType+++VALUE_MAX_DIGITS = 8+VALUE_DECIMAL_PLACES = 2++SCHEMA_MAX_VALUE = Decimal(10) ** (VALUE_MAX_DIGITS - VALUE_DECIMAL_PLACES) - Decimal('0.01')+"""+Largest value the column can hold. It is what a category without a metric type+is bounded by, since nothing about a free-form category says more.+"""+++class Limits(NamedTuple):+ """+ The range a measurement value of one metric type may be in.++ ``min``/``max`` are enforced by the API, a value outside them is a 400.+ ``soft_min``/``soft_max`` are the everyday range: they are meant for+ warnings and chart axes in the clients and are never enforced anywhere.+ """++ min: Decimal+ max: Decimal+ soft_min: Decimal | None = None+ soft_max: Decimal | None = None+++# Body weight is the only metric whose values come in more than one unit, so it+# is also the only one whose bounds are per unit (see wger-project/wger#1019,+# where a single 300 kg cap locked out everyone entering pounds)+_BODY_WEIGHT_KG = Limits(Decimal(20), Decimal(350), Decimal(30), Decimal(300))+_SLEEP_STAGE = Limits(Decimal(0), Decimal(1440), Decimal(0), Decimal(720))++METRIC_LIMITS: dict[str, dict[str | None, Limits]] = {+ MetricType.BODY_WEIGHT: {+ 'kg': _BODY_WEIGHT_KG,+ 'lb': Limits(Decimal(44), Decimal(770), Decimal(66), Decimal(661)),+ None: _BODY_WEIGHT_KG,+ },+ MetricType.BODY_FAT: {None: Limits(Decimal(2), Decimal(60), Decimal(5), Decimal(50))},+ MetricType.HEIGHT: {None: Limits(Decimal(50), Decimal(250), Decimal(140), Decimal(210))},+ MetricType.BLOOD_PRESSURE_SYSTOLIC: {+ None: Limits(Decimal(50), Decimal(250), Decimal(90), Decimal(180)),+ },+ MetricType.BLOOD_PRESSURE_DIASTOLIC: {+ None: Limits(Decimal(30), Decimal(150), Decimal(50), Decimal(110)),+ },+ MetricType.HEART_RATE: {None: Limits(Decimal(30), Decimal(250), Decimal(40), Decimal(200))},+ MetricType.RESTING_HEART_RATE: {+ None: Limits(Decimal(30), Decimal(120), Decimal(40), Decimal(100)),+ },+ # The cumulative types hold a whole day, and a rest day really is 0 steps+ MetricType.STEPS: {None: Limits(Decimal(0), Decimal(100000), Decimal(0), Decimal(30000))},+ MetricType.DISTANCE: {None: Limits(Decimal(0), Decimal(500), Decimal(0), Decimal(30))},+ MetricType.ENERGY: {None: Limits(Decimal(0), Decimal(10000), Decimal(0), Decimal(2000))},+ # Sleep is stored in minutes, so the upper bound is not a rarity but+ # arithmetic: a day has 1440 of them+ MetricType.SLEEP_TOTAL: {None: Limits(Decimal(0), Decimal(1440), Decimal(180), Decimal(720))},+ MetricType.SLEEP_LIGHT: {None: _SLEEP_STAGE},+ MetricType.SLEEP_DEEP: {None: _SLEEP_STAGE},+ MetricType.SLEEP_REM: {None: _SLEEP_STAGE},+ MetricType.SLEEP_AWAKE: {None: _SLEEP_STAGE},+}+"""+The bounds per metric type, in the unit the type is stored in.++A hard bound answers "beyond this it is almost certainly a typo", not "no human+could reach this": they start low deliberately, because the direction we may+have to correct them in later is wider, and widening is the safe one. Tightening+a bound after the release is not: a client that still knows the old, wider one+would write values the server then rejects permanently, which in the offline+sync path is silent data loss.++The clients mirror this table (flutter ``MetricType.limits``, react+``limitsFor``), so a change here needs a change there.+"""++DEFAULT_LIMITS = Limits(Decimal(0), SCHEMA_MAX_VALUE)+++def limits_for(metric_type: str, unit: str | None = None) -> Limits:+ """+ Returns the bounds a value in a category of this metric type must be in.++ Types without an entry fall back to the technical cap of the column: the+ free-form categories, which have no semantics to derive a bound from, and+ the group containers, which carry no measurements at all.+ """+ per_unit = METRIC_LIMITS.get(metric_type)+ if per_unit is None:+ return DEFAULT_LIMITS++ return per_unit.get(unit) or per_unit[None]diff --git a/wger/measurements/management/commands/dummy-generator-health-measurements.py b/wger/measurements/management/commands/dummy-generator-health-measurements.pyindex d73ca0773..e78cea57f 100644--- a/wger/measurements/management/commands/dummy-generator-health-measurements.py+++ b/wger/measurements/management/commands/dummy-generator-health-measurements.py@@ -25,10 +25,10 @@ from django.core.management.base import (BaseCommand,CommandError,)-from django.core.validators import MaxValueValidatorfrom django.utils import timezone# wger+from wger.measurements.limits import limits_forfrom wger.measurements.models import (Category,Measurement,@@ -46,18 +46,6 @@ logger = logging.getLogger(__name__)# (category, source, external_id) turns it into a no-op instead of duplicatingEXTERNAL_ID_NAMESPACE = uuid.UUID('4b3a3b8e-1b4e-4b2a-9c3a-6b1e7f2d5c40')-# Largest value a measurement currently accepts. Real daily step and energy-# totals are well above it, they are capped so that everything the generator-# writes stays valid for the API as well-MAX_VALUE = min(- (- Decimal(validator.limit_value)- for validator in Measurement._meta.get_field('value').validators- if isinstance(validator, MaxValueValidator)- ),- default=Decimal('9999.99'),-)-# Device the entries claim to come fromDEVICES = {MeasurementSource.APPLE.value: ('Apple Watch', 'com.apple.health'),@@ -189,8 +177,7 @@ class Command(BaseCommand):if self.clamped:self.stdout.write(self.style.WARNING(- f' {self.clamped} values were capped at {MAX_VALUE}: real daily step or '- f'energy totals are above what a measurement currently accepts'+ f' {self.clamped} values were clamped to the limits of their metric type'))@@ -361,10 +348,14 @@ class Command(BaseCommand):extra_data: dict,external_id: uuid.UUID,) -> Measurement:+ # Everything the generator writes has to stay valid for the API, whose+ # bounds depend on the metric type and, for body weight, on the unit+ # the entry is stamped with+ limits = limits_for(category.metric_type, extra_data.get('unit') or category.unit)decimal_value = Decimal(value).quantize(TWOPLACES)- if decimal_value > MAX_VALUE:+ if not limits.min <= decimal_value <= limits.max:self.clamped += 1- decimal_value = MAX_VALUE+ decimal_value = min(max(decimal_value, limits.min), limits.max)return Measurement(category=category,diff --git a/wger/measurements/migrations/0006_category_externally_synced_category_metric_type_and_more.py b/wger/measurements/migrations/0006_category_externally_synced_category_metric_type_and_more.pyindex 6e29b46b4..025fd443b 100644--- a/wger/measurements/migrations/0006_category_externally_synced_category_metric_type_and_more.py+++ b/wger/measurements/migrations/0006_category_externally_synced_category_metric_type_and_more.py@@ -1,5 +1,6 @@# Generated by Django 6.0.5 on 2026-07-01 14:53+import django.core.validatorsimport django.db.models.deletionfrom django.db import migrations, models@@ -102,4 +103,20 @@ class Migration(migrations.Migration):name='measurement_category_date',),),+ # Daily step and energy totals do not fit into the old numeric(6, 2),+ # and the flat 5000 cap was the binding limit no matter what a metric+ # type allows. The bounds per metric type live in+ # wger.measurements.limits and are checked in the serializer instead.+ # The column only gets wider, so no existing row can violate it; on+ # Postgres the precision change rewrites the table+ migrations.AlterField(+ model_name='measurement',+ name='value',+ field=models.DecimalField(+ decimal_places=2,+ max_digits=8,+ validators=[django.core.validators.MinValueValidator(0)],+ verbose_name='Value',+ ),+ ),]diff --git a/wger/measurements/models/measurement.py b/wger/measurements/models/measurement.pyindex 0963606bf..7662da93c 100644--- a/wger/measurements/models/measurement.py+++ b/wger/measurements/models/measurement.py@@ -18,14 +18,15 @@ from decimal import Decimal# Djangofrom django.contrib.auth.models import User-from django.core.validators import (- MaxValueValidator,- MinValueValidator,-)+from django.core.validators import MinValueValidatorfrom django.db import modelsfrom django.utils import timezone# wger+from wger.measurements.limits import (+ VALUE_DECIMAL_PLACES,+ VALUE_MAX_DIGITS,+)from wger.measurements.models import Categoryfrom wger.measurements.models.category import MetricTypefrom wger.utils.constants import TWOPLACES@@ -85,12 +86,12 @@ class Measurement(models.Model):value = models.DecimalField(verbose_name='Value',- max_digits=6,- decimal_places=2,- validators=[- MinValueValidator(0),- MaxValueValidator(5000),- ],+ max_digits=VALUE_MAX_DIGITS,+ decimal_places=VALUE_DECIMAL_PLACES,+ # The column only holds the technical cap, the range a value has to be+ # in depends on the metric type of its category and is checked in the+ # serializer (wger.measurements.limits)+ validators=[MinValueValidator(0)],)notes = models.CharField(diff --git a/wger/measurements/tests/test_measurements.py b/wger/measurements/tests/test_measurements.pyindex 122d8fe96..465050e6f 100644--- a/wger/measurements/tests/test_measurements.py+++ b/wger/measurements/tests/test_measurements.py@@ -17,6 +17,7 @@from decimal import Decimal# Django+from django.contrib.auth.models import Userfrom django.db import (IntegrityError,transaction,@@ -188,6 +189,102 @@ class MeasurementUnitTestCase(WgerTestCase):entry.value_in('kg')+class MeasurementValueLimitsTestCase(WgerTestCase):+ """+ The range a value may be in follows the metric type of its category+ """++ body_weight_id = 'cccccccc-cccc-cccc-cccc-0000000000b0' # user 'test', kg+ custom_id = 'cccccccc-cccc-cccc-cccc-000000000001' # user 'test', cm++ def setUp(self):+ super().setUp()+ self.user_login('test')+ self.url = reverse('measurement-list')++ def add_entry(self, category_id, value, extra_data=None):+ return self.client.post(+ self.url,+ {+ 'category': str(category_id),+ 'date': '2023-05-01T12:00:00Z',+ 'value': value,+ 'extra_data': extra_data or {},+ },+ content_type='application/json',+ )++ def create_category(self, metric_type, unit):+ return Category.objects.create(+ user=User.objects.get(username='test'),+ name=metric_type,+ unit=unit,+ metric_type=metric_type,+ )++ def test_daily_step_count(self):+ """+ Test that a daily step total is accepted+ """+ category = self.create_category(MetricType.STEPS, 'count')++ self.assertEqual(self.add_entry(category.pk, 11000).status_code, 201)++ def test_value_above_limit(self):+ """+ Test that a value above the limit of its metric type is rejected+ """+ category = self.create_category(MetricType.STEPS, 'count')++ response = self.add_entry(category.pk, 150000)++ self.assertEqual(response.status_code, 400)+ self.assertIn('value', response.data)++ def test_value_below_limit(self):+ """+ Test that a value below the limit of its metric type is rejected+ """+ category = self.create_category(MetricType.HEART_RATE, 'bpm')++ response = self.add_entry(category.pk, 5)++ self.assertEqual(response.status_code, 400)+ self.assertIn('value', response.data)++ def test_body_weight_limits_per_unit(self):+ """+ Test that the body weight bounds are resolved in the entry's own unit+ """+ self.assertEqual(self.add_entry(self.body_weight_id, 700, {'unit': 'lb'}).status_code, 201)++ response = self.add_entry(self.body_weight_id, 700, {'unit': 'kg'})+ self.assertEqual(response.status_code, 400)+ self.assertIn('value', response.data)++ def test_custom_category_limited_by_column(self):+ """+ Test that a free-form category is only bounded by the column itself+ """+ self.assertEqual(self.add_entry(self.custom_id, 50000).status_code, 201)+ self.assertEqual(self.add_entry(self.custom_id, 1000000).status_code, 400)++ def test_limits_on_update(self):+ """+ Test that updating a value validates it as well+ """+ entry = Measurement.objects.filter(category_id=self.body_weight_id).first()++ response = self.client.patch(+ reverse('measurement-detail', kwargs={'pk': entry.pk}),+ {'value': 700},+ content_type='application/json',+ )++ self.assertEqual(response.status_code, 400)+ self.assertIn('value', response.data)++class MeasurementExtraDataApiTestCase(WgerTestCase):"""extra_data over the measurement APIdiff --git a/wger/weight/api/serializers.py b/wger/weight/api/serializers.pyindex f00299a36..93a430c99 100644--- a/wger/weight/api/serializers.py+++ b/wger/weight/api/serializers.py@@ -15,14 +15,17 @@# You should have received a copy of the GNU Affero General Public License# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.-# Standard Library-from decimal import Decimal-# Third Partyfrom rest_framework import serializers# wger+from wger.measurements.limits import (+ VALUE_DECIMAL_PLACES,+ VALUE_MAX_DIGITS,+ limits_for,+)from wger.measurements.models import Measurement+from wger.measurements.models.category import MetricTypeclass WeightEntrySerializer(serializers.ModelSerializer):@@ -33,10 +36,8 @@ class WeightEntrySerializer(serializers.ModelSerializer):user = serializers.PrimaryKeyRelatedField(source='category.user', read_only=True)weight = serializers.DecimalField(source='value',- max_digits=5,- decimal_places=2,- min_value=Decimal(30),- max_value=Decimal(600),+ max_digits=VALUE_MAX_DIGITS,+ decimal_places=VALUE_DECIMAL_PLACES,)class Meta:@@ -48,6 +49,24 @@ class WeightEntrySerializer(serializers.ModelSerializer):'user',)+ def validate_weight(self, value):+ """+ The same bounds a body weight measurement has, in the profile unit.++ Values are read and written in the unit of the user profile, so that is+ the unit the bounds are resolved in: 350 kg and 770 lb are the same+ limit, a single span covering both would allow 550 kg as readily as+ 550 lb (wger-project/wger#1019).+ """+ unit = self.context['request'].user.userprofile.weight_unit+ limits = limits_for(MetricType.BODY_WEIGHT, unit)++ if not limits.min <= value <= limits.max:+ raise serializers.ValidationError(+ f'Weight must be between {limits.min} and {limits.max} {unit}'+ )+ return value+def to_representation(self, instance):"""Weight values are returned in the user's preferred weight unit,diff --git a/wger/weight/helpers.py b/wger/weight/helpers.pyindex 14ed178ba..372ea15fa 100644--- a/wger/weight/helpers.py+++ b/wger/weight/helpers.py@@ -25,6 +25,7 @@ import loggingfrom django.utils.timezone import make_aware# wger+from wger.measurements.limits import limits_forfrom wger.measurements.models import (Category,Measurement,@@ -50,6 +51,11 @@ def parse_weight_csv(request, cleaned_data):MAX_ROW_COUNT = 1000row_count = 0+ # The values are read in the unit of the profile, so that is the unit the+ # bounds are resolved in. This is the one path that writes body weight+ # without going through a serializer+ limits = limits_for(MetricType.BODY_WEIGHT, request.user.userprofile.weight_unit)+# Process the CSV items firstfor row in parsed_csv:try:@@ -66,7 +72,9 @@ def parse_weight_csv(request, cleaned_data):# there is no existing weight entry in the database for that dateunique_in_db = not duplicate_date_in_db- if unique_among_csv and unique_in_db and parsed_weight:+ plausible_weight = limits.min <= parsed_weight <= limits.max++ if unique_among_csv and unique_in_db and plausible_weight:distinct_weight_entries.append((parsed_date, parsed_weight))entry_dates.add(parsed_date)else:diff --git a/wger/weight/tests/test_csv_import.py b/wger/weight/tests/test_csv_import.pyindex 1f9929400..cc3ebd611 100644--- a/wger/weight/tests/test_csv_import.py+++ b/wger/weight/tests/test_csv_import.py@@ -50,6 +50,7 @@ class WeightCsvImportTestCase(WgerTestCase):19.02.10 00:00:00 71 22226.02.10 00:00:00 71,9 22226.02.10 00:00:00 71,9 222+07.03.10 00:00:00 7000 22219.03.10 00:00:00 72 222"""response = self.client.post(reverse('weight:import-csv'),@@ -58,7 +59,8 @@ class WeightCsvImportTestCase(WgerTestCase):self.assertEqual(response.status_code, 200)self.assertEqual(len(response.context['weight_list']), 6)- self.assertEqual(len(response.context['error_list']), 4)+ # the unparseable rows, the duplicate date, and the 7000 kg entry+ self.assertEqual(len(response.context['error_list']), 5)hash_value = response.context['hash_value']# 2nd. stepdiff --git a/wger/weight/tests/test_entry.py b/wger/weight/tests/test_entry.pyindex 1a66a27ef..a4294bcae 100644--- a/wger/weight/tests/test_entry.py+++ b/wger/weight/tests/test_entry.py@@ -157,3 +157,39 @@ class WeightEntryUnitTestCase(api_base_test.ApiBaseTestCase, WgerTestCase):self.assertEqual(response.status_code, 200)entry.refresh_from_db()self.assertEqual(entry.extra_data, {'unit': 'lb'})+++class WeightEntryLimitsTestCase(api_base_test.ApiBaseTestCase, WgerTestCase):+ """+ The bounds of the legacy weight endpoint are the body weight bounds,+ resolved in the unit of the user profile+ """++ url = '/api/v2/weightentry/'++ def set_unit(self, unit):+ user = User.objects.get(username='test')+ user.userprofile.weight_unit = unit+ user.userprofile.save()++ def add_entry(self, weight):+ self.authenticate('test')+ return self.client.post(self.url, {'weight': weight, 'date': timezone.now()})++ def test_metric_bounds(self):+ """+ Test that a weight in kg is bounded by the kg limits+ """+ self.set_unit('kg')++ self.assertEqual(self.add_entry(340).status_code, 201)+ self.assertEqual(self.add_entry(360).status_code, 400)++ def test_imperial_bounds(self):+ """+ Test that the same weight in lb is bounded by the lb limits+ """+ self.set_unit('lb')++ self.assertEqual(self.add_entry(360).status_code, 201)+ self.assertEqual(self.add_entry(800).status_code, 400)