From 7f79d781e885b1c8a94f323c1918c966a09510aa Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Sat, 26 Sep 2026 23:23:44 +0200 Subject: [PATCH] Create the admin's body weight category, refuse client-made ones See https://github.com/wger-project/react/pull/1311 --- wger/measurements/api/serializers.py | 4 ++++ wger/measurements/tests/test_categories.py | 21 +++++++++++++++++++++ wger/tasks.py | 15 ++++++++++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/wger/measurements/api/serializers.py b/wger/measurements/api/serializers.py index 6c061be53..9115edc97 100644 --- a/wger/measurements/api/serializers.py +++ b/wger/measurements/api/serializers.py @@ -93,6 +93,10 @@ class CategorySerializer(serializers.ModelSerializer): """ if self.instance and metric_type != self.instance.metric_type: raise serializers.ValidationError('The metric type of a category cannot be changed') + + # The server creates the official category itself + if self.instance is None and metric_type == MetricType.BODY_WEIGHT: + raise serializers.ValidationError('The body weight category is managed by the server') return metric_type def validate_dynamic_type(self, dynamic_type): diff --git a/wger/measurements/tests/test_categories.py b/wger/measurements/tests/test_categories.py index 7eddd91ee..becade9a3 100644 --- a/wger/measurements/tests/test_categories.py +++ b/wger/measurements/tests/test_categories.py @@ -458,6 +458,27 @@ class OfficialCategoryTestCase(WgerTestCase): self.assertEqual(category.metric_type, MetricType.BODY_WEIGHT) self.assertEqual(category.unit, user.userprofile.weight_unit) + def test_create_body_weight_forbidden(self): + """ + Test that a body weight category cannot be created over the API, also + not by a user who has no official one + """ + Category.objects.filter(pk=self.official).delete() + + response = self.client.post( + reverse('measurement-category-list'), + {'name': 'Weight', 'unit': 'kg', 'metric_type': MetricType.BODY_WEIGHT}, + content_type='application/json', + ) + + self.assertEqual(response.status_code, 400) + self.assertIn('metric_type', response.data) + self.assertFalse( + Category.objects.filter( + user__username='test', metric_type=MetricType.BODY_WEIGHT + ).exists() + ) + def test_delete_official_forbidden(self): """ Test that the official category cannot be deleted over the API diff --git a/wger/tasks.py b/wger/tasks.py index 10894b022..013d0e5b8 100644 --- a/wger/tasks.py +++ b/wger/tasks.py @@ -136,7 +136,7 @@ def load_fixtures(context, settings_path: str = None): # Core call_command('loaddata', 'languages.json') call_command('loaddata', 'groups.json') - call_command('loaddata', 'users.json') + load_admin_fixture() call_command('loaddata', 'licenses.json') call_command('loaddata', 'setting_repetition_units.json') call_command('loaddata', 'setting_weight_units.json') @@ -270,8 +270,21 @@ def load_admin_fixture(): current_dir = os.path.dirname(os.path.abspath(__file__)) path = os.path.join(current_dir, 'core', 'fixtures/') + # can't be imported in global scope as it already requires + # the settings module during import + # Django + from django.contrib.auth.models import User + + # wger + from wger.measurements.models import Category + call_command('loaddata', path + 'users.json') + # loaddata skips the signal that gives every user the official body + # weight category + admin = User.objects.get(username='admin') + Category.get_or_create_body_weight(admin, unit=admin.userprofile.weight_unit) + class WgerConfig(Config): """