mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	add bulk editing methods
This commit is contained in:
		| @@ -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) | ||||
|  | ||||
|     return "OK" | ||||
|  | ||||
|  | ||||
| def set_correspondent(ids, args): | ||||
|     if not len(args) == 1: | ||||
|         raise ValueError() | ||||
| def add_tag(doc_ids, tag): | ||||
|  | ||||
|     if not args[0]: | ||||
|         correspondent = None | ||||
|     else: | ||||
|         if not isinstance(args[0], int): | ||||
|             raise ValueError() | ||||
|     qs = Document.objects.filter(Q(id__in=doc_ids) & ~Q(tags__id=tag)) | ||||
|     affected_docs = [doc.id for doc in qs] | ||||
|  | ||||
|         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 | ||||
| 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) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 jonaswinkler
					jonaswinkler