mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
add bulk editing methods
This commit is contained in:
parent
51b1528fee
commit
fbb3a069cd
@ -1,49 +1,69 @@
|
|||||||
from documents.models import Document, Correspondent
|
from django.db.models import Q
|
||||||
|
from django_q.tasks import async_task
|
||||||
|
|
||||||
methods_supported = [
|
from documents.models import Document, Correspondent, DocumentType
|
||||||
"set_correspondent"
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def validate_data(data):
|
def set_correspondent(doc_ids, correspondent):
|
||||||
if 'ids' not in data or not isinstance(data['ids'], list):
|
if correspondent:
|
||||||
raise ValueError()
|
correspondent = Correspondent.objects.get(id=correspondent)
|
||||||
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):
|
qs = Document.objects.filter(Q(id__in=doc_ids) & ~Q(correspondent=correspondent))
|
||||||
raise ValueError()
|
affected_docs = [doc.id for doc in qs]
|
||||||
method = data['method']
|
qs.update(correspondent=correspondent)
|
||||||
if method not in methods_supported:
|
|
||||||
raise ValueError()
|
|
||||||
|
|
||||||
if 'args' not in data or not isinstance(data['args'], list):
|
async_task("documents.tasks.bulk_rename_files", affected_docs)
|
||||||
raise ValueError()
|
|
||||||
parameters = data['args']
|
|
||||||
|
|
||||||
return ids, method, parameters
|
return "OK"
|
||||||
|
|
||||||
|
|
||||||
def perform_bulk_edit(data):
|
def set_document_type(doc_ids, document_type):
|
||||||
ids, method, args = validate_data(data)
|
if document_type:
|
||||||
|
document_type = DocumentType.objects.get(id=document_type)
|
||||||
|
|
||||||
getattr(__file__, method)(ids, args)
|
qs = Document.objects.filter(Q(id__in=doc_ids) & ~Q(document_type=document_type))
|
||||||
|
affected_docs = [doc.id for doc in qs]
|
||||||
|
qs.update(document_type=document_type)
|
||||||
|
|
||||||
|
async_task("documents.tasks.bulk_rename_files", affected_docs)
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
|
||||||
|
|
||||||
def set_correspondent(ids, args):
|
def add_tag(doc_ids, tag):
|
||||||
if not len(args) == 1:
|
|
||||||
raise ValueError()
|
|
||||||
|
|
||||||
if not args[0]:
|
qs = Document.objects.filter(Q(id__in=doc_ids) & ~Q(tags__id=tag))
|
||||||
correspondent = None
|
affected_docs = [doc.id for doc in qs]
|
||||||
else:
|
|
||||||
if not isinstance(args[0], int):
|
|
||||||
raise ValueError()
|
|
||||||
|
|
||||||
correspondent = Correspondent.objects.get(args[0])
|
DocumentTagRelationship = Document.tags.through
|
||||||
|
|
||||||
Document.objects.filter(id__in=ids).update(correspondent=correspondent)
|
DocumentTagRelationship.objects.bulk_create([
|
||||||
|
DocumentTagRelationship(document_id=doc, tag_id=tag) for doc in affected_docs
|
||||||
|
])
|
||||||
|
|
||||||
|
async_task("documents.tasks.bulk_rename_files", affected_docs)
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
|
||||||
|
|
||||||
|
def remove_tag(doc_ids, tag):
|
||||||
|
|
||||||
|
qs = Document.objects.filter(Q(id__in=doc_ids) & Q(tags__id=tag))
|
||||||
|
affected_docs = [doc.id for doc in qs]
|
||||||
|
|
||||||
|
DocumentTagRelationship = Document.tags.through
|
||||||
|
|
||||||
|
DocumentTagRelationship.objects.filter(
|
||||||
|
Q(document_id__in=affected_docs) &
|
||||||
|
Q(tag_id=tag)
|
||||||
|
).delete()
|
||||||
|
|
||||||
|
async_task("documents.tasks.bulk_rename_files", affected_docs)
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
|
||||||
|
|
||||||
|
def delete(doc_ids):
|
||||||
|
Document.objects.filter(id__in=doc_ids).delete()
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
@ -2,6 +2,7 @@ import logging
|
|||||||
|
|
||||||
import tqdm
|
import tqdm
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.db.models.signals import post_save
|
||||||
from whoosh.writing import AsyncWriter
|
from whoosh.writing import AsyncWriter
|
||||||
|
|
||||||
from documents import index, sanity_checker
|
from documents import index, sanity_checker
|
||||||
@ -87,3 +88,9 @@ def sanity_check():
|
|||||||
raise SanityFailedError(messages)
|
raise SanityFailedError(messages)
|
||||||
else:
|
else:
|
||||||
return "No issues detected."
|
return "No issues detected."
|
||||||
|
|
||||||
|
|
||||||
|
def bulk_rename_files(ids):
|
||||||
|
qs = Document.objects.filter(id__in=ids)
|
||||||
|
for doc in qs:
|
||||||
|
post_save.send(Document, instance=doc, created=False)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user