Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add a chart_config column for per-category chart settings
Roland Geidera115c16f1f04
diff --git a/wger/measurements/api/serializers.py b/wger/measurements/api/serializers.pyindex 2941193a0..bf454ea2b 100644--- a/wger/measurements/api/serializers.py+++ b/wger/measurements/api/serializers.py@@ -22,6 +22,7 @@ from rest_framework import serializers# wgerfrom wger.measurements.limits import (+ CHART_CONFIG_MAX_BYTES,EXTRA_DATA_MAX_BYTES,VALUE_DECIMAL_PLACES,VALUE_MAX_DIGITS,@@ -37,6 +38,20 @@ from wger.measurements.models.category import ()+def validate_json_object(value, field: str, max_bytes: int):+ """+ Refuses anything that is not an object, or whose compact serialization+ (what the column stores) is larger than ``max_bytes``+ """+ if not isinstance(value, dict):+ raise serializers.ValidationError(f'{field} must be an object')++ size = len(json.dumps(value, separators=(',', ':'), ensure_ascii=False).encode())+ if size > max_bytes:+ raise serializers.ValidationError(f'{field} must be at most {max_bytes} bytes, got {size}')+ return value++class CategorySerializer(serializers.ModelSerializer):"""Measurement category serializer@@ -50,12 +65,19 @@ class CategorySerializer(serializers.ModelSerializer):'unit','metric_type','chart_type',+ 'chart_config','parent','order','is_official',)read_only_fields = ('is_official',)+ def validate_chart_config(self, chart_config):+ """+ Only the shape is the server's business, the keys are the clients'+ """+ return validate_json_object(chart_config, 'chart_config', CHART_CONFIG_MAX_BYTES)+def validate_metric_type(self, metric_type):"""The metric type is fixed once the category exists: the key of a typed@@ -249,16 +271,7 @@ class MeasurementSerializer(serializers.ModelSerializer):"""The unit key holds the unit the value was entered in"""- if not isinstance(extra_data, dict):- raise serializers.ValidationError('extra_data must be an object')-- # Compact separators: measure what the column stores, not whitespace- size = len(json.dumps(extra_data, separators=(',', ':'), ensure_ascii=False).encode())- if size > EXTRA_DATA_MAX_BYTES:- raise serializers.ValidationError(- f'extra_data must be at most {EXTRA_DATA_MAX_BYTES} bytes, got {size}'- )- return extra_data+ return validate_json_object(extra_data, 'extra_data', EXTRA_DATA_MAX_BYTES)def validate(self, data):"""diff --git a/wger/measurements/limits.py b/wger/measurements/limits.pyindex 1eea384c6..4f67212f2 100644--- a/wger/measurements/limits.py+++ b/wger/measurements/limits.py@@ -37,6 +37,12 @@ unbounded, so this is what keeps clients from pushing arbitrarily large blobs;the largest real payload (the importer's provenance) is ~200 bytes."""+CHART_CONFIG_MAX_BYTES = 1000+"""+Upper bound for a category's ``chart_config``, same purpose as+``EXTRA_DATA_MAX_BYTES``; the real payload is a few dozen bytes.+"""+class Limits(NamedTuple):"""diff --git a/wger/measurements/migrations/0006_health_sync.py b/wger/measurements/migrations/0006_health_sync.pyindex caafe0707..d1332f88b 100644--- a/wger/measurements/migrations/0006_health_sync.py+++ b/wger/measurements/migrations/0006_health_sync.py@@ -78,6 +78,11 @@ class Migration(migrations.Migration):verbose_name='Chart type',),),+ migrations.AddField(+ model_name='category',+ name='chart_config',+ field=models.JSONField(blank=True, default=dict, verbose_name='Chart config'),+ ),migrations.AddConstraint(model_name='category',constraint=models.UniqueConstraint(diff --git a/wger/measurements/models/category.py b/wger/measurements/models/category.pyindex 9cd625a0e..f889379ba 100644--- a/wger/measurements/models/category.py+++ b/wger/measurements/models/category.py@@ -210,6 +210,15 @@ class Category(models.Model):null=True,)+ # Taste-level chart settings, e.g. {"trend": "sluggish"}: the keys are+ # client business, a missing one means the client's default. PATCH replaces+ # the object, so clients merge back keys they do not know (like extra_data)+ chart_config = models.JSONField(+ verbose_name='Chart config',+ default=dict,+ blank=True,+ )+# Multi-value measurements (e.g. blood pressure) are modelled as a parent# category holding one child category per component. Only leaf categories# (no children) carry measurements; nesting is limited to one level.diff --git a/wger/measurements/powersync.py b/wger/measurements/powersync.pyindex c379bf945..5ea568a8f 100644--- a/wger/measurements/powersync.py+++ b/wger/measurements/powersync.py@@ -13,10 +13,6 @@# 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-import json-from typing import Any-# wgerfrom wger.measurements.api.serializers import (CategorySerializer,@@ -46,6 +42,7 @@ class CategoryHandler(PowerSyncHandler):model = Categoryserializer_class = CategorySerializerviewset_class = CategoryViewSet+ json_fields = frozenset({'chart_config'})# The serializer checks the "one category per metric type" rule, for which# it needs to know whose categories to look at@@ -69,18 +66,7 @@ class MeasurementHandler(PowerSyncHandler):serializer_class = MeasurementSerializerviewset_class = MeasurementViewSetuser_filter = 'category__user_id'-- def preprocess_payload(self, payload: dict[str, Any]) -> dict[str, Any]:- # PowerSync clients store JSON columns as text; decode before- # validation. Malformed JSON is passed through unchanged so the- # serializer rejects it visibly.- extra_data = payload.get('extra_data')- if isinstance(extra_data, str):- try:- payload['extra_data'] = json.loads(extra_data) if extra_data else {}- except ValueError:- pass- return payload+ json_fields = frozenset({'extra_data'})def create_save_kwargs(self, payload, user_id):# Ownership is enforced through the category FK, not via a directdiff --git a/wger/measurements/tests/test_categories.py b/wger/measurements/tests/test_categories.pyindex 8f6a416ff..5580f87cb 100644--- a/wger/measurements/tests/test_categories.py+++ b/wger/measurements/tests/test_categories.py@@ -120,6 +120,66 @@ class CategoryGroupApiTestCase(WgerTestCase):self.assertFalse(Category.objects.filter(pk=child.data['id']).exists())+class ChartConfigApiTestCase(WgerTestCase):+ """+ The per-category chart settings+ """++ # Pinned in test-measurement-categories.json+ category = 'cccccccc-cccc-cccc-cccc-000000000003' # user 'test'++ def setUp(self):+ super().setUp()+ self.user_login('test')+ self.url = reverse('measurement-category-detail', kwargs={'pk': self.category})++ def patch_config(self, chart_config):+ return self.client.patch(+ self.url,+ {'chart_config': chart_config},+ content_type='application/json',+ )++ def test_defaults_to_an_empty_object(self):+ response = self.client.get(self.url)++ self.assertEqual(response.status_code, 200)+ self.assertEqual(response.data['chart_config'], {})++ def test_roundtrip(self):+ """+ Test that the keys are stored as sent: they are client business+ """+ response = self.patch_config({'trend': 'sluggish', 'average_window': 14})++ self.assertEqual(response.status_code, 200)+ self.assertEqual(response.data['chart_config'], {'trend': 'sluggish', 'average_window': 14})++ def test_unknown_keys_are_kept(self):+ """+ Test that a key this release knows nothing about is not dropped+ """+ response = self.patch_config({'goal_line': 75})++ self.assertEqual(response.status_code, 200)+ self.assertEqual(Category.objects.get(pk=self.category).chart_config, {'goal_line': 75})++ def test_non_object_rejected(self):+ response = self.patch_config(['sluggish'])++ self.assertEqual(response.status_code, 400)+ self.assertIn('chart_config', response.data)++ def test_size_bound(self):+ """+ Test that the column cannot be used as a blob store+ """+ response = self.patch_config({'trend': 'x' * 1000})++ self.assertEqual(response.status_code, 400)+ self.assertIn('chart_config', response.data)++class TypedCategoryTestCase(WgerTestCase):"""Identity and structural rules of the categories with a metric typediff --git a/wger/measurements/tests/test_powersync.py b/wger/measurements/tests/test_powersync.pyindex a63919173..9db2d86c3 100644--- a/wger/measurements/tests/test_powersync.py+++ b/wger/measurements/tests/test_powersync.py@@ -58,6 +58,38 @@ class CategoryPowerSyncTestCase(powersync_base_test.PowerSyncResourceTestCase):fk_ownership = (('parent', CATEGORY_OTHER_UUID),)+ def test_create_decodes_chart_config_string(self):+ """+ PowerSync clients store JSON columns as text; the handler decodes the+ string before validation+ """+ self.authenticate()+ payload = {**self.create_payload, 'chart_config': '{"trend": "sluggish"}'}+ response = self.push('PUT', payload)++ self.assertEqual(response.status_code, status.HTTP_200_OK, response.content)+ self.assertEqual(response.json(), {'status': 'ok!'})+ self.assertEqual(+ Category.objects.get(pk=payload['id']).chart_config,+ {'trend': 'sluggish'},+ )++ def test_update_decodes_chart_config_string(self):+ """+ Changing the setting on an existing category, which is the path the+ form takes+ """+ self.authenticate()+ payload = {**self.update_payload, 'chart_config': '{"trend": "reactive"}'}+ response = self.push('PATCH', payload)++ self.assertEqual(response.status_code, status.HTTP_200_OK, response.content)+ self.assertEqual(response.json(), {'status': 'ok!'})+ self.assertEqual(+ Category.objects.get(pk=self.pk_owned).chart_config,+ {'trend': 'reactive'},+ )+class TypedCategoryPowerSyncTestCase(powersync_base_test.PowerSyncBaseTestCase):"""diff --git a/wger/utils/powersync.py b/wger/utils/powersync.pyindex ddb266cae..7f76eb787 100644--- a/wger/utils/powersync.py+++ b/wger/utils/powersync.py@@ -23,6 +23,7 @@ name and calls ``dispatch(http_verb, ...)``."""# Standard Library+import jsonimport loggingfrom typing import Any@@ -88,6 +89,12 @@ class PowerSyncHandler:# their queryset.pass_user_id_in_context: bool = False+ # JSONField columns of this table. PowerSync clients store them as text, so+ # they arrive as a string and are decoded before validation. One that is+ # missing here is refused as "must be an object", permanently, so every+ # JSONField of the table belongs in this set.+ json_fields: frozenset[str] = frozenset()+@propertydef label(self) -> str:"""Human-readable model name used in log lines and error details."""@@ -105,6 +112,21 @@ class PowerSyncHandler:"""return payload+ def _decode_json_fields(self, payload: dict[str, Any]) -> dict[str, Any]:+ """+ Decodes the ``json_fields``, which a PowerSync client sends as text.+ Malformed JSON is passed through unchanged, so the serializer refuses+ it visibly instead of the field silently arriving empty.+ """+ for field in self.json_fields:+ value = payload.get(field)+ if isinstance(value, str):+ try:+ payload[field] = json.loads(value) if value else {}+ except ValueError:+ pass+ return payload+def create_save_kwargs(self, payload: dict[str, Any], user_id: int) -> dict[str, Any]:"""kwargs forwarded to ``serializer.save()`` on create. Default forces the@@ -126,7 +148,7 @@ class PowerSyncHandler:}logger.debug(f'Received PowerSync payload for {self.label} create: {payload}')- payload = self.preprocess_payload(payload)+ payload = self.preprocess_payload(self._decode_json_fields(payload))# Idempotent create: a redelivered PUT for a row this user already owns is# a no-op success, not a PK-conflict error (PowerSync redelivers writes).@@ -149,7 +171,7 @@ class PowerSyncHandler:def handle_update(self, payload: dict[str, Any], user_id: int) -> dict | None:logger.debug(f'Received PowerSync payload for {self.label} update: {payload}')- payload = self.preprocess_payload(payload)+ payload = self.preprocess_payload(self._decode_json_fields(payload))entry = self._get_or_none(payload, user_id)if entry is None: