Self hosted FLOSS fitness/workout, nutrition and weight tracker
Simplify and consolidate serializer behaviour
Roland Geider73df8abbcc5b
diff --git a/wger/core/api/serializers.py b/wger/core/api/serializers.pyindex 392e7cbaf..0ffa73377 100644--- a/wger/core/api/serializers.py+++ b/wger/core/api/serializers.py@@ -272,7 +272,6 @@ class LanguageCheckSerializer(serializers.Serializer):# Try to detect the languagedetector = (LanguageDetectorBuilder.from_all_languages()- .with_low_accuracy_mode().with_preloaded_language_models().build())diff --git a/wger/exercises/api/serializers.py b/wger/exercises/api/serializers.pyindex 6c2fa04a0..b9b421f8e 100644--- a/wger/exercises/api/serializers.py+++ b/wger/exercises/api/serializers.py@@ -24,14 +24,11 @@ from django.db.models import Q# Third Partyfrom actstream import action as actstream_action-from lingua import LanguageDetectorBuilderfrom rest_framework import serializers# wger-from wger.core.models import (- Language,- License,-)+from wger.core.models import License+from wger.exercises.api.validators import validate_language_matchesfrom wger.exercises.models import (Alias,DeletionLog,@@ -225,56 +222,12 @@ class ExerciseCommentSerializer(serializers.ModelSerializer):'comment',)--class ExerciseCommentSubmissionSerializer(serializers.ModelSerializer):- """- ExerciseComment submission serializer- """-- class Meta:- model = ExerciseComment- fields = ('comment',)-- def create(self, validated_data, **kwargs):- """- Custom create-method to handle the 'translation' keyword argument- and set the foreign key relationship.- """- translation: Translation = kwargs.get('translation')- if not translation:- raise serializers.ValidationError(- 'A translation object is required for creating a comment.'- )-- # Validate the language of the description- # -> This is done here instead of in the serializer's validate method- # because the language is not available in the serializer's initial_data- detector = (- LanguageDetectorBuilder.from_all_languages()- .with_low_accuracy_mode()- .with_preloaded_language_models()- .build()- )- language = translation.language-- # Try to detect the language- detected_language = detector.detect_language_of(validated_data['comment'])- detected_language_code = detected_language.iso_code_639_1.name.lower()- if detected_language_code != language.short_name.lower():- raise serializers.ValidationError(- {- 'language': f'The detected language of the comment is "{detected_language.name.capitalize()}" '- f'({detected_language_code}), which does not match your selected language: '- f'"{language.full_name.capitalize()}" ({language.short_name}). If you believe '- f'this is incorrect, try adding more content or rephrasing your text, as '- f'language detection works better with longer or more complete sentences.'- }- )-- # Create the comment with the parent translation- comment = ExerciseComment.objects.create(translation=translation, **validated_data)- _log_action_creation(self, comment)- return comment+ def validate(self, data):+ comment = data.get('comment')+ translation = data.get('translation') or (self.instance and self.instance.translation)+ if comment and translation:+ validate_language_matches(comment, translation.language, 'comment')+ return super().validate(data)class ExerciseAliasSerializer(serializers.ModelSerializer):@@ -292,32 +245,6 @@ class ExerciseAliasSerializer(serializers.ModelSerializer):)-class ExerciseAliasSubmissionSerializer(serializers.ModelSerializer):- """- ExerciseAlias submission serializer- """-- class Meta:- model = Alias- fields = ('alias',)-- def create(self, validated_data, **kwargs):- """- Custom create-method to handle the 'translation' keyword argument- and set the foreign key relationship.- """- translation = kwargs.get('translation')- if not translation:- raise serializers.ValidationError(- 'A translation object is required for creating an alias.'- )-- # Create the Alias with the parent translation- alias = Alias.objects.create(translation=translation, **validated_data)- _log_action_creation(self, alias)- return alias--class ExerciseInfoAliasSerializer(serializers.ModelSerializer):"""Exercise alias serializer for info endpoint@@ -422,87 +349,6 @@ class ExerciseTranslationInfoSerializer(serializers.ModelSerializer):)-class ExerciseTranslationSubmissionSerializer(serializers.ModelSerializer):- """- Exercise translation submission serializer- """-- language = serializers.PrimaryKeyRelatedField(queryset=Language.objects.all())- aliases = ExerciseAliasSubmissionSerializer(many=True, required=False)- comments = ExerciseCommentSubmissionSerializer(many=True, required=False)-- class Meta:- model = Translation- fields = (- 'name',- 'description_source',- 'language',- 'aliases',- 'comments',- 'license_author',- )-- def validate(self, data):- """- Custom validator to ensure the detected language of the description corresponds with the- provided language.- """- detector = (- LanguageDetectorBuilder.from_all_languages()- .with_low_accuracy_mode()- .with_preloaded_language_models()- .build()- )-- language = data.get('language')- description = data.get('description_source')-- # Try to detect the language- detected_language = detector.detect_language_of(description)- detected_language_code = detected_language.iso_code_639_1.name.lower()- if detected_language_code != language.short_name.lower():- raise serializers.ValidationError(- {- 'language': f'The detected language of the description is "{detected_language.name.capitalize()}" '- f'({detected_language_code}), which does not match your selected language: '- f'"{language.full_name.capitalize()}" ({language.short_name}). If you believe '- f'this is incorrect, try adding more content or rephrasing your text, as '- f'language detection works better with longer or more complete sentences.'- }- )-- return super().validate(data)-- def create(self, validated_data, **kwargs):- """- Custom create-method to handle the 'exercise' keyword argument- and set the foreign key relationship.- """- aliases_data = validated_data.pop('aliases', [])- comments_data = validated_data.pop('comments', [])- exercise = kwargs.get('exercise')- if not exercise:- raise serializers.ValidationError(- 'Translation object is required for creating an Alias'- )-- # Create the translation with the parent exercise- translation = Translation.objects.create(exercise=exercise, **validated_data)- _log_action_creation(self, translation)-- # Create the individual aliases- for alias_data in aliases_data:- alias_serializer = self.fields['aliases'].child- alias_serializer.create(validated_data=alias_data, translation=translation)-- # Create the individual comments- for comment_data in comments_data:- comment_serializer = self.fields['comments'].child- comment_serializer.create(validated_data=comment_data, translation=translation)-- return translation--class ExerciseTranslationSerializer(serializers.ModelSerializer):"""Exercise translation serializer@@ -533,8 +379,10 @@ class ExerciseTranslationSerializer(serializers.ModelSerializer):def validate(self, value):"""- Check that there is only one language per exercise+ Check that there is only one language per exercise, and that the+ detected language of the description matches the declared language."""+if value.get('language'):# Editing an existing object# -> Check if the language already exists, excluding the current object@@ -546,9 +394,13 @@ class ExerciseTranslationSerializer(serializers.ModelSerializer):raise serializers.ValidationError(f'There is already a translation for this exercise in {value["language"]}')+# Creating a new object- # -> Check if the language already exists- else:+ # -> Check if the language already exists. When ``exercise`` isn't+ # in the payload (submission flow creates a fresh exercise), the+ # check can't run and isn't relevant and there are no duplicates+ # by definition.+ elif value.get('exercise'):if Translation.objects.filter(exercise=value['exercise'],language=value['language'],@@ -557,9 +409,84 @@ class ExerciseTranslationSerializer(serializers.ModelSerializer):f'There is already a translation for this exercise in {value["language"]}')+ # Verify the detected language of the description matches the declared+ # language. On PATCH either field may be absent, so we fall back to the+ # persisted value where available.+ description = value.get('description_source')+ language = value.get('language') or (self.instance and self.instance.language)+ if description and language:+ validate_language_matches(description, language, 'description')+return super().validate(value)+class ExerciseTranslationSubmissionSerializer(ExerciseTranslationSerializer):+ """+ Translation serializer used as a nested child of ``ExerciseSubmissionSerializer``.++ Differs from the regular serializer only because:+ - the ``exercise`` FK isn't known until the parent creates it (passed via ``create()`` kwargs);+ - the payload also accepts nested ``aliases`` and ``comments`` lists,+ which the regular CRUD endpoint doesn't.+ """++ aliases = ExerciseAliasSerializer(many=True, required=False)+ comments = ExerciseCommentSerializer(many=True, required=False)++ class Meta(ExerciseTranslationSerializer.Meta):+ fields = (+ 'name',+ 'description_source',+ 'language',+ 'aliases',+ 'comments',+ 'license_author',+ )++ def __init__(self, *args, **kwargs):+ super().__init__(*args, **kwargs)++ # ``translation`` is assigned by the parent submission in create()+ for nested in ('aliases', 'comments'):+ self.fields[nested].child.fields.pop('translation', None)++ def validate(self, data):+ data = super().validate(data)++ # Language of each child comment must also match the one in the declared translation+ language = data.get('language')+ if language:+ for comment_data in data.get('comments', []):+ comment_text = comment_data.get('comment')+ if comment_text:+ validate_language_matches(comment_text, language, 'comment')++ return data++ def create(self, validated_data, **kwargs):+ aliases_data = validated_data.pop('aliases', [])+ comments_data = validated_data.pop('comments', [])+ exercise = kwargs.get('exercise')+ if not exercise:+ raise serializers.ValidationError(+ 'Exercise is required to create a translation via submission.'+ )+ validated_data['exercise'] = exercise++ translation = Translation.objects.create(**validated_data)+ _log_action_creation(self, translation)++ for alias_data in aliases_data:+ alias = Alias.objects.create(translation=translation, **alias_data)+ _log_action_creation(self, alias)++ for comment_data in comments_data:+ comment = ExerciseComment.objects.create(translation=translation, **comment_data)+ _log_action_creation(self, comment)++ return translation++class ExerciseInfoSerializer(serializers.ModelSerializer):"""Exercise info serializerdiff --git a/wger/exercises/api/validators.py b/wger/exercises/api/validators.pynew file mode 100644index 000000000..9a97f9c2c--- /dev/null+++ b/wger/exercises/api/validators.py@@ -0,0 +1,51 @@+# 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/>.++# Third Party+from lingua import LanguageDetectorBuilder+from rest_framework import serializers++# wger+from wger.core.models import Language+++def validate_language_matches(text: str, language: Language, source_label: str) -> None:+ """+ Raise ``ValidationError`` if the detected language of ``text`` doesn't+ match the ``short_name`` of the given ``Language``.++ ``source_label`` (e.g. ``'description'``, ``'comment'``) is used in the+ error message to point the caller at the offending field.+ """++ detector = (+ LanguageDetectorBuilder.from_all_languages()+ .with_preloaded_language_models()+ .build()+ )++ detected_language = detector.detect_language_of(text)+ detected_language_code = detected_language.iso_code_639_1.name.lower()+ if detected_language_code != language.short_name.lower():+ raise serializers.ValidationError(+ {+ 'language': f'The detected language of the {source_label} is '+ f'"{detected_language.name.capitalize()}" ({detected_language_code}), '+ f'which does not match your selected language: "{language.full_name.capitalize()}" '+ f'({language.short_name}). If you believe this is incorrect, try adding '+ f'more content or rephrasing your text, as language detection works'+ f'better with longer or more complete sentences.'+ }+ )diff --git a/wger/exercises/tests/api/test_exercise_submission_api.py b/wger/exercises/tests/api/test_exercise_submission_api.pyindex 3e6339863..0fe1ff8ac 100644--- a/wger/exercises/tests/api/test_exercise_submission_api.py+++ b/wger/exercises/tests/api/test_exercise_submission_api.py@@ -154,6 +154,49 @@ class SearchSubmissionApiTestCase(BaseTestCase, ApiBaseTestCase):response_data.get('translations'), ['You must provide at least one translation.'])+ def test_unsuccessful_submission_language_mismatch_description(self):+ """+ A translation whose description language doesn't match the declared language+ field is rejected.+ """+ self.authenticate('admin')++ payload = self.get_payload()+ # Swap the EN description for a clearly German one.+ payload['translations'][0]['description_source'] = (+ 'Das ist eine deutsche Beschreibung der Übung, mit ausreichend Text '+ 'damit die Spracherkennung sie zuverlässig erkennen kann.'+ )++ counts_before = self.get_counts()+ response = self.client.post(self.url, data=payload)+ counts_after = self.get_counts()++ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertEqual(counts_before, counts_after)+ self.assertIn('language', response.json().get('translations')[0])++ def test_unsuccessful_submission_language_mismatch_comment(self):+ """+ A comment whose detected language doesn't match the parent translation's+ language is rejected.+ """+ self.authenticate('admin')++ payload = self.get_payload()+ # Replace one of the English comments with a clearly French one.+ payload['translations'][0]['comments'][0]['comment'] = (+ 'Ceci est un long commentaire en français qui ne correspond pas du '+ 'tout à la traduction anglaise et devrait être rejeté.'+ )++ counts_before = self.get_counts()+ response = self.client.post(self.url, data=payload)+ counts_after = self.get_counts()++ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertEqual(counts_before, counts_after)+def test_unsuccessful_submission_no_english_translations(self):"""If any part of the exercise submission fails, no exercise is created.diff --git a/wger/exercises/tests/test_exercise_comments.py b/wger/exercises/tests/test_exercise_comments.pyindex 66fdfabc4..61c5846b2 100644--- a/wger/exercises/tests/test_exercise_comments.py+++ b/wger/exercises/tests/test_exercise_comments.py@@ -12,6 +12,9 @@## You should have received a copy of the GNU Affero General Public License+# Third Party+from rest_framework import status+# wgerfrom wger.core.tests.api_base_test import ExerciseCrudApiTestCasefrom wger.core.tests.base_testcase import WgerTestCase@@ -39,7 +42,29 @@ class ExerciseCommentApiTestCase(ActstreamApiMixin, ExerciseCrudApiTestCase):pk = 1resource = ExerciseCommentdata = {- 'comment': 'a cool comment',+ 'comment': 'This is a clearly English comment used for testing purposes.','translation': '1','id': 1,}++ def test_post_rejects_language_mismatch(self):+ """+ Adding a comment whose language doesn't match the parent translation's+ language is rejected.+ """++ # Translation pk=1 has language=2 (en); posting a German comment must+ # be rejected.+ self.authenticate('trainer1')+ response = self.client.post(+ self.url,+ data={+ 'translation': 1,+ 'comment': (+ 'Ein ausreichend langer deutscher Kommentar, damit die '+ 'Spracherkennung greifen kann und ihn ablehnt.'+ ),+ },+ )+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertIn('language', response.json())diff --git a/wger/exercises/tests/test_exercise_translation.py b/wger/exercises/tests/test_exercise_translation.pyindex bc92b5695..d42b962e2 100644--- a/wger/exercises/tests/test_exercise_translation.py+++ b/wger/exercises/tests/test_exercise_translation.py@@ -195,7 +195,10 @@ class ExerciseTranslationCustomApiTestCase(ActstreamApiMixin, ExerciseCrudApiTes"""payload = {'name': 'A new translation',- 'description_source': 'Bend your knees and squat down.',+ '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,}@@ -206,9 +209,9 @@ class ExerciseTranslationCustomApiTestCase(ActstreamApiMixin, ExerciseCrudApiTesself.assertEqual(response.status_code, status.HTTP_201_CREATED)translation = Translation.objects.get(pk=response.json()['id'])- self.assertEqual(translation.description_source, 'Bend your knees and squat down.')+ self.assertEqual(translation.description_source, payload['description_source'])# Backend rendered the markdown into description on save()- self.assertIn('Bend your knees', translation.description)+ self.assertIn('Beuge die Knie', translation.description)def test_cant_set_description_directly(self):"""@@ -218,7 +221,10 @@ class ExerciseTranslationCustomApiTestCase(ActstreamApiMixin, ExerciseCrudApiTespayload = {'name': 'A new translation','description': '<script>alert(1)</script><p>raw html</p>',- 'description_source': 'safe markdown',+ 'description_source': (+ 'Sicherer Markdown-Text der nur in deutscher Sprache geschrieben '+ 'ist um die Spracherkennung zuverlässig zu bestehen.'+ ),'language': 1,'exercise': 1,}@@ -232,7 +238,7 @@ class ExerciseTranslationCustomApiTestCase(ActstreamApiMixin, ExerciseCrudApiTesself.assertNotIn('<script>', translation.description)self.assertNotIn('raw html', translation.description)- self.assertIn('safe markdown', translation.description)+ self.assertIn('Sicherer Markdown-Text', translation.description)def test_cant_patch_description_directly(self):"""@@ -260,6 +266,71 @@ class ExerciseTranslationCustomApiTestCase(ActstreamApiMixin, ExerciseCrudApiTestranslation = Translation.objects.get(pk=self.pk)self.assertEqual(translation.description, ' The wild boar is a suid native...')+ def test_post_accepts_matching_language(self):+ """+ POSTing a translation whose description is in the declared language+ succeeds (positive counterpart to ``test_post_rejects_language_mismatch``).+ """+ Translation.objects.filter(exercise_id=1, language_id=1).delete()++ payload = {+ 'name': 'Eine neue Übersetzung',+ 'description_source': (+ 'Halte die Hantel mit beiden Händen und führe die Bewegung '+ 'kontrolliert aus, dabei den Rücken stets gerade.'+ ),+ 'language': 1,+ 'exercise': 1,+ }+ self.authenticate('trainer1')+ response = self.client.post(self.url, data=payload)+ self.assertEqual(response.status_code, status.HTTP_201_CREATED)++ def test_post_rejects_language_mismatch(self):+ """+ POSTing a translation whose description language doesn't match the+ declared language field is rejected.+ """++ # Free up exercise=1/language=2 so the duplicate-translation check+ # doesn't fire before the language-mismatch check.+ Translation.objects.filter(exercise_id=1, language_id=2).delete()++ payload = {+ 'name': 'A new translation',+ 'description_source': (+ 'Das ist eine deutsche Beschreibung der Übung, mit ausreichend '+ 'Text damit die Spracherkennung sie zuverlässig erkennen kann.'+ ),+ 'language': 2,+ 'exercise': 1,+ }+ self.authenticate('trainer1')+ response = self.client.post(self.url, data=payload)+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertIn('language', response.json())++ def test_patch_rejects_language_mismatch(self):+ """+ PATCHing ``description_source`` to text in a different language than+ the (existing) translation's language is rejected.+ """++ # Translation pk=1 has language=2 (en); push a clearly German+ # description and expect the validator to reject it.+ self.authenticate('trainer1')+ response = self.client.patch(+ self.url_detail,+ data={+ 'description_source': (+ 'Eine ausreichend lange deutsche Beschreibung, damit die '+ 'Spracherkennung sicher greifen kann.'+ ),+ },+ )+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertIn('language', response.json())+def test_post_only_one_language_per_base(self):"""Test that it's not possible to add a second translation for the same