@@ -80,21 +80,26 @@ def __init__(self, model, client):
80
80
raise AlgoliaIndexError ('{} is not an attribute of {}' .format (
81
81
self .tags , model ))
82
82
83
- # Check geo_field
83
+ # Check geo_field + get the callable
84
84
if self .geo_field :
85
85
if hasattr (model , self .geo_field ):
86
86
attr = getattr (model , self .geo_field )
87
- if not callable (attr ):
87
+ if callable (attr ):
88
+ self .geo_field = attr
89
+ else :
88
90
raise AlgoliaIndexError ('{} should be a callable.' .format (
89
91
self .geo_field ))
90
92
else :
91
93
raise AlgoliaIndexError ('{} is not an attribute of {}.' .format (
92
94
self .geo_field , model ))
93
95
96
+ # Check should_index + get the callable
94
97
if self .should_index :
95
98
if hasattr (model , self .should_index ):
96
99
attr = getattr (model , self .should_index )
97
- if not callable (attr ):
100
+ if callable (attr ):
101
+ self .should_index = attr
102
+ else :
98
103
raise AlgoliaIndexError ('{} should be a callable.' .format (
99
104
self .should_index ))
100
105
else :
@@ -150,10 +155,8 @@ def _build_object(self, instance):
150
155
tmp [field ] = attr
151
156
152
157
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 ]}
157
160
158
161
if self .tags :
159
162
attr = getattr (instance , self .tags )
@@ -169,8 +172,7 @@ def _build_object(self, instance):
169
172
def update_obj_index (self , instance ):
170
173
'''Update the object.'''
171
174
if self .should_index :
172
- attr = getattr (instance , self .should_index )
173
- if not attr ():
175
+ if not self .should_index (instance ):
174
176
# Should not index, but since we don't now the state of the
175
177
# instance, we need to send a DELETE request to ensure that if
176
178
# the instance was previously indexed, it will be removed.
@@ -188,6 +190,7 @@ def delete_obj_index(self, instance):
188
190
logger .debug ('DELETE %s FROM %s' , objectID , self .model )
189
191
190
192
def raw_search (self , query = '' , params = {}):
193
+ '''Return the raw JSON.'''
191
194
return self .__index .search (query , params )
192
195
193
196
def set_settings (self ):
@@ -214,8 +217,7 @@ def reindex_all(self, batch_size=1000):
214
217
batch = []
215
218
for instance in self .model .objects .all ():
216
219
if self .should_index :
217
- attr = getattr (instance , self .should_index )
218
- if not attr ():
220
+ if not self .should_index (instance ):
219
221
continue # should not index
220
222
221
223
batch .append (self ._build_object (instance ))
0 commit comments