Prevent signal handlers from indexing the content of fields omitted from save(update_fields=[...]).

Fixes #3239
This commit is contained in:
Matt Westcott 2017-01-13 17:37:21 +00:00 committed by Karl Hobley
parent f9eca4a866
commit 2b1c9bc715
2 changed files with 20 additions and 1 deletions

View file

@ -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)

View file

@ -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")