Skip to content

Commit 4bcc6a0

Browse files
committed
feat: support attribute for should_index
1 parent 027b224 commit 4bcc6a0

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ var/
2323
.installed.cfg
2424
*.egg
2525

26+
# IDE Files
27+
*.iml
28+
.idea/
29+
2630
# PyInstaller
2731
# Usually these files are written by a python script from a template
2832
# before PyInstaller builds the exe, so as to inject date/other infos into it.

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,18 @@ class ArticleIndex(AlgoliaIndex):
208208

209209
### Restrict indexing to a subset of your data
210210

211-
You can add constraints controlling if a record must be indexed or not. `should_index` should be a callable that returns a boolean.
211+
You can add constraints controlling if a record must be indexed or not. `should_index` should be a boolean or a callable that returns a boolean.
212212

213213
```python
214+
# with a boolean attribute
215+
class Article(models.model):
216+
name = models.CharField(max_length=64)
217+
is_indexable = True
218+
219+
class ArticleIndex(AlgoliaIndex):
220+
should_index = "is_indexable"
221+
222+
# with a callable returning a boolean
214223
class Contact(models.model):
215224
name = models.CharField(max_lenght=20)
216225
age = models.IntegerField()

src/models.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,12 @@ def __init__(self, model, client):
9898
if callable(attr):
9999
self.should_index = attr
100100
else:
101-
raise AlgoliaIndexError('{} should be a callable.'.format(
102-
self.should_index))
101+
should_index_attr_name = self.should_index
102+
if isinstance(attr, bool):
103+
self.should_index = lambda instance: getattr(instance, should_index_attr_name) is True
104+
else:
105+
raise AlgoliaIndexError('{} should be a callable or a boolean attribute.'.format(
106+
self.should_index))
103107
else:
104108
raise AlgoliaIndexError('{} is not an attribute of {}.'.format(
105109
self.should_index, model))
@@ -201,6 +205,10 @@ def update_obj_index(self, instance):
201205
self.__index.save_object(obj)
202206
logger.debug('UPDATE %s FROM %s', obj['objectID'], self.model)
203207

208+
def _should_index(self, instance):
209+
'''Return true if the object should be indexed.'''
210+
return self.should_index(instance) if self.should_index else True
211+
204212
def delete_obj_index(self, instance):
205213
'''Delete the object.'''
206214
objectID = self.__get_objectID(instance)

tests/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ class Example(models.Model):
99
lng = models.FloatField()
1010
category = []
1111
locations = []
12+
index_me = True
1213

1314
def location(self):
1415
return (self.lat, self.lng)
1516

1617
def geolocations(self):
1718
return self.locations
19+
20+
def has_name(self):
21+
return self.name is not None

tests/test_index.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,32 @@ class ExampleIndex(AlgoliaIndex):
167167

168168
with self.assertRaises(AlgoliaIndexError):
169169
index = ExampleIndex(Example, self.client)
170+
171+
def test_should_index_method(self):
172+
class ExampleIndex(AlgoliaIndex):
173+
fields = 'name'
174+
should_index = 'has_name'
175+
index = ExampleIndex(Example, self.client)
176+
obj = index._build_object(self.instance)
177+
self.assertTrue(index._should_index(self.instance),
178+
"We should index an instance when should_index(instance) returns True")
179+
180+
instance_should_not = Example(name=None)
181+
obj = index._build_object(instance_should_not)
182+
self.assertFalse(index._should_index(instance_should_not),
183+
"We should not index an instance when should_index(instance) returns False")
184+
185+
def test_should_index_attr(self):
186+
class ExampleIndex(AlgoliaIndex):
187+
fields = 'name'
188+
should_index = 'index_me'
189+
index = ExampleIndex(Example, self.client)
190+
obj = index._build_object(self.instance)
191+
self.assertTrue(index._should_index(self.instance),
192+
"We should index an instance when its should_index attr is True")
193+
194+
instance_should_not = Example()
195+
instance_should_not.index_me = False
196+
obj = index._build_object(instance_should_not)
197+
self.assertFalse(index._should_index(instance_should_not),
198+
"We should not index an instance when its should_index attr is False")

0 commit comments

Comments
 (0)