1- from django .conf import settings
21from django .core .exceptions import ImproperlyConfigured
32from django .core .management import call_command
43from django .db .models .loading import get_model
54
65from celery .task import Task
6+ from celery_haystack .conf import settings
77
88try :
99 from haystack import connections
@@ -88,10 +88,8 @@ def get_index(self, model_class, **kwargs):
8888 logger .error ("Couldn't find a SearchIndex for %s." % model_class )
8989 return None
9090
91- def get_handler_options (self , instance , ** kwargs ):
92- options = {
93- 'instance' : instance ,
94- }
91+ def get_handler_options (self , ** kwargs ):
92+ options = {}
9593 if legacy :
9694 options ['using' ] = self .using
9795 return options
@@ -111,31 +109,42 @@ def run(self, action, identifier, **kwargs):
111109
112110 # Then get the model class for the object path
113111 model_class = self .get_model_class (object_path , ** kwargs )
114- # and the instance of the model class with the pk
115- instance = self .get_instance (model_class , pk , ** kwargs )
116- if instance is None :
117- logger .debug ("Didn't update index for '%s'" % identifier )
112+ current_index = self .get_index (model_class , ** kwargs )
113+
114+ if action == 'delete' :
115+ # If the object is gone, we'll use just the identifier against the
116+ # index.
117+ try :
118+ handler_options = self .get_handler_options (** kwargs )
119+ current_index .remove_object (identifier , ** handler_options )
120+ except Exception , exc :
121+ logger .error (exc )
122+ self .retry ([action , identifier ], kwargs , exc = exc )
123+ else :
124+ logger .debug ("Deleted '%s' from index" % identifier )
118125 return
119126
120- # Call the appropriate handler of the current index and
121- # handle exception if neccessary
122- logger .debug ("Indexing '%s'." % instance )
123- try :
124- current_index = self .get_index (model_class , ** kwargs )
125- handlers = {
126- 'update' : current_index .update_object ,
127- 'delete' : current_index .remove_object ,
128- }
129- handler_options = self .get_handler_options (instance , ** kwargs )
130- handlers [action ](** handler_options )
131- except KeyError , exc :
127+ elif action == 'update' :
128+ # and the instance of the model class with the pk
129+ instance = self .get_instance (model_class , pk , ** kwargs )
130+ if instance is None :
131+ logger .debug ("Didn't update index for '%s'" % identifier )
132+ return
133+
134+ # Call the appropriate handler of the current index and
135+ # handle exception if neccessary
136+ logger .debug ("Indexing '%s'." % instance )
137+ try :
138+ handler_options = self .get_handler_options (** kwargs )
139+ current_index .update_object (instance , ** handler_options )
140+ except Exception , exc :
141+ logger .error (exc )
142+ self .retry ([action , identifier ], kwargs , exc = exc )
143+ else :
144+ logger .debug ("Updated index with '%s'" % instance )
145+ else :
132146 logger .error ("Unrecognized action '%s'. Moving on..." % action )
133147 self .retry ([action , identifier ], kwargs , exc = exc )
134- except Exception , exc :
135- logger .error (exc )
136- self .retry ([action , identifier ], kwargs , exc = exc )
137- else :
138- logger .debug ("Updated index with '%s'" % instance )
139148
140149
141150class CeleryHaystackUpdateIndex (Task ):
0 commit comments