basic support for bulk editing.

This commit is contained in:
jonaswinkler 2020-11-30 13:58:40 +01:00
parent fd4c9a1758
commit 35124023f0
4 changed files with 52 additions and 16 deletions

View File

@ -66,4 +66,12 @@ export class DocumentService extends AbstractPaperlessService<PaperlessDocument>
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
})
}
}

View File

@ -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")

View File

@ -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):

View File

@ -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):