From 35124023f0a207a6b127210b8661688325aa541e Mon Sep 17 00:00:00 2001 From: jonaswinkler Date: Mon, 30 Nov 2020 13:58:40 +0100 Subject: [PATCH] basic support for bulk editing. --- .../src/app/services/rest/document.service.ts | 8 ++++ src/documents/bulk_edit.py | 39 +++++++++++++++++++ src/documents/forms.py | 12 ------ src/documents/views.py | 9 +++-- 4 files changed, 52 insertions(+), 16 deletions(-) create mode 100644 src/documents/bulk_edit.py diff --git a/src-ui/src/app/services/rest/document.service.ts b/src-ui/src/app/services/rest/document.service.ts index cdea89914..07e69c87a 100644 --- a/src-ui/src/app/services/rest/document.service.ts +++ b/src-ui/src/app/services/rest/document.service.ts @@ -66,4 +66,12 @@ export class DocumentService extends AbstractPaperlessService return this.http.post(this.getResourceUrl(null, 'post_document'), formData) } + bulk_edit(ids: number[], method: string, args: any[]) { + return this.http.post(this.getResourceUrl(null, 'bulk_edit'), { + 'ids': ids, + 'method': method, + 'args': args + }) + } + } diff --git a/src/documents/bulk_edit.py b/src/documents/bulk_edit.py new file mode 100644 index 000000000..ef5d3f509 --- /dev/null +++ b/src/documents/bulk_edit.py @@ -0,0 +1,39 @@ +from documents.models import Document + + +methods_supported = [ + "set_correspondent" +] + + +def validate_data(data): + if 'ids' not in data or not isinstance(data['ids'], list): + raise ValueError() + ids = data['ids'] + if not all([isinstance(i, int) for i in ids]): + raise ValueError() + count = Document.objects.filter(pk__in=ids).count() + if not count == len(ids): + raise Document.DoesNotExist() + + if 'method' not in data or not isinstance(data['method'], str): + raise ValueError() + method = data['method'] + if method not in methods_supported: + raise ValueError() + + if 'args' not in data or not isinstance(data['args'], list): + raise ValueError() + parameters = data['args'] + + return ids, method, parameters + + +def perform_bulk_edit(data): + ids, method, args = validate_data(data) + + getattr(__file__, method)(ids, args) + + +def set_correspondent(ids, args): + print("WOW") diff --git a/src/documents/forms.py b/src/documents/forms.py index 0c73c3810..63dd307b2 100644 --- a/src/documents/forms.py +++ b/src/documents/forms.py @@ -1,5 +1,4 @@ import os -import re import tempfile from datetime import datetime from time import mktime @@ -12,17 +11,6 @@ from pathvalidate import validate_filename, ValidationError from documents.parsers import is_mime_type_supported -class BuldEditForm(forms.Form): - - def clean_ids(self): - ids = self.cleaned_data.get("ids") - if not re.match(r"[0-9,]+", ids): - raise forms.ValidationError("id list invalid") - id_list = [int(id) for id in ids.split(",")] - - - - class UploadForm(forms.Form): diff --git a/src/documents/views.py b/src/documents/views.py index ee45af267..95448ad62 100755 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -23,6 +23,7 @@ from rest_framework.viewsets import ( import documents.index as index from paperless.db import GnuPG from paperless.views import StandardPagination +from .bulk_edit import perform_bulk_edit from .filters import ( CorrespondentFilterSet, DocumentFilterSet, @@ -155,10 +156,10 @@ class DocumentViewSet(RetrieveModelMixin, @action(methods=['post'], detail=False) def bulk_edit(self, request, pk=None): - form = BulkEditForm(data=request.POST) - if not form.is_valid(): - return HttpResponseBadRequest("") - return Response({'asd': request.POST['content']}) + 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):