paperless-ngx/src/documents/bulk_edit.py

102 lines
2.8 KiB
Python
Raw Normal View History

import itertools
2020-12-11 14:27:54 +01:00
from django.db.models import Q
from django_q.tasks import async_task
from documents.models import Correspondent
from documents.models import Document
from documents.models import DocumentType
2020-11-30 13:58:40 +01:00
2020-12-11 14:27:54 +01:00
def set_correspondent(doc_ids, correspondent):
if correspondent:
correspondent = Correspondent.objects.get(id=correspondent)
2020-11-30 13:58:40 +01:00
2022-02-27 15:26:41 +01:00
qs = Document.objects.filter(Q(id__in=doc_ids) & ~Q(correspondent=correspondent))
2020-12-11 14:27:54 +01:00
affected_docs = [doc.id for doc in qs]
qs.update(correspondent=correspondent)
2020-11-30 13:58:40 +01:00
2022-02-27 15:26:41 +01:00
async_task("documents.tasks.bulk_update_documents", document_ids=affected_docs)
2020-11-30 13:58:40 +01:00
2020-12-11 14:27:54 +01:00
return "OK"
2020-11-30 13:58:40 +01:00
2020-12-11 14:27:54 +01:00
def set_document_type(doc_ids, document_type):
if document_type:
document_type = DocumentType.objects.get(id=document_type)
2020-11-30 13:58:40 +01:00
2022-02-27 15:26:41 +01:00
qs = Document.objects.filter(Q(id__in=doc_ids) & ~Q(document_type=document_type))
2020-12-11 14:27:54 +01:00
affected_docs = [doc.id for doc in qs]
qs.update(document_type=document_type)
2020-11-30 13:58:40 +01:00
2022-02-27 15:26:41 +01:00
async_task("documents.tasks.bulk_update_documents", document_ids=affected_docs)
2020-11-30 13:58:40 +01:00
2020-12-11 14:27:54 +01:00
return "OK"
2020-12-06 14:39:53 +01:00
2020-12-11 14:27:54 +01:00
def add_tag(doc_ids, tag):
2020-12-06 14:39:53 +01:00
2020-12-11 14:27:54 +01:00
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
2022-02-27 15:26:41 +01:00
DocumentTagRelationship.objects.bulk_create(
[DocumentTagRelationship(document_id=doc, tag_id=tag) for doc in affected_docs],
2022-02-27 15:26:41 +01:00
)
2020-12-11 14:27:54 +01:00
2022-02-27 15:26:41 +01:00
async_task("documents.tasks.bulk_update_documents", document_ids=affected_docs)
2020-12-11 14:27:54 +01:00
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),
2020-12-11 14:27:54 +01:00
).delete()
2022-02-27 15:26:41 +01:00
async_task("documents.tasks.bulk_update_documents", document_ids=affected_docs)
2020-12-11 14:27:54 +01:00
return "OK"
def modify_tags(doc_ids, add_tags, remove_tags):
qs = Document.objects.filter(id__in=doc_ids)
affected_docs = [doc.id for doc in qs]
DocumentTagRelationship = Document.tags.through
DocumentTagRelationship.objects.filter(
document_id__in=affected_docs,
tag_id__in=remove_tags,
).delete()
2022-02-27 15:26:41 +01:00
DocumentTagRelationship.objects.bulk_create(
[
DocumentTagRelationship(document_id=doc, tag_id=tag)
for (doc, tag) in itertools.product(affected_docs, add_tags)
],
ignore_conflicts=True,
)
2022-02-27 15:26:41 +01:00
async_task("documents.tasks.bulk_update_documents", document_ids=affected_docs)
return "OK"
2020-12-11 14:27:54 +01:00
def delete(doc_ids):
Document.objects.filter(id__in=doc_ids).delete()
2021-02-15 13:26:36 +01:00
from documents import index
with index.open_index_writer() as writer:
for id in doc_ids:
index.remove_document_by_id(writer, id)
2020-12-11 14:27:54 +01:00
return "OK"