Skip to content

Commit 97ee548

Browse files
committed
little optimization
1 parent 6533665 commit 97ee548

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/models.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,26 @@ def __init__(self, model, client):
8080
raise AlgoliaIndexError('{} is not an attribute of {}'.format(
8181
self.tags, model))
8282

83-
# Check geo_field
83+
# Check geo_field + get the callable
8484
if self.geo_field:
8585
if hasattr(model, self.geo_field):
8686
attr = getattr(model, self.geo_field)
87-
if not callable(attr):
87+
if callable(attr):
88+
self.geo_field = attr
89+
else:
8890
raise AlgoliaIndexError('{} should be a callable.'.format(
8991
self.geo_field))
9092
else:
9193
raise AlgoliaIndexError('{} is not an attribute of {}.'.format(
9294
self.geo_field, model))
9395

96+
# Check should_index + get the callable
9497
if self.should_index:
9598
if hasattr(model, self.should_index):
9699
attr = getattr(model, self.should_index)
97-
if not callable(attr):
100+
if callable(attr):
101+
self.should_index = attr
102+
else:
98103
raise AlgoliaIndexError('{} should be a callable.'.format(
99104
self.should_index))
100105
else:
@@ -150,10 +155,8 @@ def _build_object(self, instance):
150155
tmp[field] = attr
151156

152157
if self.geo_field:
153-
attr = getattr(instance, self.geo_field)
154-
if callable(attr):
155-
attr = attr()
156-
tmp['_geoloc'] = {'lat': attr[0], 'lng': attr[1]}
158+
loc = self.geo_field(instance)
159+
tmp['_geoloc'] = {'lat': loc[0], 'lng': loc[1]}
157160

158161
if self.tags:
159162
attr = getattr(instance, self.tags)
@@ -169,8 +172,7 @@ def _build_object(self, instance):
169172
def update_obj_index(self, instance):
170173
'''Update the object.'''
171174
if self.should_index:
172-
attr = getattr(instance, self.should_index)
173-
if not attr():
175+
if not self.should_index(instance):
174176
# Should not index, but since we don't now the state of the
175177
# instance, we need to send a DELETE request to ensure that if
176178
# the instance was previously indexed, it will be removed.
@@ -188,6 +190,7 @@ def delete_obj_index(self, instance):
188190
logger.debug('DELETE %s FROM %s', objectID, self.model)
189191

190192
def raw_search(self, query='', params={}):
193+
'''Return the raw JSON.'''
191194
return self.__index.search(query, params)
192195

193196
def set_settings(self):
@@ -214,8 +217,7 @@ def reindex_all(self, batch_size=1000):
214217
batch = []
215218
for instance in self.model.objects.all():
216219
if self.should_index:
217-
attr = getattr(instance, self.should_index)
218-
if not attr():
220+
if not self.should_index(instance):
219221
continue # should not index
220222

221223
batch.append(self._build_object(instance))

0 commit comments

Comments
 (0)