Skip to content

Commit da47117

Browse files
authored
Merge pull request #223 from m-vdb/mvdb/fix-reindex-all-should-index-not-callable
Fix reindex_all() and save_record() when should_index is not callable
2 parents 0f70028 + 1eac710 commit da47117

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

algoliasearch_django/models.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,12 @@ def save_record(self, instance, update_fields=None, **kwargs):
278278
For more information about partial_update_object:
279279
https://github.com/algolia/algoliasearch-client-python#update-an-existing-object-in-the-index
280280
"""
281-
if self.should_index:
282-
if not self.should_index(instance):
283-
# Should not index, but since we don't now the state of the
284-
# instance, we need to send a DELETE request to ensure that if
285-
# the instance was previously indexed, it will be removed.
286-
self.delete_record(instance)
287-
return
281+
if not self._should_index(instance):
282+
# Should not index, but since we don't now the state of the
283+
# instance, we need to send a DELETE request to ensure that if
284+
# the instance was previously indexed, it will be removed.
285+
self.delete_record(instance)
286+
return
288287

289288
try:
290289
if update_fields:
@@ -424,9 +423,8 @@ def reindex_all(self, batch_size=1000):
424423
qs = self.model.objects.all()
425424

426425
for instance in qs:
427-
if self.should_index:
428-
if not self.should_index(instance):
429-
continue # should not index
426+
if not self._should_index(instance):
427+
continue # should not index
430428

431429
batch.append(self.get_raw_record(instance))
432430
if len(batch) >= batch_size:

tests/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def permissions(self):
2525
class Website(models.Model):
2626
name = models.CharField(max_length=100)
2727
url = models.URLField()
28+
is_online = models.BooleanField(default=False)
2829

2930

3031
class Example(models.Model):

tests/test_index.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ class WebsiteIndex(AlgoliaIndex):
7272
index = WebsiteIndex(Website, self.client, settings.ALGOLIA)
7373
index.reindex_all()
7474

75+
def test_reindex_with_should_index_boolean(self):
76+
Website.objects.create(
77+
name='Algolia',
78+
url='https://algolia.com',
79+
is_online=True
80+
)
81+
index = AlgoliaIndex(Website, self.client, settings.ALGOLIA)
82+
class WebsiteIndex(AlgoliaIndex):
83+
settings = {
84+
'replicas': [
85+
index.index_name + '_name_asc',
86+
index.index_name + '_name_desc'
87+
]
88+
}
89+
should_index = 'is_online'
90+
91+
index = WebsiteIndex(Website, self.client, settings.ALGOLIA)
92+
index.reindex_all()
93+
7594
def test_custom_objectID(self):
7695
class UserIndex(AlgoliaIndex):
7796
custom_objectID = 'username'
@@ -382,3 +401,22 @@ class ExampleIndex(AlgoliaIndex):
382401
index = ExampleIndex(Example, self.client, settings.ALGOLIA)
383402
with self.assertRaises(AlgoliaIndexError, msg="We should raise when the should_index property is not boolean"):
384403
index._should_index(self.example)
404+
405+
def test_save_record_should_index_boolean(self):
406+
website = Website.objects.create(
407+
name='Algolia',
408+
url='https://algolia.com',
409+
is_online=True
410+
)
411+
index = AlgoliaIndex(Website, self.client, settings.ALGOLIA)
412+
class WebsiteIndex(AlgoliaIndex):
413+
settings = {
414+
'replicas': [
415+
index.index_name + '_name_asc',
416+
index.index_name + '_name_desc'
417+
]
418+
}
419+
should_index = 'is_online'
420+
421+
index = WebsiteIndex(Website, self.client, settings.ALGOLIA)
422+
index.save_record(website)

0 commit comments

Comments
 (0)