From 5369e0be037b0ea373d655b6f7ecd246efec792d Mon Sep 17 00:00:00 2001 From: jonaswinkler Date: Sun, 6 Dec 2020 14:39:53 +0100 Subject: [PATCH] more bulk edit --- src/documents/bulk_edit.py | 16 +++++++++++++--- src/documents/serialisers.py | 28 ++++++++++++++++++++++++++++ src/documents/views.py | 32 ++++++++++++++++++++++++-------- 3 files changed, 65 insertions(+), 11 deletions(-) diff --git a/src/documents/bulk_edit.py b/src/documents/bulk_edit.py index ef5d3f509..f80c55c58 100644 --- a/src/documents/bulk_edit.py +++ b/src/documents/bulk_edit.py @@ -1,5 +1,4 @@ -from documents.models import Document - +from documents.models import Document, Correspondent methods_supported = [ "set_correspondent" @@ -36,4 +35,15 @@ def perform_bulk_edit(data): def set_correspondent(ids, args): - print("WOW") + if not len(args) == 1: + raise ValueError() + + if not args[0]: + correspondent = None + else: + if not isinstance(args[0], int): + raise ValueError() + + correspondent = Correspondent.objects.get(args[0]) + + Document.objects.filter(id__in=ids).update(correspondent=correspondent) diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index c988b2137..a8da79cdd 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -113,6 +113,34 @@ class LogSerializer(serializers.ModelSerializer): ) +class BulkEditSerializer(serializers.Serializer): + + documents = serializers.PrimaryKeyRelatedField( + many=True, + label="Documents", + write_only=True, + queryset=Document.objects.all() + ) + + method = serializers.ChoiceField( + choices=[ + "set_correspondent", + "set_document_type", + "add_tag", + "remove_tag", + "delete" + ], + label="Method", + write_only=True, + ) + + parameters = serializers.DictField(allow_empty=True) + + def validate(self, attrs): + + return attrs + + class PostDocumentSerializer(serializers.Serializer): document = serializers.FileField( diff --git a/src/documents/views.py b/src/documents/views.py index 219cc61b7..88ceb2efd 100755 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -46,7 +46,8 @@ from .serialisers import ( LogSerializer, TagSerializer, DocumentTypeSerializer, - PostDocumentSerializer + PostDocumentSerializer, + BulkEditSerializer ) @@ -165,13 +166,6 @@ class DocumentViewSet(RetrieveModelMixin, disposition, filename) return response - @action(methods=['post'], detail=False) - def bulk_edit(self, request, pk=None): - try: - return Response(perform_bulk_edit(request.data)) - except Exception as e: - return HttpResponseBadRequest(str(e)) - @action(methods=['get'], detail=True) def metadata(self, request, pk=None): try: @@ -225,6 +219,28 @@ class LogViewSet(ReadOnlyModelViewSet): ordering_fields = ("created",) +class BulkEditView(APIView): + + permission_classes = (IsAuthenticated,) + serializer_class = BulkEditSerializer + parser_classes = (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, *args, **kwargs): + serializer = self.get_serializer(data=request.data) + serializer.is_valid(raise_exception=True) + + class PostDocumentView(APIView): permission_classes = (IsAuthenticated,)