Skip to content

Commit 4c434bf

Browse files
committed
Merge branch 'develop' of https://github.com/Kronuz/celery-haystack into Kronuz-develop
Conflicts: celery_haystack/tasks.py
2 parents 19af705 + b842240 commit 4c434bf

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

celery_haystack/indexes.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,27 @@ def handle_model(self, model):
2525
# operation.
2626
def _setup_save(self, model=None):
2727
model = self.handle_model(model)
28-
signals.post_save.connect(self.enqueue_save, sender=model)
28+
signals.post_save.connect(self._enqueue_save, sender=model, dispatch_uid=CelerySearchIndex)
2929

3030
def _setup_delete(self, model=None):
3131
model = self.handle_model(model)
32-
signals.post_delete.connect(self.enqueue_delete, sender=model)
32+
signals.post_delete.connect(self._enqueue_delete, sender=model, dispatch_uid=CelerySearchIndex)
3333

3434
def _teardown_save(self, model=None):
3535
model = self.handle_model(model)
36-
signals.post_save.disconnect(self.enqueue_save, sender=model)
36+
signals.post_save.disconnect(self._enqueue_save, sender=model, dispatch_uid=CelerySearchIndex)
3737

3838
def _teardown_delete(self, model=None):
3939
model = self.handle_model(model)
40-
signals.post_delete.disconnect(self.enqueue_delete, sender=model)
40+
signals.post_delete.disconnect(self._enqueue_delete, sender=model, dispatch_uid=CelerySearchIndex)
41+
42+
def _enqueue_save(self, instance, **kwargs):
43+
if not getattr(instance, 'skip_indexing', False):
44+
self.enqueue_save(instance, **kwargs)
45+
46+
def _enqueue_delete(self, instance, **kwargs):
47+
if not getattr(instance, 'skip_indexing', False):
48+
self.enqueue_delete(instance, **kwargs)
4149

4250
def enqueue_save(self, instance, **kwargs):
4351
return self.enqueue('update', instance)

celery_haystack/tasks.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
from celery_haystack.conf import settings
77

88
try:
9-
from haystack import connections
10-
index_holder = connections['default'].get_unified_index()
9+
from haystack import connections, connection_router
1110
from haystack.exceptions import NotHandled as IndexNotFoundException
1211
legacy = False
1312
except ImportError:
@@ -75,10 +74,11 @@ def get_instance(self, model_class, pk, **kwargs):
7574
logger = self.get_logger(**kwargs)
7675
instance = None
7776
try:
78-
instance = model_class.objects.get(pk=pk)
77+
instance = model_class._default_manager.get(pk=int(pk))
7978
except model_class.DoesNotExist:
80-
logger.error("Couldn't load model instance "
81-
"with pk %s. Somehow it went missing?" % pk)
79+
logger.error("Couldn't load %s.%s.%s. Somehow it went missing?" %
80+
(model_class._meta.app_label.lower(),
81+
model_class._meta.object_name.lower(), pk))
8282
except model_class.MultipleObjectsReturned:
8383
logger.error("More than one object with pk %s. Oops?" % pk)
8484
return instance
@@ -88,6 +88,9 @@ def get_index(self, model_class, **kwargs):
8888
Fetch the model's registered ``SearchIndex`` in a standarized way.
8989
"""
9090
try:
91+
if not legacy:
92+
backend_alias = connection_router.for_write(**{'models': [model_class]})
93+
index_holder = connections[backend_alias].get_unified_index()
9194
return index_holder.get_index(model_class)
9295
except IndexNotFoundException:
9396
raise ImproperlyConfigured("Couldn't find a SearchIndex for %s." %
@@ -134,7 +137,6 @@ def run(self, action, identifier, **kwargs):
134137
(identifier, current_index_name))
135138
logger.debug(msg)
136139
return msg
137-
138140
elif action == 'update':
139141
# and the instance of the model class with the pk
140142
instance = self.get_instance(model_class, pk, **kwargs)

0 commit comments

Comments
 (0)