Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

add bulk editing methods

jonaswinklerfbb3a069cd20
diff --git a/src/documents/bulk_edit.py b/src/documents/bulk_edit.py
index f80c55c58..1349f9d54 100644
--- a/src/documents/bulk_edit.py
+++ b/src/documents/bulk_edit.py
@@ -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 = [
- "set_correspondent"
-]
+from documents.models import Document, Correspondent, DocumentType
-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()
+def set_correspondent(doc_ids, correspondent):
+ if correspondent:
+ correspondent = Correspondent.objects.get(id=correspondent)
- if 'method' not in data or not isinstance(data['method'], str):
- raise ValueError()
- method = data['method']
- if method not in methods_supported:
- raise ValueError()
+ qs = Document.objects.filter(Q(id__in=doc_ids) & ~Q(correspondent=correspondent))
+ affected_docs = [doc.id for doc in qs]
+ qs.update(correspondent=correspondent)
- if 'args' not in data or not isinstance(data['args'], list):
- raise ValueError()
- parameters = data['args']
+ async_task("documents.tasks.bulk_rename_files", affected_docs)
- return ids, method, parameters
+ return "OK"
-def perform_bulk_edit(data):
- ids, method, args = validate_data(data)
+def set_document_type(doc_ids, document_type):
+ 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)
-def set_correspondent(ids, args):
- if not len(args) == 1:
- raise ValueError()
+ return "OK"
- if not args[0]:
- correspondent = None
- else:
- if not isinstance(args[0], int):
- raise ValueError()
- correspondent = Correspondent.objects.get(args[0])
+def add_tag(doc_ids, tag):
- Document.objects.filter(id__in=ids).update(correspondent=correspondent)
+ 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.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"
diff --git a/src/documents/tasks.py b/src/documents/tasks.py
index 8c9b00dd6..af4c91448 100644
--- a/src/documents/tasks.py
+++ b/src/documents/tasks.py
@@ -2,6 +2,7 @@ import logging
import tqdm
from django.conf import settings
+from django.db.models.signals import post_save
from whoosh.writing import AsyncWriter
from documents import index, sanity_checker
@@ -87,3 +88,9 @@ def sanity_check():
raise SanityFailedError(messages)
else:
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)