Skip to content
Snippets Groups Projects
Commit 4ff58dfe authored by Valentin Rigal's avatar Valentin Rigal Committed by Erwan Rouchet
Browse files

Api transcriptions list

parent 44384873
No related branches found
No related tags found
No related merge requests found
from rest_framework.generics import \
ListAPIView, ListCreateAPIView, RetrieveAPIView, RetrieveUpdateAPIView, RetrieveUpdateDestroyAPIView
from arkindex.project.pagination import PageNumberPagination
from rest_framework.exceptions import ValidationError
from django.db.models import prefetch_related_objects
from arkindex.documents.serializers.light import PageLightSerializer
from arkindex.documents.serializers.elements import (
ElementSerializer, ElementLightSerializer, CorpusSerializer,
PageSerializer, ActSerializer, SurfaceSerializer,
)
from arkindex.documents.models import Element, ElementType, Page, Act, Corpus, Right
from arkindex.documents.serializers.ml import TranscriptionSerializer
from arkindex.documents.models import (
Element, ElementType, Page, Act, TranscriptionType, Transcription,
Corpus, Right,
)
from arkindex.project.mixins import CorpusACLMixin
from arkindex.project.permissions import IsVerifiedOrReadOnly
......@@ -177,3 +183,30 @@ class ActEdit(RetrieveAPIView):
act.parent_elements = Element.objects.get_ascending_paths(act.id)
act.child_elements = Element.objects.get_descending(act.id)
return act
class TranscriptionsPagination(PageNumberPagination):
page_size = 100
class ElementTranscriptions(ListAPIView):
"""
Get transcriptions corresponding to a specific element
Transcriptions can be filtered by type
"""
serializer_class = TranscriptionSerializer
pagination_class = TranscriptionsPagination
def get_queryset(self):
queryset = Transcription.objects.filter(
element_id=self.kwargs['pk'],
element__corpus__in=Corpus.objects.readable(self.request.user),
).prefetch_related('zone__image__server', 'source').order_by('id')
req_type = self.request.query_params.get('type')
if req_type:
try:
req_type = TranscriptionType(req_type)
queryset = queryset.filter(type=req_type)
except ValueError:
raise ValidationError({'type': 'Not a valid transcription type'})
return queryset
from django.urls import reverse
from rest_framework import status
from arkindex.documents.models import Element, ElementType
from arkindex.documents.models import Element, ElementType, DataSource, TranscriptionType
from arkindex.project.tests import FixtureAPITestCase
......@@ -10,6 +10,8 @@ class TestElementsAPI(FixtureAPITestCase):
def setUpTestData(cls):
super().setUpTestData()
cls.vol = Element.objects.get(name='Volume 1', type=ElementType.Volume, corpus=cls.corpus)
cls.page = Element.objects.filter(type=ElementType.Page, corpus=cls.corpus).first().page
cls.src = DataSource.objects.get(slug='test')
def test_get_element(self):
response = self.client.get(reverse('api:element-retrieve', kwargs={'pk': str(self.vol.id)}))
......@@ -42,3 +44,19 @@ class TestElementsAPI(FixtureAPITestCase):
self.assertEqual(response.json()['name'], 'Untitled (2)')
self.vol.refresh_from_db()
self.assertEqual(self.vol.name, 'Untitled (2)')
def test_get_transcriptions(self):
self.page.transcriptions.all().delete()
transcriptions = []
for i in range(10):
transcriptions.append(self.page.transcriptions.create(source_id=self.src.id, type=TranscriptionType.Word))
self.client.force_login(self.user)
response = self.client.get(reverse('api:element-transcriptions', kwargs={'pk': str(self.page.id)}))
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.json()['results']), 10)
self.assertEqual(len(transcriptions), 10)
# Wrong transcription type
url = reverse('api:element-transcriptions', kwargs={'pk': str(self.page.id)})
url += '?type=potato'
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
......@@ -2,8 +2,9 @@ from django.urls import path
from django.views.generic.base import RedirectView
from arkindex.documents.api.elements import (
ElementsList, RelatedElementsList, ElementRetrieve, ElementPages, ElementSurfaces,
CorpusList, CorpusRetrieve, CorpusPages, ActEdit, PageDetails, SurfaceDetails,
ElementsList, RelatedElementsList, ElementRetrieve, ElementPages, ElementSurfaces,
CorpusList, CorpusRetrieve, CorpusPages, ActEdit, PageDetails, SurfaceDetails,
ElementTranscriptions,
)
from arkindex.documents.api.search import PageSearch, ActSearch
from arkindex.documents.api.ml import ClassificationBulk, TranscriptionCreate, TranscriptionBulk
......@@ -31,6 +32,7 @@ api = [
path('elements/', ElementsList.as_view(), name='elements'),
path('element/<uuid:pk>/', ElementRetrieve.as_view(), name='element-retrieve'),
path('element/<uuid:pk>/history/', ElementHistory.as_view(), name='element-history'),
path('element/<uuid:pk>/transcriptions/', ElementTranscriptions.as_view(), name='element-transcriptions'),
path('page/<uuid:pk>/', PageDetails.as_view(), name='page-details'),
path('surface/<uuid:pk>/', SurfaceDetails.as_view(), name='surface-details'),
path('corpus/', CorpusList.as_view(), name='corpus'),
......
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