diff --git a/src/documents/admin.py b/src/documents/admin.py index 74a152c68..6ac949a45 100755 --- a/src/documents/admin.py +++ b/src/documents/admin.py @@ -2,7 +2,9 @@ from django.contrib import admin from django.contrib.auth.models import Group, User from django.utils.html import format_html, format_html_join from django.utils.safestring import mark_safe +from whoosh.writing import AsyncWriter +from . import index from .models import Correspondent, Document, DocumentType, Log, Tag @@ -71,6 +73,21 @@ class DocumentAdmin(admin.ModelAdmin): return obj.created.date().strftime("%Y-%m-%d") created_.short_description = "Created" + def delete_queryset(self, request, queryset): + ix = index.open_index() + with AsyncWriter(ix) as writer: + for o in queryset: + index.remove_document(writer, o) + super(DocumentAdmin, self).delete_queryset(request, queryset) + + def delete_model(self, request, obj): + index.remove_document_from_index(obj) + super(DocumentAdmin, self).delete_model(request, obj) + + def save_model(self, request, obj, form, change): + index.add_or_update_document(obj) + super(DocumentAdmin, self).save_model(request, obj, form, change) + @mark_safe def tags_(self, obj): r = "" diff --git a/src/documents/apps.py b/src/documents/apps.py index ca278e2e3..83e671d07 100644 --- a/src/documents/apps.py +++ b/src/documents/apps.py @@ -18,7 +18,8 @@ class DocumentsConfig(AppConfig): set_log_entry, set_correspondent, set_document_type, - set_tags + set_tags, + add_to_index ) @@ -29,6 +30,7 @@ class DocumentsConfig(AppConfig): document_consumption_finished.connect(set_document_type) document_consumption_finished.connect(set_tags) document_consumption_finished.connect(set_log_entry) + document_consumption_finished.connect(add_to_index) document_consumption_finished.connect(run_post_consume_script) post_delete.connect(cleanup_document_deletion) diff --git a/src/documents/index.py b/src/documents/index.py index e08d016d2..c55402e10 100644 --- a/src/documents/index.py +++ b/src/documents/index.py @@ -11,6 +11,9 @@ from documents.models import Document from paperless import settings +logger = logging.getLogger(__name__) + + class JsonFormatter(Formatter): def __init__(self): self.seen = {} @@ -68,6 +71,7 @@ def open_index(recreate=False): def update_document(writer, doc): + logger.debug("Indexing {}...".format(doc)) writer.update_document( id=doc.pk, title=doc.title, @@ -76,18 +80,21 @@ def update_document(writer, doc): ) -@receiver(models.signals.post_save, sender=Document) -def add_document_to_index(sender, instance, **kwargs): - ix = open_index() - with AsyncWriter(ix) as writer: - update_document(writer, instance) +def remove_document(writer, doc): + logger.debug("Removing {} from index...".format(doc)) + writer.delete_by_term('id', doc.pk) -@receiver(models.signals.post_delete, sender=Document) -def remove_document_from_index(sender, instance, **kwargs): +def add_or_update_document(document): ix = open_index() with AsyncWriter(ix) as writer: - writer.delete_by_term('id', instance.pk) + update_document(writer, document) + + +def remove_document_from_index(document): + ix = open_index() + with AsyncWriter(ix) as writer: + remove_document(writer, document) def autocomplete(ix, term, limit=10): diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.py index 231a39e0d..cee1e042b 100755 --- a/src/documents/signals/handlers.py +++ b/src/documents/signals/handlers.py @@ -166,3 +166,7 @@ def set_log_entry(sender, document=None, logging_group=None, **kwargs): user=user, object_repr=document.__str__(), ) + + +def add_to_index(sender, document, **kwargs): + index.add_or_update_document(document) diff --git a/src/documents/views.py b/src/documents/views.py index 63f0e0bb9..28ac7ae26 100755 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -99,6 +99,15 @@ class DocumentViewSet(RetrieveModelMixin, ordering_fields = ( "id", "title", "correspondent__name", "document_type__name", "created", "modified", "added", "archive_serial_number") + def update(self, request, *args, **kwargs): + response = super(DocumentViewSet, self).update(request, *args, **kwargs) + index.add_or_update_document(self.get_object()) + return response + + def destroy(self, request, *args, **kwargs): + index.remove_document_from_index(self.get_object()) + return super(DocumentViewSet, self).destroy(request, *args, **kwargs) + def file_response(self, pk, disposition): #TODO: this should not be necessary here. content_types = {