Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Validate the value range only when a value is written

Roland Geider68447173ac12
diff --git a/wger/measurements/api/serializers.py b/wger/measurements/api/serializers.py
index 2a0a9f78f..488e97047 100644
--- a/wger/measurements/api/serializers.py
+++ b/wger/measurements/api/serializers.py
@@ -256,7 +256,7 @@ class MeasurementSerializer(serializers.ModelSerializer):
extra_data = data.get('extra_data', self.instance.extra_data if self.instance else {})
unit = extra_data.get('unit')
- if unit is not None and category is not None:
+ if 'extra_data' in data and unit is not None and category is not None:
if category.metric_type == MetricType.BODY_WEIGHT and unit not in ('kg', 'lb'):
raise serializers.ValidationError(
{'extra_data': 'Body weight entries only support kg and lb as unit'}
@@ -273,8 +273,14 @@ class MeasurementSerializer(serializers.ModelSerializer):
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.
+
+ Only a value the payload actually carries is checked. Entries predating
+ the limits exist (the weight backfill copies them as they are), and
+ re-checking a stored value would lock every one of them: a PATCH of the
+ notes alone would come back as a 400, and over PowerSync that is a
+ write the client acknowledges and drops.
"""
- value = data.get('value', self.instance.value if self.instance else None)
+ value = data.get('value')
if value is None or category is None:
return
diff --git a/wger/measurements/tests/test_measurements.py b/wger/measurements/tests/test_measurements.py
index 465050e6f..2b874afe9 100644
--- a/wger/measurements/tests/test_measurements.py
+++ b/wger/measurements/tests/test_measurements.py
@@ -284,6 +284,56 @@ class MeasurementValueLimitsTestCase(WgerTestCase):
self.assertEqual(response.status_code, 400)
self.assertIn('value', response.data)
+ def test_editing_an_entry_outside_the_limits(self):
+ """
+ Test that a stored value outside the limits does not block other edits
+ """
+ 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}),
+ {'notes': 'Wrong unit'},
+ content_type='application/json',
+ )
+
+ self.assertEqual(response.status_code, 200)
+ entry.refresh_from_db()
+ self.assertEqual(entry.notes, 'Wrong unit')
+ self.assertEqual(entry.value, Decimal(550))
+
+ def test_correcting_an_entry_outside_the_limits(self):
+ """
+ Test that such an entry can be corrected, but only to a valid value
+ """
+ entry = Measurement.objects.filter(category_id=self.body_weight_id).first()
+ Measurement.objects.filter(pk=entry.pk).update(value=550)
+ url = reverse('measurement-detail', kwargs={'pk': entry.pk})
+
+ self.assertEqual(
+ self.client.patch(url, {'value': 600}, content_type='application/json').status_code,
+ 400,
+ )
+ self.assertEqual(
+ self.client.patch(url, {'value': 80}, content_type='application/json').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
+ """
+ entry = Measurement.objects.filter(category_id=self.body_weight_id).first()
+ Measurement.objects.filter(pk=entry.pk).update(extra_data={'unit': 'st'})
+
+ response = self.client.patch(
+ reverse('measurement-detail', kwargs={'pk': entry.pk}),
+ {'notes': 'Stones'},
+ content_type='application/json',
+ )
+
+ self.assertEqual(response.status_code, 200)
+
class MeasurementExtraDataApiTestCase(WgerTestCase):
"""