kopia lustrzana https://github.com/wagtail/wagtail
Fix handling of models with UUID primary keys in search indexing
rodzic
b313098aa0
commit
f29b50d72f
|
@ -6,7 +6,7 @@ from .tasks import insert_or_update_object_task
|
|||
|
||||
def post_save_signal_handler(instance, **kwargs):
|
||||
insert_or_update_object_task.enqueue(
|
||||
instance._meta.app_label, instance._meta.model_name, instance.pk
|
||||
instance._meta.app_label, instance._meta.model_name, str(instance.pk)
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ from django.test import TestCase, override_settings
|
|||
from wagtail.models import Page
|
||||
from wagtail.search import index
|
||||
from wagtail.test.search import models
|
||||
from wagtail.test.testapp.models import SimplePage
|
||||
from wagtail.test.testapp.models import AdvertWithCustomUUIDPrimaryKey, SimplePage
|
||||
from wagtail.test.utils import WagtailTestUtils
|
||||
|
||||
|
||||
|
@ -166,6 +166,12 @@ class TestSignalHandlers(WagtailTestUtils, TestCase):
|
|||
)
|
||||
backend().add.assert_called_with(obj)
|
||||
|
||||
def test_index_on_create_with_uuid_primary_key(self, backend):
|
||||
backend().reset_mock()
|
||||
with self.captureOnCommitCallbacks(execute=True):
|
||||
obj = AdvertWithCustomUUIDPrimaryKey.objects.create(text="Test")
|
||||
backend().add.assert_called_with(obj)
|
||||
|
||||
def test_index_on_update(self, backend):
|
||||
obj = models.Book.objects.create(
|
||||
title="Test", publication_date=date(2017, 10, 18), number_of_pages=100
|
||||
|
@ -180,6 +186,18 @@ class TestSignalHandlers(WagtailTestUtils, TestCase):
|
|||
indexed_object = backend().add.call_args[0][0]
|
||||
self.assertEqual(indexed_object.title, "Updated test")
|
||||
|
||||
def test_index_on_update_with_uuid_primary_key(self, backend):
|
||||
obj = AdvertWithCustomUUIDPrimaryKey.objects.create(text="Test")
|
||||
|
||||
backend().reset_mock()
|
||||
obj.text = "Updated test"
|
||||
with self.captureOnCommitCallbacks(execute=True):
|
||||
obj.save()
|
||||
|
||||
self.assertEqual(backend().add.call_count, 1)
|
||||
indexed_object = backend().add.call_args[0][0]
|
||||
self.assertEqual(indexed_object.text, "Updated test")
|
||||
|
||||
def test_index_on_delete(self, backend):
|
||||
obj = models.Book.objects.create(
|
||||
title="Test", publication_date=date(2017, 10, 18), number_of_pages=100
|
||||
|
@ -190,6 +208,14 @@ class TestSignalHandlers(WagtailTestUtils, TestCase):
|
|||
obj.delete()
|
||||
backend().delete.assert_called_with(obj)
|
||||
|
||||
def test_index_on_delete_with_uuid_primary_key(self, backend):
|
||||
obj = AdvertWithCustomUUIDPrimaryKey.objects.create(text="Test")
|
||||
|
||||
backend().reset_mock()
|
||||
with self.captureOnCommitCallbacks(execute=True):
|
||||
obj.delete()
|
||||
backend().delete.assert_called_with(obj)
|
||||
|
||||
def test_do_not_index_fields_omitted_from_update_fields(self, backend):
|
||||
obj = models.Book.objects.create(
|
||||
title="Test", publication_date=date(2017, 10, 18), number_of_pages=100
|
||||
|
|
|
@ -1006,7 +1006,7 @@ class AdvertWithCustomPrimaryKey(ClusterableModel):
|
|||
register_snippet(AdvertWithCustomPrimaryKey)
|
||||
|
||||
|
||||
class AdvertWithCustomUUIDPrimaryKey(ClusterableModel):
|
||||
class AdvertWithCustomUUIDPrimaryKey(index.Indexed, ClusterableModel):
|
||||
advert_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
url = models.URLField(null=True, blank=True)
|
||||
text = models.CharField(max_length=255)
|
||||
|
@ -1018,6 +1018,10 @@ class AdvertWithCustomUUIDPrimaryKey(ClusterableModel):
|
|||
FieldPanel("page"),
|
||||
]
|
||||
|
||||
search_fields = [
|
||||
index.SearchField("text"),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return self.text
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue