diff --git a/streamfieldindex/indexer.py b/streamfieldindex/indexer.py index edd81e0..f5923cd 100644 --- a/streamfieldindex/indexer.py +++ b/streamfieldindex/indexer.py @@ -18,6 +18,10 @@ def index_all(page_query=None): index_page(page) +def clear_index(page): + IndexEntry.objects.filter(page__id=page.id).delete() + + def index_page(page): # Clear the index for this specific page diff --git a/streamfieldindex/wagtail_hooks.py b/streamfieldindex/wagtail_hooks.py index c4233a4..f7a820f 100644 --- a/streamfieldindex/wagtail_hooks.py +++ b/streamfieldindex/wagtail_hooks.py @@ -3,7 +3,7 @@ from wagtail.admin.admin_url_finder import ModelAdminURLFinder, register_admin_url_finder from wagtail.signals import page_published, page_unpublished, post_page_move -from .indexer import index_page +from .indexer import clear_index, index_page from .models import IndexEntry @@ -22,15 +22,16 @@ def index_after_copy_page(request, page): index_page(page) -def post_publish(sender, instance, **kwargs): - index_page(instance) +@hooks.register("before_unpublish_page") +def index_post_unpublished(request, page): + clear_index(page) -def index_post_page_move(sender, instance, **kwargs): +def post_publish(sender, instance, **kwargs): index_page(instance) -def index_post_unpublished(sender, instance, **kwargs): +def index_post_page_move(sender, instance, **kwargs): index_page(instance) @@ -47,5 +48,4 @@ def construct_edit_url(self, instance): register_admin_url_finder(IndexEntry, IndexEntryAdminURLFinder) page_published.connect(post_publish) -page_unpublished.connect(index_post_unpublished) post_page_move.connect(index_post_page_move)