Self hosted FLOSS fitness/workout, nutrition and weight tracker
Harden the dynamic measurement types
Roland Geider360a2e19a983
diff --git a/wger/measurements/api/views.py b/wger/measurements/api/views.pyindex db9bb5645..ca2520ec1 100644--- a/wger/measurements/api/views.py+++ b/wger/measurements/api/views.py@@ -32,6 +32,7 @@ from rest_framework.permissions import IsAuthenticatedfrom rest_framework.response import Response# wger+from wger.measurements import dynamicfrom wger.measurements.api.aggregates import (DEFAULT_MAX_POINTS,InvalidBucket,@@ -40,7 +41,6 @@ from wger.measurements.api.aggregates import (value_count_rows,)from wger.measurements.api.filtersets import MeasurementEntryFilterSet-from wger.measurements import dynamicfrom wger.measurements.api.serializers import (BucketSerializer,CategorySerializer,diff --git a/wger/measurements/dynamic/engine.py b/wger/measurements/dynamic/engine.pyindex 381d64856..0e6bd4dd3 100644--- a/wger/measurements/dynamic/engine.py+++ b/wger/measurements/dynamic/engine.py@@ -118,10 +118,16 @@ def _run_scheduled(category_id) -> None:returndone.add(category_id)- if settings.WGER_SETTINGS['USE_CELERY']:- # wger- from wger.measurements.tasks import reconcile_dynamic_category_task-- reconcile_dynamic_category_task.delay(str(category_id))- else:- reconcile_by_id(category_id)+ # This runs after the commit, so a failure here has nothing to roll back+ # and would only turn an unrelated request into a 500. The daily task+ # picks the category up again+ try:+ if settings.WGER_SETTINGS['USE_CELERY']:+ # wger+ from wger.measurements.tasks import reconcile_dynamic_category_task++ reconcile_dynamic_category_task.delay(str(category_id))+ else:+ reconcile_by_id(category_id)+ except Exception:+ logger.exception(f'Could not reconcile dynamic category {category_id}')diff --git a/wger/measurements/dynamic/types.py b/wger/measurements/dynamic/types.pyindex af2deeb9e..870d9fcd9 100644--- a/wger/measurements/dynamic/types.py+++ b/wger/measurements/dynamic/types.py@@ -43,9 +43,11 @@ from wger.measurements.models import ()from wger.measurements.models.category import MetricTypefrom wger.measurements.models.measurement import MeasurementSource-from wger.measurements.utils.bmi import calculate_bmifrom wger.utils.constants import TWOPLACES-from wger.utils.units import AbstractWeight+from wger.utils.units import (+ AbstractHeight,+ AbstractWeight,+)@register@@ -69,13 +71,26 @@ class Bmi(DynamicMeasurementType):]def compute(self, category: Category) -> list[DesiredRow]:+ profile = category.user.userprofile+ if not profile.height or profile.height <= 0:+ return []++ # The profile height is centimeters, the formula wants meters+ height_sq = (Decimal(profile.height) / 100) ** 2++ # Calculated entries are never an input, otherwise a body weight+ # category calculating itself would grow with every run+ entries = Measurement.body_weight_for(category.user).exclude(+ source=MeasurementSource.CALCULATED+ )+return [DesiredRow(- external_id=row['source_id'],- date=row['date'],- value=row['value'],+ external_id=entry.pk,+ date=entry.date,+ value=round(entry.value_in('kg') / height_sq, 2),)- for row in calculate_bmi(category.user)+ for entry in entries]@@ -105,6 +120,21 @@ class WaistToHeightRatio(DynamicMeasurementType):raise ValueError('The source category does not exist')if source.dynamic_type != Category.DynamicType.NONE:raise ValueError('A calculated category cannot be the source of another one')+ if self._cm_factor(source.unit) is None:+ raise ValueError('The source category has to be measured in cm or inches')++ @staticmethod+ def _cm_factor(unit: str) -> Decimal | None:+ """+ What a value of this unit has to be multiplied with to become+ centimeters, or None if it is not a length this type understands+ """+ normalized = (unit or '').strip().lower()+ if normalized == 'cm':+ return Decimal(1)+ if normalized in ('in', 'inch', 'inches'):+ return AbstractHeight.INCHES_IN_CM+ return None@staticmethoddef _source_category(user_id, params) -> Category | None:@@ -130,14 +160,22 @@ class WaistToHeightRatio(DynamicMeasurementType):entries = Measurement.objects.filter(category=source).exclude(source=MeasurementSource.CALCULATED)- return [- DesiredRow(- external_id=entry.pk,- date=entry.date,- value=round(entry.value / height, 2),++ rows = []+ for entry in entries:+ # An entry can carry a unit of its own, and a value whose unit+ # this type cannot read would produce a plausible wrong ratio+ factor = self._cm_factor(entry.unit)+ if factor is None:+ continue+ rows.append(+ DesiredRow(+ external_id=entry.pk,+ date=entry.date,+ value=round(entry.value * factor / height, 2),+ ))- for entry in entries- ]+ return rowsDEFAULT_MAX_REPS = 5diff --git a/wger/measurements/signals.py b/wger/measurements/signals.pyindex a7a011afd..536c855f5 100644--- a/wger/measurements/signals.py+++ b/wger/measurements/signals.py@@ -17,7 +17,6 @@import logging# Django-from django.contrib.auth.models import Userfrom django.core.exceptions import ObjectDoesNotExistfrom django.db.models.signals import (post_delete,@@ -39,20 +38,15 @@ from wger.measurements.models import (Measurement,)from wger.measurements.models.measurement import MeasurementSource-from wger.utils.helpers import disable_for_loaddata+from wger.utils.helpers import (+ deletion_originates_from_user,+ disable_for_loaddata,+)logger = logging.getLogger(__name__)-def _deletion_originates_from_user(origin) -> bool:- """- True if this delete is part of removing a User account: everything the- reconcile would look at is going away in the same cascade- """- return isinstance(origin, User) or getattr(origin, 'model', None) is User--def _dispatch_source_change(sender, instance):"""Schedules a reconcile for every dynamic category that depends on the@@ -90,7 +84,7 @@ def _source_saved(sender, instance, **kwargs):def _source_deleted(sender, instance, origin=None, **kwargs):- if _deletion_originates_from_user(origin):+ if deletion_originates_from_user(origin):return_dispatch_source_change(sender, instance)diff --git a/wger/measurements/tasks.py b/wger/measurements/tasks.pyindex 139ff8f79..e1c682156 100644--- a/wger/measurements/tasks.py+++ b/wger/measurements/tasks.py@@ -48,8 +48,13 @@ def reconcile_all_dynamic_categories_task():"""count = 0for category in Category.objects.exclude(dynamic_type=Category.DynamicType.NONE).iterator():- reconcile(category)- count += 1+ # One category the computation chokes on must not take the safety net+ # down with it, the others would stay stale for good+ try:+ reconcile(category)+ count += 1+ except Exception:+ logger.exception(f'Could not reconcile dynamic category {category.pk}')logger.info(f'Reconciled {count} dynamic measurement categories')diff --git a/wger/measurements/tests/test_dynamic.py b/wger/measurements/tests/test_dynamic.pyindex 99df8516c..4a4257008 100644--- a/wger/measurements/tests/test_dynamic.py+++ b/wger/measurements/tests/test_dynamic.py@@ -16,6 +16,7 @@# Standard Libraryimport datetimefrom decimal import Decimal+from unittest.mock import patch# Djangofrom django.contrib.auth.models import User@@ -36,6 +37,7 @@ from wger.measurements.models import ()from wger.measurements.models.measurement import MeasurementSourcefrom wger.measurements.powersync import MeasurementHandler+from wger.measurements.tasks import reconcile_all_dynamic_categories_task# Pinned in test-measurement-categories.json, the official body weight@@ -358,6 +360,63 @@ class ExistingEntriesTestCase(DynamicMeasurementTestCase):self.assertFalse(Measurement.objects.filter(pk=self.entry.pk).exists())+class ReconcileFailureTestCase(DynamicMeasurementTestCase):+ """+ A computation that raises stays contained: neither the safety net nor an+ unrelated request goes down with it+ """++ def test_catch_all_continues_after_a_failure(self):+ """+ The daily task reconciles the remaining categories+ """+ first = self.enable_bmi()+ second = Category.objects.create(+ user=self.user,+ name='Second',+ unit='',+ dynamic_type=Category.DynamicType.BMI,+ )+ order = list(+ Category.objects.exclude(dynamic_type=Category.DynamicType.NONE).values_list(+ 'pk', flat=True+ )+ )+ self.assertEqual(set(order), {first.pk, second.pk})++ seen = []++ def reconcile(category):+ seen.append(category.pk)+ if category.pk == order[0]:+ raise ValueError('boom')++ with patch('wger.measurements.tasks.reconcile', side_effect=reconcile):+ reconcile_all_dynamic_categories_task()++ self.assertEqual(seen, order)++ def test_scheduled_reconcile_does_not_reach_the_request(self):+ """+ The callback runs after the commit, a failure there must not turn an+ unrelated write into a 500+ """+ category = self.enable_bmi()++ with patch(+ 'wger.measurements.dynamic.engine.reconcile_by_id',+ side_effect=ValueError('boom'),+ ):+ with self.captureOnCommitCallbacks(execute=True):+ Measurement.objects.create(+ category=self.weight_category,+ date=timezone.now(),+ value=Decimal('81.00'),+ )++ self.assertTrue(Category.objects.filter(pk=category.pk).exists())++class ApiWriteBlockTestCase(DynamicMeasurementTestCase):"""The entries of a calculated category cannot be written through the API@@ -638,6 +697,41 @@ class WhtrTestCase(DynamicMeasurementTestCase):)self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ def test_source_in_inches_is_converted(self):+ """+ A source measured in inches is read as centimeters+ """+ self.waist.unit = 'in'+ self.waist.save()+ self.create_waist('34.00')++ category = self.enable_whtr()+ rows = self.calculated_rows(category)++ # 34 in = 86.36 cm, / 180 = 0.48 (and not the raw 34 / 180 = 0.19)+ self.assertEqual(rows[0].value, Decimal('0.48'))++ def test_api_refuses_a_source_that_is_not_a_length(self):+ """+ A category holding kilograms cannot be the source of a ratio+ """+ weights = Category.objects.create(user=self.user, name='Dumbbells', unit='kg')++ self.user_login('test')+ response = self.client.post(+ reverse('measurement-category-list'),+ {+ 'name': 'WHtR',+ 'unit': '',+ 'dynamic_type': 'WHTR',+ 'dynamic_params': {'category_id': str(weights.pk)},+ },+ content_type='application/json',+ )++ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertIn('dynamic_params', response.data)+def test_unrelated_patch_survives_a_deleted_source(self):"""Renaming the category still works once its source is gone; only adiff --git a/wger/measurements/utils/__init__.py b/wger/measurements/utils/__init__.pydeleted file mode 100644index e69de29bb..000000000diff --git a/wger/measurements/utils/bmi.py b/wger/measurements/utils/bmi.pydeleted file mode 100644index 6650156c0..000000000--- a/wger/measurements/utils/bmi.py+++ /dev/null@@ -1,48 +0,0 @@-# -*- coding: utf-8 -*---# 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---def calculate_bmi(user) -> list[dict]:- """- Returns one row per body weight entry: its date, the BMI it works out to- and the id of the entry it derives from- """- # wger- from wger.measurements.models import Measurement- from wger.measurements.models.measurement import MeasurementSource-- profile = user.userprofile- if not profile.height or profile.height <= 0:- return []-- height_sq = (Decimal(profile.height) / 100) ** 2-- # Calculated entries are never an input, otherwise a body weight category- # that calculates itself would grow with every run- entries = Measurement.body_weight_for(user).exclude(source=MeasurementSource.CALCULATED)-- return [- {- 'source_id': entry.id,- 'date': entry.date,- 'value': round(entry.value_in('kg') / height_sq, 2),- }- for entry in entries.order_by('date')- ]diff --git a/wger/trophies/signals.py b/wger/trophies/signals.pyindex 997c05e09..6bb0d2320 100644--- a/wger/trophies/signals.py+++ b/wger/trophies/signals.py@@ -44,23 +44,15 @@ from wger.trophies.models.user_trophy import UserTrophyfrom wger.trophies.services import UserStatisticsServicefrom wger.trophies.services.trophy import TrophyServicefrom wger.trophies.tasks import evaluate_user_trophies_task-from wger.utils.helpers import disable_for_loaddata+from wger.utils.helpers import (+ deletion_originates_from_user,+ disable_for_loaddata,+)logger = logging.getLogger(__name__)-def _deletion_originates_from_user(origin) -> bool:- """- True if this delete is part of removing a User account.-- During a user deletion the statistics row is cascade-deleted too;- recreating it from a workout-deletion signal would leave an orphan row- and break the transaction's foreign key check at COMMIT.- """- return isinstance(origin, User) or getattr(origin, 'model', None) is User--def _trigger_trophy_evaluation(user_id: int):"""Trigger async trophy evaluation for a user.@@ -137,7 +129,7 @@ def workout_log_deleted(sender, instance: WorkoutLog, origin=None, **kwargs):if not instance.user_id:return- if _deletion_originates_from_user(origin):+ if deletion_originates_from_user(origin):returntry:@@ -196,7 +188,7 @@ def workout_session_deleted(sender, instance: WorkoutSession, origin=None, **kwaif not instance.user_id:return- if _deletion_originates_from_user(origin):+ if deletion_originates_from_user(origin):returntry:diff --git a/wger/utils/helpers.py b/wger/utils/helpers.pyindex 2dbe7530a..34ef0a482 100644--- a/wger/utils/helpers.py+++ b/wger/utils/helpers.py@@ -73,6 +73,20 @@ def disable_for_loaddata(signal_handler):return wrapper+def deletion_originates_from_user(origin) -> bool:+ """+ True if a delete is part of removing a User account.++ Signal handlers that write derived rows have to sit this one out:+ everything they would look at or recreate is going away in the same+ cascade, and a new row would break the foreign key check at COMMIT.+ """+ # Django+ from django.contrib.auth.models import User++ return isinstance(origin, User) or getattr(origin, 'model', None) is User++def make_uid(input):"""Small wrapper to generate a UID, usually used in URLs to allow for