diff --git a/arkindex/documents/serializers/ml.py b/arkindex/documents/serializers/ml.py index bf6a02b996cd1349be93d69867ea3f0f786fb4dc..8de9dd0a13732d25d427e55936f911b36325023f 100644 --- a/arkindex/documents/serializers/ml.py +++ b/arkindex/documents/serializers/ml.py @@ -14,6 +14,7 @@ from arkindex.documents.models import ( Element, ElementType, MLClass, + TextOrientation, Transcription, ) from arkindex.documents.serializers.light import ElementZoneSerializer @@ -218,6 +219,7 @@ class TranscriptionSerializer(serializers.ModelSerializer): read_only=True, help_text='This field is deprecated; please use the `confidence` field instead.', ) + orientation = EnumField(TextOrientation) class Meta: model = Transcription @@ -227,6 +229,7 @@ class TranscriptionSerializer(serializers.ModelSerializer): 'text', 'score', 'confidence', + 'orientation', 'worker_version_id', ) @@ -268,10 +271,11 @@ class TranscriptionCreateSerializer(serializers.ModelSerializer): max_value=1, required=False, ) + orientation = EnumField(TextOrientation, default=TextOrientation.HorizontalLeftToRight, required=False) class Meta: model = Transcription - fields = ('text', 'worker_version', 'score', 'confidence') + fields = ('text', 'worker_version', 'score', 'confidence', 'orientation') def validate(self, data): data = super().validate(data) diff --git a/arkindex/documents/tests/test_create_transcriptions.py b/arkindex/documents/tests/test_create_transcriptions.py index 63bf23516a86b53f89a5b06b8459b2ba32a39056..5e21010034cc47f4801fe6c8b12a2ae4b131581d 100644 --- a/arkindex/documents/tests/test_create_transcriptions.py +++ b/arkindex/documents/tests/test_create_transcriptions.py @@ -5,7 +5,7 @@ from django.urls import reverse from rest_framework import status from arkindex.dataimport.models import WorkerVersion -from arkindex.documents.models import Corpus, Transcription +from arkindex.documents.models import Corpus, TextOrientation, Transcription from arkindex.project.tests import FixtureAPITestCase from arkindex.users.models import Role, User @@ -73,6 +73,7 @@ class TestTranscriptionCreate(FixtureAPITestCase): 'confidence': None, 'score': None, 'text': 'A perfect day in a perfect place', + 'orientation': TextOrientation.HorizontalLeftToRight.value, 'worker_version_id': None, }) @@ -101,6 +102,41 @@ class TestTranscriptionCreate(FixtureAPITestCase): 2 ) + def test_create_transcription_with_orientation(self): + """ + Check that a transcription is created with the specified orientation + """ + self.client.force_login(self.user) + response = self.client.post( + reverse('api:transcription-create', kwargs={'pk': self.line.id}), + format='json', + data={'text': 'A perfect day in a perfect place', 'orientation': 'vertical-lr'} + ) + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + tr = Transcription.objects.get(text='A perfect day in a perfect place') + self.assertDictEqual(response.json(), { + 'id': str(tr.id), + 'confidence': None, + 'score': None, + 'text': 'A perfect day in a perfect place', + 'orientation': 'vertical-lr', + 'worker_version_id': None, + }) + new_ts = Transcription.objects.get(text='A perfect day in a perfect place') + self.assertEqual(new_ts.orientation, TextOrientation.VerticalLeftToRight) + + def test_create_transcription_invalid_orientation(self): + """ + Specifying an invalid text-orientation causes an error + """ + self.client.force_login(self.user) + response = self.client.post( + reverse('api:transcription-create', kwargs={'pk': self.line.id}), + format='json', + data={'text': 'A perfect day in a perfect place', 'orientation': 'wiggly'} + ) + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + @override_settings(ARKINDEX_FEATURES={'search': False}) def test_create_transcription_no_search(self): self.client.force_login(self.user) @@ -151,6 +187,7 @@ class TestTranscriptionCreate(FixtureAPITestCase): 'confidence': .42, 'score': .42, 'text': 'NEKUDOTAYIM', + 'orientation': TextOrientation.HorizontalLeftToRight.value, 'worker_version_id': str(self.worker_version.id), }) @@ -176,6 +213,7 @@ class TestTranscriptionCreate(FixtureAPITestCase): 'confidence': .42, 'score': .42, 'text': 'NEKUDOTAYIM', + 'orientation': TextOrientation.HorizontalLeftToRight.value, 'worker_version_id': str(self.worker_version.id), }) diff --git a/arkindex/documents/tests/test_edit_transcriptions.py b/arkindex/documents/tests/test_edit_transcriptions.py index 0a99f94e998597de625906ea5e46c10166f35936..ec1bc298d5e6322fab3a2f643b1eccfb462d969c 100644 --- a/arkindex/documents/tests/test_edit_transcriptions.py +++ b/arkindex/documents/tests/test_edit_transcriptions.py @@ -3,7 +3,7 @@ from uuid import uuid4 from django.urls import reverse from rest_framework import status -from arkindex.documents.models import Corpus, Element, EntityType, Transcription +from arkindex.documents.models import Corpus, Element, EntityType, TextOrientation, Transcription from arkindex.project.tests import FixtureAPITestCase from arkindex.users.models import Role, User @@ -53,6 +53,7 @@ class TestEditTranscription(FixtureAPITestCase): 'confidence': None, 'score': None, 'text': 'A manual transcription', + 'orientation': TextOrientation.HorizontalLeftToRight.value, 'worker_version_id': None, }) @@ -144,6 +145,7 @@ class TestEditTranscription(FixtureAPITestCase): 'confidence': None, 'score': None, 'text': 'a knight was living lonely', + 'orientation': TextOrientation.HorizontalLeftToRight.value, 'worker_version_id': None, }) diff --git a/arkindex/documents/tests/test_transcriptions.py b/arkindex/documents/tests/test_transcriptions.py index dab8bfbf6b74a75b72cc80fbf61aa52d25e6c473..cd8ada88d2a8e75f3297cbe70bfaa50e97ae1100 100644 --- a/arkindex/documents/tests/test_transcriptions.py +++ b/arkindex/documents/tests/test_transcriptions.py @@ -2,7 +2,7 @@ from django.urls import reverse from rest_framework import status from arkindex.dataimport.models import WorkerVersion -from arkindex.documents.models import Corpus +from arkindex.documents.models import Corpus, TextOrientation from arkindex.project.tests import FixtureAPITestCase from arkindex.users.models import User @@ -52,6 +52,7 @@ class TestTranscriptions(FixtureAPITestCase): 'id': str(tr1.id), 'text': 'Lorem ipsum dolor sit amet', 'confidence': 1.0, + 'orientation': TextOrientation.HorizontalLeftToRight.value, 'score': 1.0, 'worker_version_id': str(self.worker_version_1.id), 'element': None, @@ -60,6 +61,7 @@ class TestTranscriptions(FixtureAPITestCase): 'id': str(tr2.id), 'text': 'something', 'confidence': 0.369, + 'orientation': TextOrientation.HorizontalLeftToRight.value, 'score': 0.369, 'worker_version_id': str(self.worker_version_2.id), 'element': None, @@ -129,6 +131,7 @@ class TestTranscriptions(FixtureAPITestCase): 'text': 'something', 'score': 0.369, 'confidence': 0.369, + 'orientation': TextOrientation.HorizontalLeftToRight.value, 'worker_version_id': str(self.worker_version_2.id), 'element': { 'id': str(self.page.id),