mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-05 22:14:45 +00:00
Prevent signal handlers from indexing the content of fields omitted from save(update_fields=[...]).
Fixes #3239
This commit is contained in:
parent
f9eca4a866
commit
2b1c9bc715
2 changed files with 20 additions and 1 deletions
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Reference in a new issue