Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

add api method to get selection data

jonaswinkler320298e3ffb5
diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py
index f34176d8a..155030a68 100644
--- a/src/documents/serialisers.py
+++ b/src/documents/serialisers.py
@@ -393,3 +393,11 @@ class PostDocumentSerializer(serializers.Serializer):
return [tag.id for tag in tags]
else:
return None
+
+
+class SelectionDataSerializer(serializers.Serializer):
+
+ documents = serializers.ListField(
+ required=True,
+ child=serializers.IntegerField()
+ )
diff --git a/src/documents/views.py b/src/documents/views.py
index 8f6ec7f13..d4d7b7123 100755
--- a/src/documents/views.py
+++ b/src/documents/views.py
@@ -4,7 +4,7 @@ from datetime import datetime
from time import mktime
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.views.decorators.cache import cache_control
from django.views.generic import TemplateView
@@ -48,7 +48,7 @@ from .serialisers import (
DocumentTypeSerializer,
PostDocumentSerializer,
SavedViewSerializer,
- BulkEditSerializer
+ BulkEditSerializer, SelectionDataSerializer
)
@@ -372,6 +372,62 @@ class PostDocumentView(APIView):
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):
permission_classes = (IsAuthenticated,)
diff --git a/src/paperless/urls.py b/src/paperless/urls.py
index 831eb02b4..39e99a7a4 100755
--- a/src/paperless/urls.py
+++ b/src/paperless/urls.py
@@ -19,7 +19,8 @@ from documents.views import (
StatisticsView,
PostDocumentView,
SavedViewViewSet,
- BulkEditView
+ BulkEditView,
+ SelectionDataView
)
from paperless.views import FaviconView
@@ -53,10 +54,12 @@ urlpatterns = [
re_path(r"^documents/post_document/", PostDocumentView.as_view(),
name="post_document"),
-
re_path(r"^documents/bulk_edit/", BulkEditView.as_view(),
name="bulk_edit"),
+ re_path(r"^documents/selection_data/", SelectionDataView.as_view(),
+ name="selection_data"),
+
path('token/', views.obtain_auth_token)
] + api_router.urls)),