mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
I removed the model save/delete hooks for index updates since they were causing too much trouble with migrations
This commit is contained in:
parent
3378ac1487
commit
942fab7298
@ -2,7 +2,9 @@ from django.contrib import admin
|
|||||||
from django.contrib.auth.models import Group, User
|
from django.contrib.auth.models import Group, User
|
||||||
from django.utils.html import format_html, format_html_join
|
from django.utils.html import format_html, format_html_join
|
||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
|
from whoosh.writing import AsyncWriter
|
||||||
|
|
||||||
|
from . import index
|
||||||
from .models import Correspondent, Document, DocumentType, Log, Tag
|
from .models import Correspondent, Document, DocumentType, Log, Tag
|
||||||
|
|
||||||
|
|
||||||
@ -71,6 +73,21 @@ class DocumentAdmin(admin.ModelAdmin):
|
|||||||
return obj.created.date().strftime("%Y-%m-%d")
|
return obj.created.date().strftime("%Y-%m-%d")
|
||||||
created_.short_description = "Created"
|
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
|
@mark_safe
|
||||||
def tags_(self, obj):
|
def tags_(self, obj):
|
||||||
r = ""
|
r = ""
|
||||||
|
@ -18,7 +18,8 @@ class DocumentsConfig(AppConfig):
|
|||||||
set_log_entry,
|
set_log_entry,
|
||||||
set_correspondent,
|
set_correspondent,
|
||||||
set_document_type,
|
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_document_type)
|
||||||
document_consumption_finished.connect(set_tags)
|
document_consumption_finished.connect(set_tags)
|
||||||
document_consumption_finished.connect(set_log_entry)
|
document_consumption_finished.connect(set_log_entry)
|
||||||
|
document_consumption_finished.connect(add_to_index)
|
||||||
document_consumption_finished.connect(run_post_consume_script)
|
document_consumption_finished.connect(run_post_consume_script)
|
||||||
|
|
||||||
post_delete.connect(cleanup_document_deletion)
|
post_delete.connect(cleanup_document_deletion)
|
||||||
|
@ -11,6 +11,9 @@ from documents.models import Document
|
|||||||
from paperless import settings
|
from paperless import settings
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class JsonFormatter(Formatter):
|
class JsonFormatter(Formatter):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.seen = {}
|
self.seen = {}
|
||||||
@ -68,6 +71,7 @@ def open_index(recreate=False):
|
|||||||
|
|
||||||
|
|
||||||
def update_document(writer, doc):
|
def update_document(writer, doc):
|
||||||
|
logger.debug("Indexing {}...".format(doc))
|
||||||
writer.update_document(
|
writer.update_document(
|
||||||
id=doc.pk,
|
id=doc.pk,
|
||||||
title=doc.title,
|
title=doc.title,
|
||||||
@ -76,18 +80,21 @@ def update_document(writer, doc):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@receiver(models.signals.post_save, sender=Document)
|
def remove_document(writer, doc):
|
||||||
def add_document_to_index(sender, instance, **kwargs):
|
logger.debug("Removing {} from index...".format(doc))
|
||||||
ix = open_index()
|
writer.delete_by_term('id', doc.pk)
|
||||||
with AsyncWriter(ix) as writer:
|
|
||||||
update_document(writer, instance)
|
|
||||||
|
|
||||||
|
|
||||||
@receiver(models.signals.post_delete, sender=Document)
|
def add_or_update_document(document):
|
||||||
def remove_document_from_index(sender, instance, **kwargs):
|
|
||||||
ix = open_index()
|
ix = open_index()
|
||||||
with AsyncWriter(ix) as writer:
|
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):
|
def autocomplete(ix, term, limit=10):
|
||||||
|
@ -166,3 +166,7 @@ def set_log_entry(sender, document=None, logging_group=None, **kwargs):
|
|||||||
user=user,
|
user=user,
|
||||||
object_repr=document.__str__(),
|
object_repr=document.__str__(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def add_to_index(sender, document, **kwargs):
|
||||||
|
index.add_or_update_document(document)
|
||||||
|
@ -99,6 +99,15 @@ class DocumentViewSet(RetrieveModelMixin,
|
|||||||
ordering_fields = (
|
ordering_fields = (
|
||||||
"id", "title", "correspondent__name", "document_type__name", "created", "modified", "added", "archive_serial_number")
|
"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):
|
def file_response(self, pk, disposition):
|
||||||
#TODO: this should not be necessary here.
|
#TODO: this should not be necessary here.
|
||||||
content_types = {
|
content_types = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user