add api method to get selection data

This commit is contained in:
jonaswinkler 2020-12-27 12:43:05 +01:00
parent 0a1b810da7
commit 320298e3ff
3 changed files with 71 additions and 4 deletions

View File

@ -393,3 +393,11 @@ class PostDocumentSerializer(serializers.Serializer):
return [tag.id for tag in tags] return [tag.id for tag in tags]
else: else:
return None return None
class SelectionDataSerializer(serializers.Serializer):
documents = serializers.ListField(
required=True,
child=serializers.IntegerField()
)

View File

@ -4,7 +4,7 @@ from datetime import datetime
from time import mktime from time import mktime
from django.conf import settings from django.conf import settings
from django.db.models import Count, Max from django.db.models import Count, Max, Case, When, IntegerField
from django.http import HttpResponse, HttpResponseBadRequest, Http404 from django.http import HttpResponse, HttpResponseBadRequest, Http404
from django.views.decorators.cache import cache_control from django.views.decorators.cache import cache_control
from django.views.generic import TemplateView from django.views.generic import TemplateView
@ -48,7 +48,7 @@ from .serialisers import (
DocumentTypeSerializer, DocumentTypeSerializer,
PostDocumentSerializer, PostDocumentSerializer,
SavedViewSerializer, SavedViewSerializer,
BulkEditSerializer BulkEditSerializer, SelectionDataSerializer
) )
@ -372,6 +372,62 @@ class PostDocumentView(APIView):
return Response("OK") return Response("OK")
class SelectionDataView(APIView):
permission_classes = (IsAuthenticated,)
serializer_class = SelectionDataSerializer
parser_classes = (parsers.MultiPartParser, parsers.JSONParser)
def get_serializer_context(self):
return {
'request': self.request,
'format': self.format_kwarg,
'view': self
}
def get_serializer(self, *args, **kwargs):
kwargs['context'] = self.get_serializer_context()
return self.serializer_class(*args, **kwargs)
def post(self, request, format=None):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
ids = serializer.validated_data.get('documents')
correspondents = Correspondent.objects.annotate(dcount=Count(Case(
When(documents__id__in=ids, then=1),
output_field=IntegerField()
)))
tags = Tag.objects.annotate(dcount=Count(Case(
When(documents__id__in=ids, then=1),
output_field=IntegerField()
)))
types = DocumentType.objects.annotate(dcount=Count(Case(
When(documents__id__in=ids, then=1),
output_field=IntegerField()
)))
r = Response({
"selected_correspondents": [{
"id": t.id,
"dcount": t.dcount
} for t in correspondents],
"selected_tags": [{
"id": t.id,
"dcount": t.dcount
} for t in tags],
"selected_types": [{
"id": t.id,
"dcount": t.dcount
} for t in types]
})
return r
class SearchView(APIView): class SearchView(APIView):
permission_classes = (IsAuthenticated,) permission_classes = (IsAuthenticated,)

View File

@ -19,7 +19,8 @@ from documents.views import (
StatisticsView, StatisticsView,
PostDocumentView, PostDocumentView,
SavedViewViewSet, SavedViewViewSet,
BulkEditView BulkEditView,
SelectionDataView
) )
from paperless.views import FaviconView from paperless.views import FaviconView
@ -53,10 +54,12 @@ urlpatterns = [
re_path(r"^documents/post_document/", PostDocumentView.as_view(), re_path(r"^documents/post_document/", PostDocumentView.as_view(),
name="post_document"), name="post_document"),
re_path(r"^documents/bulk_edit/", BulkEditView.as_view(), re_path(r"^documents/bulk_edit/", BulkEditView.as_view(),
name="bulk_edit"), name="bulk_edit"),
re_path(r"^documents/selection_data/", SelectionDataView.as_view(),
name="selection_data"),
path('token/', views.obtain_auth_token) path('token/', views.obtain_auth_token)
] + api_router.urls)), ] + api_router.urls)),