From 6ffb22768b348dd4a48663ca3f20d64a2e2a951d Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Sat, 13 Jun 2026 15:24:26 +0200 Subject: [PATCH] Put the validator in the correct field "description" is simply the rendered HTML from "description_source" --- wger/exercises/api/serializers.py | 1 - wger/exercises/models/translation.py | 10 +++-- .../tests/api/test_exercise_submission_api.py | 24 +++++++++++- .../tests/test_exercise_translation.py | 39 ++++++++++++++++++- 4 files changed, 67 insertions(+), 7 deletions(-) diff --git a/wger/exercises/api/serializers.py b/wger/exercises/api/serializers.py index 9971e3deb..cc02666e3 100644 --- a/wger/exercises/api/serializers.py +++ b/wger/exercises/api/serializers.py @@ -376,7 +376,6 @@ class ExerciseTranslationSerializer(serializers.ModelSerializer): id = serializers.IntegerField(required=False, read_only=True) uuid = serializers.UUIDField(required=False, read_only=True) - description_source = serializers.CharField(required=False, allow_blank=True) exercise = serializers.PrimaryKeyRelatedField( queryset=Exercise.objects.all(), required=True, diff --git a/wger/exercises/models/translation.py b/wger/exercises/models/translation.py index f6b3f80ff..a9e21559b 100644 --- a/wger/exercises/models/translation.py +++ b/wger/exercises/models/translation.py @@ -49,14 +49,16 @@ class Translation(AbstractLicenseModel, AbstractHistoryMixin, models.Model): description = models.TextField( max_length=2000, verbose_name='Description', - validators=[MinLengthValidator(40)], + blank=True, ) - """Description on how to perform the exercise""" + """ + Rendered HTML describing how to perform the exercise, generated from + ``description_source`` on save + """ description_source = models.TextField( verbose_name='Description (Source)', - blank=True, - default='', + validators=[MinLengthValidator(40)], ) """The raw Markdown source""" diff --git a/wger/exercises/tests/api/test_exercise_submission_api.py b/wger/exercises/tests/api/test_exercise_submission_api.py index 0fe1ff8ac..fe3365fe8 100644 --- a/wger/exercises/tests/api/test_exercise_submission_api.py +++ b/wger/exercises/tests/api/test_exercise_submission_api.py @@ -63,7 +63,10 @@ class SearchSubmissionApiTestCase(BaseTestCase, ApiBaseTestCase): }, { 'name': '2 Handed Kettlebell Swing', - 'description_source': 'das ist die Beschreibung für die Übung', + 'description_source': ( + 'Das ist eine ausführliche deutsche Beschreibung der Übung mit ' + 'genügend Text, damit die Spracherkennung sie zuverlässig erkennt.' + ), 'language': 1, 'license_author': 'tester', }, @@ -256,3 +259,22 @@ class SearchSubmissionApiTestCase(BaseTestCase, ApiBaseTestCase): self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertIsNotNone(exercise.variation_group) self.assertEqual(exercise.variation_group, connected_exercise.variation_group) + + def test_submission_without_description_fails(self): + """ + A submission whose translations carry no description is rejected — a + description is mandatory when creating an exercise. + """ + self.authenticate('admin') + + payload = self.get_payload() + for translation in payload['translations']: + translation.pop('description_source', None) + + before = self.get_counts() + response = self.client.post(self.url, data=payload) + after = self.get_counts() + + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + # Nothing was created (the create runs in a transaction) + self.assertEqual(before, after) diff --git a/wger/exercises/tests/test_exercise_translation.py b/wger/exercises/tests/test_exercise_translation.py index d42b962e2..65794c64e 100644 --- a/wger/exercises/tests/test_exercise_translation.py +++ b/wger/exercises/tests/test_exercise_translation.py @@ -121,7 +121,10 @@ class ExerciseTranslationCustomApiTestCase(ActstreamApiMixin, ExerciseCrudApiTes data = { 'name': 'A new name', - 'description': 'The wild boar is a suid native to much of Eurasia and North Africa', + 'description_source': ( + 'Beuge die Knie und gehe in die tiefe Hocke, halte dabei den ' + 'Rücken gerade und die Brust aufrecht.' + ), 'language': 1, 'exercise': 1, } @@ -129,6 +132,22 @@ class ExerciseTranslationCustomApiTestCase(ActstreamApiMixin, ExerciseCrudApiTes def get_resource_name(self): return 'exercise-translation' + def test_patch_user_verified_email(self): + """ + PATCH each field individually. ``pk=1`` is an English translation, so + the German ``description_source`` from ``self.data`` would trip the + language-match validator when patched on its own + """ + self.authenticate('trainer1') + patches = dict(self.data) + patches['description_source'] = ( + 'A sufficiently long English description so the language detector ' + 'can reliably recognise it as English.' + ) + for key, value in patches.items(): + response = self.client.patch(self.url_detail, data={key: value}) + self.assertEqual(response.status_code, status.HTTP_200_OK) + def test_cant_change_exercise_id(self): """ Test that it is not possible to change the exercise id of an existing @@ -213,6 +232,24 @@ class ExerciseTranslationCustomApiTestCase(ActstreamApiMixin, ExerciseCrudApiTes # Backend rendered the markdown into description on save() self.assertIn('Beuge die Knie', translation.description) + def test_post_without_description_fails(self): + """ + Creating a translation without a description is rejected, the model + requires one, and the API must enforce it on create. + """ + payload = { + 'name': 'A new translation', + 'language': 1, + 'exercise': 1, + } + Translation.objects.filter(exercise_id=1, language_id=1).delete() + + self.authenticate('trainer1') + response = self.client.post(self.url, data=payload) + + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertIn('description_source', response.json()) + def test_cant_set_description_directly(self): """ ``description`` is read-only, passing it on POST must be silently