Self hosted FLOSS fitness/workout, nutrition and weight tracker
Also sync the user's profile
Roland Geidera84a467b185c
diff --git a/wger/core/api/views.py b/wger/core/api/views.pyindex 519c20f53..7832103be 100644--- a/wger/core/api/views.py+++ b/wger/core/api/views.py@@ -59,6 +59,7 @@ from rest_framework.response import Response# wger# The per-app powersync modules are imported for their side effect: each one# registers its handlers with wger.utils.powersync.REGISTRY at import time.+import wger.core.powersync # noqa: F401import wger.gallery.powersync # noqa: F401import wger.manager.powersync # noqa: F401import wger.measurements.powersync # noqa: F401diff --git a/wger/core/powersync.py b/wger/core/powersync.pynew file mode 100644index 000000000..103622793--- /dev/null+++ b/wger/core/powersync.py@@ -0,0 +1,35 @@+# 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/>.++# wger+from wger.core.api.serializers import UserprofileSerializer+from wger.core.models import UserProfile+from wger.utils.powersync import (+ PowerSyncHandler,+ register_handler,+)+++@register_handler+class UserProfileHandler(PowerSyncHandler):+ """+ The profile row is created together with the user account and must always+ exist, so PowerSync may only edit it+ """++ model = UserProfile+ serializer_class = UserprofileSerializer+ supports_create = False+ supports_delete = Falsediff --git a/wger/core/tests/powersync_base_test.py b/wger/core/tests/powersync_base_test.pyindex 7d3916b42..1444001f1 100644--- a/wger/core/tests/powersync_base_test.py+++ b/wger/core/tests/powersync_base_test.py@@ -261,8 +261,7 @@ class PowerSyncUpdateTestCase:class PowerSyncCreateNotAllowedTestCase:"""Mixin for tables where PUT (create) is explicitly rejected by the dispatcher- because creation must go through the REST API (Routine, NutritionPlan,- GalleryImage). PATCH and DELETE still work via PowerSync.+ because creation must go through the REST API (GalleryImage, etc.)"""def test_create_method_not_allowed(self):diff --git a/wger/core/tests/test_powersync.py b/wger/core/tests/test_powersync.pynew file mode 100644index 000000000..5554ba9ff--- /dev/null+++ b/wger/core/tests/test_powersync.py@@ -0,0 +1,59 @@+# 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++# Third Party+from rest_framework import status++# wger+from wger.core.models import UserProfile+from wger.core.tests import powersync_base_test+++class UserProfilePowerSyncTestCase(+ powersync_base_test.PowerSyncBaseTestCase,+ powersync_base_test.PowerSyncCreateNotAllowedTestCase,+ powersync_base_test.PowerSyncUpdateTestCase,+):+ """+ PowerSync handler for core.UserProfile. The profile is edit-only: creation+ happens during registration and deletion is refused, only PATCH reaches us.+ """++ table = 'core_userprofile'+ resource = UserProfile++ def setUp(self):+ super().setUp()+ self.pk_owned = UserProfile.objects.get(user__username=self.user_access).pk+ self.update_payload = {'id': self.pk_owned, 'weight_unit': 'lb'}++ def test_update_persists_weight_unit(self):+ """A PATCH round-trips the edited preference into the database."""+ self.authenticate()+ response = self.push('PATCH', {'id': self.pk_owned, 'weight_unit': 'lb'})+ self.assertEqual(response.status_code, status.HTTP_200_OK, response.content)++ profile = UserProfile.objects.get(pk=self.pk_owned)+ self.assertEqual(profile.weight_unit, 'lb')++ def test_delete_method_not_allowed(self):+ """Deleting the profile is refused"""++ self.authenticate()+ before = UserProfile.objects.count()+ response = self.push('DELETE', {'id': self.pk_owned})++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(response.json().get('error'), 'Method not allowed')+ self.assertEqual(UserProfile.objects.count(), before)diff --git a/wger/utils/powersync.py b/wger/utils/powersync.pyindex 94146523c..dd5d18faf 100644--- a/wger/utils/powersync.py+++ b/wger/utils/powersync.py@@ -79,6 +79,11 @@ class PowerSyncHandler:# running the create handler.supports_create: bool = True+ # Set to False for tables that must never be deleted through PowerSync+ # (UserProfile). handle_delete then returns "Method not allowed" instead+ # of deleting the row.+ supports_delete: bool = True+# If True, the handler passes ``user_id`` into the serializer context so# that owner-scoped fields (e.g. WorkoutLogSerializer.session) can filter# their queryset.@@ -163,6 +168,12 @@ class PowerSyncHandler:return self._validation_failed(serializer.errors)def handle_delete(self, payload: dict[str, Any], user_id: int) -> dict | None:+ if not self.supports_delete:+ return {+ 'error': 'Method not allowed',+ 'details': f'{self.label} cannot be deleted',+ }+logger.debug(f'Received PowerSync payload for {self.label} delete: {payload}')entry = self._get_or_none(payload, user_id)if entry is None: