Self hosted FLOSS fitness/workout, nutrition and weight tracker
Re-check a measurement's value when an update moves its bounds
Roland Geider7fd30c703d44
diff --git a/wger/measurements/api/serializers.py b/wger/measurements/api/serializers.pyindex 011c35ff1..1ed55f83b 100644--- a/wger/measurements/api/serializers.py+++ b/wger/measurements/api/serializers.py@@ -321,19 +321,35 @@ class MeasurementSerializer(serializers.ModelSerializer):kilograms and daily step counts. Since a category can hold mixed units,the entry's own unit decides which of them applies.- Only a value the payload carries is checked: entries predating the- limits exist, and re-checking a stored one would block every edit to it.+ A stored value is only re-checked when the payload moves the entry under+ a different bound, by changing its category or its unit: entries+ predating the limits exist, and re-checking a stored value on every+ update would block all other edits to them."""- value = data.get('value')- if value is None or category is None:+ if category is None:returnlimits = limits_for(category.metric_type, unit or category.unit)++ value = data.get('value')+ if value is None:+ if self.instance is None or limits == self._stored_limits():+ return+ value = self.instance.value+if not limits.min <= value <= limits.max:raise serializers.ValidationError({'value': f'Value must be between {limits.min} and {limits.max}'})+ def _stored_limits(self):+ """+ The bounds that applied to the entry before the update+ """+ category = self.instance.category+ unit = (self.instance.extra_data or {}).get('unit')+ return limits_for(category.metric_type, unit or category.unit)+class BucketSerializer(serializers.Serializer):"""diff --git a/wger/measurements/tests/test_measurements.py b/wger/measurements/tests/test_measurements.pyindex 8f7d7826e..bba97306f 100644--- a/wger/measurements/tests/test_measurements.py+++ b/wger/measurements/tests/test_measurements.py@@ -322,6 +322,54 @@ class MeasurementValueLimitsTestCase(WgerTestCase):200,)+ def test_limits_when_moving_to_another_category(self):+ """+ Test that a value is re-checked when the entry moves to a stricter category+ """+ steps = self.create_category(MetricType.STEPS, 'count')+ oxygen = self.create_category(MetricType.BLOOD_OXYGEN, '%')+ entry_id = self.add_entry(steps.pk, 300).data['id']++ response = self.client.patch(+ reverse('measurement-detail', kwargs={'pk': entry_id}),+ {'category': str(oxygen.pk)},+ content_type='application/json',+ )++ self.assertEqual(response.status_code, 400)+ self.assertIn('value', response.data)++ def test_limits_when_restamping_the_unit(self):+ """+ Test that a value is re-checked when the entry is restamped to another unit+ """+ entry_id = self.add_entry(self.body_weight_id, 700, {'unit': 'lb'}).data['id']++ response = self.client.patch(+ reverse('measurement-detail', kwargs={'pk': entry_id}),+ {'extra_data': {'unit': 'kg'}},+ content_type='application/json',+ )++ self.assertEqual(response.status_code, 400)+ self.assertIn('value', response.data)++ def test_editing_the_extra_data_of_an_entry_outside_the_limits(self):+ """+ Test that a stored value outside the limits does not block an edit that+ leaves its bounds unchanged+ """+ entry = Measurement.objects.filter(category_id=self.body_weight_id).first()+ Measurement.objects.filter(pk=entry.pk).update(value=550)++ response = self.client.patch(+ reverse('measurement-detail', kwargs={'pk': entry.pk}),+ {'extra_data': {'unit': 'kg', 'source_name': 'Scale'}},+ content_type='application/json',+ )++ self.assertEqual(response.status_code, 200)+def test_editing_an_entry_whose_stored_unit_is_unknown(self):"""Test that a stored unit outside kg and lb does not block other edits