Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Use pagination, add params for dynamic calculations, squash migrations

Dylan Smithbdff4715c2ef
diff --git a/wger/measurements/api/serializers.py b/wger/measurements/api/serializers.py
index 3dd10b56f..92d362958 100644
--- a/wger/measurements/api/serializers.py
+++ b/wger/measurements/api/serializers.py
@@ -33,7 +33,7 @@ class UnitSerializer(serializers.ModelSerializer):
class Meta:
model = Category
- fields = ('id', 'name', 'unit', 'dynamic_type')
+ fields = ('id', 'name', 'unit', 'dynamic_type', 'dynamic_params')
class MeasurementSerializer(serializers.ModelSerializer):
diff --git a/wger/measurements/api/views.py b/wger/measurements/api/views.py
index 4ab4d4f19..09d4dba97 100644
--- a/wger/measurements/api/views.py
+++ b/wger/measurements/api/views.py
@@ -139,11 +139,16 @@ class MeasurementViewSet(WgerOwnerObjectModelViewSet):
calc_func = DYNAMIC_REGISTRY.get(category.dynamic_type)
if calc_func:
- # execute the math function and wrap it in the DRF pagination envelope
- data = calc_func(request.user, category_id)
- return Response(
- {'count': len(data), 'next': None, 'previous': None, 'results': data}
- )
+ # get the raw list of calculated dictionaries
+ raw_data = calc_func(request.user, category_id)
+
+ # paginate the list
+ page = self.paginate_queryset(raw_data)
+ if page is not None:
+ return self.get_paginated_response(page)
+
+ # fallback
+ return Response(raw_data)
except (Category.DoesNotExist, ValueError):
# fallback to standard behavior
pass
diff --git a/wger/measurements/migrations/0004_category_dynamic_params_category_dynamic_type.py b/wger/measurements/migrations/0004_category_dynamic_params_category_dynamic_type.py
new file mode 100644
index 000000000..92ecc9679
--- /dev/null
+++ b/wger/measurements/migrations/0004_category_dynamic_params_category_dynamic_type.py
@@ -0,0 +1,31 @@
+# Generated by Django 5.2.14 on 2026-05-15 08:13
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ('measurements', '0003_alter_measurement_unique_together_and_more'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='category',
+ name='dynamic_params',
+ field=models.JSONField(
+ blank=True,
+ default=dict,
+ help_text='Configuration parameters for dynamic calculations',
+ ),
+ ),
+ migrations.AddField(
+ model_name='category',
+ name='dynamic_type',
+ field=models.CharField(
+ choices=[('NONE', 'None'), ('BMI', 'Bmi')],
+ db_index=True,
+ default='NONE',
+ max_length=20,
+ ),
+ ),
+ ]
diff --git a/wger/measurements/migrations/0004_category_is_dynamic.py b/wger/measurements/migrations/0004_category_is_dynamic.py
deleted file mode 100644
index 16f7f3ab8..000000000
--- a/wger/measurements/migrations/0004_category_is_dynamic.py
+++ /dev/null
@@ -1,21 +0,0 @@
-# Generated by Django 5.2.14 on 2026-05-11 06:22
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
- dependencies = [
- ('measurements', '0003_alter_measurement_unique_together_and_more'),
- ]
-
- operations = [
- migrations.AddField(
- model_name='category',
- name='is_dynamic',
- field=models.BooleanField(
- default=False,
- help_text='Indicates if this category is automatically calculated.',
- verbose_name='Is Dynamic',
- ),
- ),
- ]
diff --git a/wger/measurements/migrations/0005_remove_category_is_dynamic_category_dynamic_type.py b/wger/measurements/migrations/0005_remove_category_is_dynamic_category_dynamic_type.py
deleted file mode 100644
index 3a129e59e..000000000
--- a/wger/measurements/migrations/0005_remove_category_is_dynamic_category_dynamic_type.py
+++ /dev/null
@@ -1,26 +0,0 @@
-# Generated by Django 5.2.14 on 2026-05-12 08:37
-
-from django.db import migrations, models
-
-
-class Migration(migrations.Migration):
- dependencies = [
- ('measurements', '0004_category_is_dynamic'),
- ]
-
- operations = [
- migrations.RemoveField(
- model_name='category',
- name='is_dynamic',
- ),
- migrations.AddField(
- model_name='category',
- name='dynamic_type',
- field=models.CharField(
- choices=[('NONE', 'None'), ('BMI', 'Bmi'), ('SQUAT_1RM', 'Squat 1Rm')],
- db_index=True,
- default='NONE',
- max_length=20,
- ),
- ),
- ]
diff --git a/wger/measurements/models/category.py b/wger/measurements/models/category.py
index bf57f595f..5bb8f5b8f 100644
--- a/wger/measurements/models/category.py
+++ b/wger/measurements/models/category.py
@@ -50,6 +50,10 @@ class Category(models.Model):
max_length=20, choices=DynamicType.choices, default=DynamicType.NONE, db_index=True
)
+ dynamic_params = models.JSONField(
+ default=dict, blank=True, help_text='Configuration parameters for dynamic calculations'
+ )
+
def get_owner_object(self):
"""
Returns the object that has owner information
diff --git a/wger/measurements/utils/bmi.py b/wger/measurements/utils/bmi.py
index 8f4e1bc9e..fc32181a4 100644
--- a/wger/measurements/utils/bmi.py
+++ b/wger/measurements/utils/bmi.py
@@ -20,7 +20,7 @@ def calculate_bmi(user, category_id):
# wger
from wger.weight.models import WeightEntry
- profile = getattr(user, 'userprofile', None)
+ profile = user.userprofile
if not profile or not profile.height or profile.height <= 0:
return []
@@ -31,9 +31,9 @@ def calculate_bmi(user, category_id):
return [
{
- 'id': w.id, # Use integer IDs so the frontend state manager doesn't complain
- 'category': int(category_id), # Link it to the requested category
- 'date': w.date.isoformat() if hasattr(w.date, 'isoformat') else w.date,
+ 'id': w.id,
+ 'category': int(category_id), # link it to the requested category
+ 'date': w.date.isoformat(),
'value': round(float(w.weight) / height_sq, 2),
'notes': 'Auto-calculated from weight entry',
}