Self hosted FLOSS fitness/workout, nutrition and weight tracker
Keep the import provenance when the shim re-stamps the unit
Roland Geider1c4e3d69e208
diff --git a/wger/measurements/api/serializers.py b/wger/measurements/api/serializers.pyindex 488e97047..e14d1a8a1 100644--- a/wger/measurements/api/serializers.py+++ b/wger/measurements/api/serializers.py@@ -274,11 +274,8 @@ 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 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.+ 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."""value = data.get('value')if value is None or category is None:diff --git a/wger/nutrition/forms.py b/wger/nutrition/forms.pyindex 07f533adc..9235e5f94 100644--- a/wger/nutrition/forms.py+++ b/wger/nutrition/forms.py@@ -129,10 +129,8 @@ class BmrForm(forms.ModelForm):# height is optional on the profile, but the BMR calculation needs itself.fields['height'].required = True- # The calculator writes its weight back as a measurement, so it is- # bounded like one. The form is prefilled with the profile weight,- # which is 0 for a user who has none yet, and submitting that unchanged- # is where the stored zeroes come from+ # The weight is written back as a measurement, so it is bounded like+ # one. Unbounded, the prefilled 0 of a user without entries was storedlimits = limits_for(MetricType.BODY_WEIGHT, self.instance.weight_unit)self.fields['weight'].min_value = limits.minself.fields['weight'].max_value = limits.maxdiff --git a/wger/weight/api/views.py b/wger/weight/api/views.pyindex daf78a55c..cde60b612 100644--- a/wger/weight/api/views.py+++ b/wger/weight/api/views.py@@ -68,10 +68,13 @@ class WeightEntryViewSet(viewsets.ModelViewSet):def perform_update(self, serializer):"""A new value is interpreted in the user's current weight unit, updates- without a value keep the stored unit+ without a value keep the stored unit.++ Only the unit key is replaced, the rest of extra_data is the provenance+ an import left there."""if 'value' in serializer.validated_data:unit = self.request.user.userprofile.weight_unit- serializer.save(extra_data={'unit': unit})+ serializer.save(extra_data={**serializer.instance.extra_data, 'unit': unit})else:serializer.save()diff --git a/wger/weight/tests/test_entry.py b/wger/weight/tests/test_entry.pyindex a4294bcae..efd21d7ff 100644--- a/wger/weight/tests/test_entry.py+++ b/wger/weight/tests/test_entry.py@@ -143,6 +143,34 @@ class WeightEntryUnitTestCase(api_base_test.ApiBaseTestCase, WgerTestCase):self.assertEqual(entry.value, Decimal('170.00'))self.assertEqual(entry.extra_data, {'unit': 'lb'})+ def test_patch_weight_keeps_the_import_provenance(self):+ """+ Test that re-stamping the unit leaves the rest of extra_data alone+ """+ entry = Measurement.objects.get(pk=self.entry_pk)+ entry.extra_data = {+ 'unit': 'kg',+ 'source_unit': 'lb',+ 'source_value': 169.76,+ 'recording_method': 'automatic',+ }+ entry.save()++ self.authenticate('test')+ response = self.client.patch(f'{self.url}{self.entry_pk}/', {'weight': 78})++ self.assertEqual(response.status_code, 200)+ entry.refresh_from_db()+ self.assertEqual(+ entry.extra_data,+ {+ 'unit': 'kg',+ 'source_unit': 'lb',+ 'source_value': 169.76,+ 'recording_method': 'automatic',+ },+ )+def test_patch_date_keeps_unit(self):"""Test that updates without a weight keep the stored unit