Skip to content
Snippets Groups Projects
Commit 73349120 authored by Erwan Rouchet's avatar Erwan Rouchet
Browse files

Optimise get_ascendings usage

parent ca48841c
No related branches found
No related tags found
1 merge request!18Transcription search
......@@ -48,25 +48,24 @@ class DocumentPages(ListAPIView):
return Page.objects.get_descending(self.kwargs['pk'])
class SearchResultSetPagination(PageNumberPagination):
page_size = 10
page_size_query_param = 'page_size'
max_page_size = 20
class TranscriptionSearch(ListAPIView):
"""
Search and list transcriptions, using pagination
"""
serializer_class = SearchResultSerializer
pagination_class = SearchResultSetPagination
@lru_cache(maxsize=10)
def get_queryset(self):
query = self.request.query_params.get('q')
if query:
return search_transcriptions(query)
return None
if not query:
return
return search_transcriptions(query)
def add_parents(self, results):
ids = [trans.id for trans in results]
all_parents = Document.objects.get_ascendings(*ids)
for trans in results:
trans.parent_docs = all_parents[trans.id]
return results
def list(self, request):
queryset = self.get_queryset()
......@@ -76,10 +75,10 @@ class TranscriptionSearch(ListAPIView):
# Handle pagination, if activated
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
serializer = self.get_serializer(self.add_parents(page), many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
serializer = self.get_serializer(self.add_parents(queryset), many=True)
return Response(serializer.data)
......
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