diff gpp/custom_search.py @ 467:b910cc1460c8

Add the ability to conditionally add model instances to the search index on update. This is not perfect, as some instances should be deleted from the index if they are updated such that they should not be in the index anymore. Will think about and address that later.
author Brian Neal <bgneal@gmail.com>
date Sun, 24 Jul 2011 18:12:20 +0000
parents 524fd1b3919a
children
line wrap: on
line diff
--- a/gpp/custom_search.py	Sun Jul 24 02:35:17 2011 +0000
+++ b/gpp/custom_search.py	Sun Jul 24 18:12:20 2011 +0000
@@ -7,6 +7,7 @@
 
 from django import forms
 from haystack.forms import ModelSearchForm
+from queued_search.indexes import QueuedSearchIndex
 
 
 MODEL_CHOICES = (
@@ -34,3 +35,28 @@
         super(CustomModelSearchForm, self).__init__(*args, **kwargs)
         self.fields['models'] = forms.MultipleChoiceField(choices=MODEL_CHOICES,
                 label='', widget=forms.CheckboxSelectMultiple)
+
+
+class CondQueuedSearchIndex(QueuedSearchIndex):
+    """
+    This customized version of QueuedSearchIndex conditionally enqueues items
+    to be indexed by calling the can_index() method.
+
+    """
+    def can_index(self, instance):
+        """
+        The default is to index all instances. Override this method to
+        customize the behavior. This will be called on all update operations.
+
+        """
+        return True
+
+    def enqueue(self, action, instance):
+        """
+        This method enqueues the instance only if the can_index() method
+        returns True.
+
+        """
+        if (action == 'update' and self.can_index(instance) or
+                action == 'delete'):
+            super(CondQueuedSearchIndex, self).enqueue(action, instance)