Skip to content
Snippets Groups Projects
Commit 42b51a4e authored by ml bonhomme's avatar ml bonhomme :bee:
Browse files

orientation in bulk element transcriptions endpoint

parent 5fc53d1e
No related branches found
No related tags found
No related merge requests found
......@@ -266,6 +266,7 @@ class ElementTranscriptionsBulk(CreateAPIView):
worker_version=worker_version,
text=annotation['text'],
confidence=annotation['confidence'],
orientation=annotation['orientation']
)
annotation['id'] = transcription.id
transcriptions.append(transcription)
......
......@@ -6,7 +6,7 @@ from django.urls import reverse
from rest_framework import status
from arkindex.dataimport.models import WorkerVersion
from arkindex.documents.models import Corpus, Element, Transcription
from arkindex.documents.models import Corpus, Element, TextOrientation, Transcription
from arkindex.project.tests import FixtureAPITestCase
......@@ -39,8 +39,8 @@ class TestBulkElementTranscriptions(FixtureAPITestCase):
existing_element_ids = list(Element.objects.get_descending(self.page.id).values_list('id', flat=True))
transcriptions = [
([[13, 37], [133, 37], [133, 137], [13, 137], [13, 37]], 'Hello world !', 0.1337),
([[24, 42], [64, 42], [64, 142], [24, 142], [24, 42]], 'I <3 JavaScript', 0.42),
([[13, 37], [133, 37], [133, 137], [13, 137], [13, 37]], 'Hello world !', 0.1337, 'vertical-lr'),
([[24, 42], [64, 42], [64, 142], [24, 142], [24, 42]], 'I <3 JavaScript', 0.42, 'horizontal-lr'),
]
data = {
'element_type': 'text_line',
......@@ -48,8 +48,9 @@ class TestBulkElementTranscriptions(FixtureAPITestCase):
'transcriptions': [{
'polygon': poly,
'text': text,
'confidence': confidence
} for poly, text, confidence in transcriptions]
'confidence': confidence,
'orientation': orientation
} for poly, text, confidence, orientation in transcriptions]
}
self.client.force_login(self.user)
response = self.client.post(
......@@ -73,10 +74,10 @@ class TestBulkElementTranscriptions(FixtureAPITestCase):
]
)
self.assertCountEqual(
created_elts.values_list('transcriptions__text', 'transcriptions__worker_version'),
created_elts.values_list('transcriptions__text', 'transcriptions__worker_version', 'transcriptions__orientation'),
[
('Hello world !', self.worker_version.id),
('I <3 JavaScript', self.worker_version.id)
('Hello world !', self.worker_version.id, TextOrientation.VerticalLeftToRight),
('I <3 JavaScript', self.worker_version.id, TextOrientation.HorizontalLeftToRight)
]
)
......@@ -354,7 +355,7 @@ class TestBulkElementTranscriptions(FixtureAPITestCase):
second_elt = created_elts.get(transcriptions__text='stock')
first_tr = Transcription.objects.get(text="stuck")
second_tr = Transcription.objects.get(text="stock")
thrid_td = Transcription.objects.get(text="stack")
third_td = Transcription.objects.get(text="stack")
self.assertListEqual(response.json(), [{
'id': str(first_tr.id),
'element_id': str(first_elt.id),
......@@ -364,7 +365,7 @@ class TestBulkElementTranscriptions(FixtureAPITestCase):
'element_id': str(second_elt.id),
'created': True
}, {
'id': str(thrid_td.id),
'id': str(third_td.id),
'element_id': str(self.line.id),
'created': False
}])
......@@ -432,3 +433,65 @@ class TestBulkElementTranscriptions(FixtureAPITestCase):
('I <3 JavaScript', self.worker_version.id)
]
)
def test_bulk_transcriptions_default_orientation(self):
"""
No text orientation set > default (horizontal-lr) value
"""
existing_element_ids = list(Element.objects.get_descending(self.page.id).values_list('id', flat=True))
transcriptions = [
([[13, 37], [133, 37], [133, 137], [13, 137], [13, 37]], 'Hello world !', 0.1337),
([[24, 42], [64, 42], [64, 142], [24, 142], [24, 42]], 'I <3 JavaScript', 0.42),
]
data = {
'element_type': 'text_line',
'worker_version': str(self.worker_version.id),
'transcriptions': [{
'polygon': poly,
'text': text,
'confidence': confidence,
} for poly, text, confidence in transcriptions]
}
self.client.force_login(self.user)
response = self.client.post(
reverse('api:element-transcriptions-bulk', kwargs={'pk': self.page.id}),
format='json',
data=data
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
created_elts = Element.objects.get_descending(self.page.id).exclude(id__in=existing_element_ids)
self.assertCountEqual(
created_elts.values_list('transcriptions__orientation'),
[(TextOrientation.HorizontalLeftToRight,), (TextOrientation.HorizontalLeftToRight,)]
)
def test_bulk_transcriptions_invalid_orientation(self):
"""
Specifying an invalid text orientation throws an error
"""
transcriptions = [
([[13, 37], [133, 37], [133, 137], [13, 137], [13, 37]], 'Hello world !', 0.1337, 'wiggly'),
([[24, 42], [64, 42], [64, 142], [24, 142], [24, 42]], 'I <3 JavaScript', 0.42, 'timey-wimey'),
]
data = {
'element_type': 'text_line',
'worker_version': str(self.worker_version.id),
'transcriptions': [{
'polygon': poly,
'text': text,
'confidence': confidence,
'orientation': orientation,
} for poly, text, confidence, orientation in transcriptions]
}
self.client.force_login(self.user)
response = self.client.post(
reverse('api:element-transcriptions-bulk', kwargs={'pk': self.page.id}),
format='json',
data=data
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.json(), {'transcriptions': [
{'orientation': ['Value is not of type TextOrientation']},
{'orientation': ['Value is not of type TextOrientation']}
]}
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment