@@ -57,39 +57,34 @@ def get_model_class(self, object_path, **kwargs):
57
57
model_class = get_model (app_name , classname )
58
58
59
59
if model_class is None :
60
- logger = self .get_logger (** kwargs )
61
- logger .error ("Could not load model "
62
- "from '%s'. Moving on..." % object_path )
63
- return None
64
-
60
+ raise ImproperlyConfigured ("Could not load model '%s'." %
61
+ object_path )
65
62
return model_class
66
63
67
64
def get_instance (self , model_class , pk , ** kwargs ):
68
65
"""
69
66
Fetch the instance in a standarized way.
70
67
"""
71
68
logger = self .get_logger (** kwargs )
69
+ instance = None
72
70
try :
73
71
instance = model_class .objects .get (pk = pk )
74
72
except model_class .DoesNotExist :
75
73
logger .error ("Couldn't load model instance "
76
- "with pk #%s. Somehow it went missing?" % pk )
77
- return None
74
+ "with pk %s. Somehow it went missing?" % pk )
78
75
except model_class .MultipleObjectsReturned :
79
- logger .error ("More than one object with pk #%s. Oops?" % pk )
80
- return None
81
-
76
+ logger .error ("More than one object with pk %s. Oops?" % pk )
82
77
return instance
83
78
84
79
def get_index (self , model_class , ** kwargs ):
85
80
"""
86
81
Fetch the model's registered ``SearchIndex`` in a standarized way.
87
82
"""
88
- logger = self .get_logger (** kwargs )
89
83
try :
90
84
return index_holder .get_index (model_class )
91
85
except IndexNotFoundException :
92
- logger .error ("Couldn't find a SearchIndex for %s." % model_class )
86
+ raise ImproperlyConfigured ("Couldn't find a SearchIndex for %s." %
87
+ model_class )
93
88
return None
94
89
95
90
def get_handler_options (self , ** kwargs ):
@@ -108,8 +103,9 @@ def run(self, action, identifier, **kwargs):
108
103
# First get the object path and pk (e.g. ('notes.note', 23))
109
104
object_path , pk = self .split_identifier (identifier , ** kwargs )
110
105
if object_path is None or pk is None :
111
- logger .error ("Skipping." )
112
- return
106
+ msg = "Couldn't handle object with identifier %s" % identifier
107
+ logger .error (msg )
108
+ raise ValueError (msg )
113
109
114
110
# Then get the model class for the object path
115
111
model_class = self .get_model_class (object_path , ** kwargs )
@@ -123,32 +119,37 @@ def run(self, action, identifier, **kwargs):
123
119
current_index .remove_object (identifier , ** handler_options )
124
120
except Exception , exc :
125
121
logger .error (exc )
126
- self .retry ([ action , identifier ], kwargs , exc = exc )
122
+ self .retry (exc = exc )
127
123
else :
128
- logger .debug ("Deleted '%s' from index" % identifier )
129
- return
124
+ msg = ("Deleted '%s' from index %s" %
125
+ (identifier , current_index ))
126
+ logger .debug (msg )
127
+ return msg
130
128
131
129
elif action == 'update' :
132
130
# and the instance of the model class with the pk
133
131
instance = self .get_instance (model_class , pk , ** kwargs )
134
132
if instance is None :
135
- logger .debug ("Didn't update index for '%s'" % identifier )
136
- return
133
+ logger .debug ("Didn't update index %s for '%s'" %
134
+ (current_index , identifier ))
135
+ raise ValueError ("Couldn't load object '%s'" % identifier )
137
136
138
137
# Call the appropriate handler of the current index and
139
138
# handle exception if neccessary
140
- logger .debug ("Indexing '%s'." % instance )
141
139
try :
142
140
handler_options = self .get_handler_options (** kwargs )
143
141
current_index .update_object (instance , ** handler_options )
144
142
except Exception , exc :
145
143
logger .error (exc )
146
- self .retry ([ action , identifier ], kwargs , exc = exc )
144
+ self .retry (exc = exc )
147
145
else :
148
- logger .debug ("Updated index with '%s'" % instance )
146
+ msg = ("Updated index %s with '%s'" %
147
+ (current_index , instance ))
148
+ logger .debug (msg )
149
+ return msg
149
150
else :
150
151
logger .error ("Unrecognized action '%s'. Moving on..." % action )
151
- self . retry ([ action , identifier ], kwargs , exc = exc )
152
+ raise ValueError ( "Unrecognized action %s" % action )
152
153
153
154
154
155
class CeleryHaystackUpdateIndex (Task ):
0 commit comments