from django.conf import settings

from haystack.signals import RealtimeSignalProcessor
from haystack.utils import get_identifier
from haystack.exceptions import NotHandled

from celery_haystack.utils import get_update_task
from celery_haystack.indexes import CelerySearchIndex


class CelerySignalProcessor(RealtimeSignalProcessor):
    def __init__(self, *args, **kwargs):
        super(CelerySignalProcessor, self).__init__(*args, **kwargs)
        self.task_cls = get_update_task()

    def handle_save(self, sender, instance, **kwargs):
        return self.handle(sender, instance, action='update', **kwargs)

    def handle_delete(self, sender, instance, **kwargs):
        return self.handle(sender, instance, action='delete', **kwargs)

    def handle(self, sender, instance, action='update', **kwargs):
        """
        Given an individual model instance, determine if a backend
        handles the model, check if the index is Celery-enabled and
        enqueue task.
        """

        using_backends = self.connection_router.for_write(instance=instance)

        for using in using_backends:
            try:
                index = (self.connections[using].get_unified_index()
                         .get_index(sender))
            except NotHandled:
                pass  # Check next backend
            else:
                if isinstance(index, CelerySearchIndex):
                    self.enqueue(action, instance)
                    return  # Only enqueue instance once

    def enqueue(self, action, instance):
        """
        Shoves a message about how to update the index into the queue.

        This is a standardized string, resembling something like::

            ``notes.note.23``
            # ...or...
            ``weblog.entry.8``
        """
        return self.task_cls.delay(action, get_identifier(instance))

@@ -1283,12 +1283,16 @@
     
-pass
+continue
   # 
@@ -1302,32 +1302,33 @@
 ck next backend%0A
+%0A
             else
@@ -1327,30 +1327,8 @@
     
-else:%0A                
 if i
@@ -1364,24 +1364,126 @@
 Index):%0A    
+            if action == 'update' and not index.should_update(instance):%0A                    continue%0A
             
@@ -1517,20 +1517,16 @@
 stance)%0A
-    
         

Check should_update of index before enqueueing task