From 2b1c9bc71548649ca3b0481982556e138c8d9691 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Fri, 13 Jan 2017 17:37:21 +0000 Subject: [PATCH] Prevent signal handlers from indexing the content of fields omitted from save(update_fields=[...]). Fixes #3239 --- wagtail/wagtailsearch/signal_handlers.py | 8 +++++++- wagtail/wagtailsearch/tests/test_index_functions.py | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/wagtail/wagtailsearch/signal_handlers.py b/wagtail/wagtailsearch/signal_handlers.py index e522bb289..559ef9262 100644 --- a/wagtail/wagtailsearch/signal_handlers.py +++ b/wagtail/wagtailsearch/signal_handlers.py @@ -5,7 +5,13 @@ from django.db.models.signals import post_delete, post_save from wagtail.wagtailsearch import index -def post_save_signal_handler(instance, **kwargs): +def post_save_signal_handler(instance, update_fields=None, **kwargs): + if update_fields is not None: + # fetch a fresh copy of instance from the database to ensure + # that we're not indexing any of the unsaved data contained in + # the fields that were not passed in update_fields + instance = type(instance).objects.get(pk=instance.pk) + index.insert_or_update_object(instance) diff --git a/wagtail/wagtailsearch/tests/test_index_functions.py b/wagtail/wagtailsearch/tests/test_index_functions.py index 9d1b9989b..eac8a5702 100644 --- a/wagtail/wagtailsearch/tests/test_index_functions.py +++ b/wagtail/wagtailsearch/tests/test_index_functions.py @@ -166,3 +166,16 @@ class TestSignalHandlers(TestCase, WagtailTestUtils): backend().reset_mock() obj.delete() backend().delete.assert_called_with(obj) + + def test_do_not_index_fields_omitted_from_update_fields(self, backend): + obj = models.SearchTest.objects.create(title="Test", content="This is the original content") + + backend().reset_mock() + obj.title = "Updated test" + obj.content = "This is the updated content" + obj.save(update_fields=['title']) + + backend().add.assert_called_once() + indexed_object = backend().add.call_args[0][0] + self.assertEqual(indexed_object.title, "Updated test") + self.assertEqual(indexed_object.content, "This is the original content")