Put the validator in the correct field

"description" is simply the rendered HTML from "description_source"
This commit is contained in:
Roland Geider
2026-06-13 15:24:26 +02:00
parent ea7844731f
commit 6ffb22768b
4 changed files with 67 additions and 7 deletions

View File

@@ -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,

View File

@@ -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"""

View File

@@ -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)

View File

@@ -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