mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-28 18:24:38 -05:00
basic support for bulk editing.
This commit is contained in:
39
src/documents/bulk_edit.py
Normal file
39
src/documents/bulk_edit.py
Normal 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")
|
@@ -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):
|
||||
|
||||
|
@@ -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):
|
||||
|
Reference in New Issue
Block a user