changeset 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 (2011-07-24)
parents c78c6e007e61
children ad4b63fbc584
files gpp/bio/search_indexes.py gpp/custom_search.py gpp/downloads/search_indexes.py gpp/forums/search_indexes.py gpp/news/search_indexes.py gpp/podcast/search_indexes.py gpp/weblinks/search_indexes.py gpp/ygroup/search_indexes.py
diffstat 8 files changed, 54 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/bio/search_indexes.py	Sun Jul 24 02:35:17 2011 +0000
+++ b/gpp/bio/search_indexes.py	Sun Jul 24 18:12:20 2011 +0000
@@ -1,12 +1,12 @@
 """Haystack search index for the bio application."""
 from haystack.indexes import *
 from haystack import site
-from queued_search.indexes import QueuedSearchIndex
+from custom_search import CondQueuedSearchIndex
 
 from bio.models import UserProfile
 
 
-class UserProfileIndex(QueuedSearchIndex):
+class UserProfileIndex(CondQueuedSearchIndex):
     text = CharField(document=True, use_template=True)
     author = CharField(model_attr='user')
 
@@ -16,5 +16,8 @@
     def get_updated_field(self):
         return 'update_date'
 
+    def can_index(self, instance):
+        return instance.user.is_active
+
 
 site.register(UserProfile, UserProfileIndex)
--- 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)
--- a/gpp/downloads/search_indexes.py	Sun Jul 24 02:35:17 2011 +0000
+++ b/gpp/downloads/search_indexes.py	Sun Jul 24 18:12:20 2011 +0000
@@ -1,12 +1,12 @@
 """Haystack search index for the downloads application."""
 from haystack.indexes import *
 from haystack import site
-from queued_search.indexes import QueuedSearchIndex
+from custom_search import CondQueuedSearchIndex
 
 from downloads.models import Download
 
 
-class DownloadIndex(QueuedSearchIndex):
+class DownloadIndex(CondQueuedSearchIndex):
     text = CharField(document=True, use_template=True)
     author = CharField(model_attr='user')
     pub_date = DateTimeField(model_attr='date_added')
@@ -17,5 +17,7 @@
     def get_updated_field(self):
         return 'update_date'
 
+    def can_index(self, instance):
+        return instance.is_public
 
 site.register(Download, DownloadIndex)
--- a/gpp/forums/search_indexes.py	Sun Jul 24 02:35:17 2011 +0000
+++ b/gpp/forums/search_indexes.py	Sun Jul 24 18:12:20 2011 +0000
@@ -1,12 +1,12 @@
 """Haystack search index for the weblinks application."""
 from haystack.indexes import *
 from haystack import site
-from queued_search.indexes import QueuedSearchIndex
+from custom_search import CondQueuedSearchIndex
 
 from forums.models import Forum, Topic, Post
 
 
-class TopicIndex(QueuedSearchIndex):
+class TopicIndex(CondQueuedSearchIndex):
     text = CharField(document=True, use_template=True)
     author = CharField(model_attr='user')
     pub_date = DateTimeField(model_attr='creation_date')
@@ -17,8 +17,11 @@
     def get_updated_field(self):
         return 'update_date'
 
+    def can_index(self, instance):
+        return instance.forum.id in Forum.objects.public_forum_ids()
 
-class PostIndex(QueuedSearchIndex):
+
+class PostIndex(CondQueuedSearchIndex):
     text = CharField(document=True, use_template=True)
     author = CharField(model_attr='user')
     pub_date = DateTimeField(model_attr='creation_date')
@@ -30,6 +33,9 @@
     def get_updated_field(self):
         return 'update_date'
 
+    def can_index(self, instance):
+        return instance.topic.forum.id in Forum.objects.public_forum_ids()
+
 
 site.register(Topic, TopicIndex)
 site.register(Post, PostIndex)
--- a/gpp/news/search_indexes.py	Sun Jul 24 02:35:17 2011 +0000
+++ b/gpp/news/search_indexes.py	Sun Jul 24 18:12:20 2011 +0000
@@ -1,12 +1,12 @@
 """Haystack search index for the news application."""
 from haystack.indexes import *
 from haystack import site
-from queued_search.indexes import QueuedSearchIndex
+from custom_search import CondQueuedSearchIndex
 
 from news.models import Story
 
 
-class StoryIndex(QueuedSearchIndex):
+class StoryIndex(CondQueuedSearchIndex):
     text = CharField(document=True, use_template=True)
     author = CharField(model_attr='submitter')
     pub_date = DateTimeField(model_attr='date_submitted')
--- a/gpp/podcast/search_indexes.py	Sun Jul 24 02:35:17 2011 +0000
+++ b/gpp/podcast/search_indexes.py	Sun Jul 24 18:12:20 2011 +0000
@@ -1,12 +1,12 @@
 """Haystack search index for the news application."""
 from haystack.indexes import *
 from haystack import site
-from queued_search.indexes import QueuedSearchIndex
+from custom_search import CondQueuedSearchIndex
 
 from podcast.models import Item
 
 
-class ItemIndex(QueuedSearchIndex):
+class ItemIndex(CondQueuedSearchIndex):
     text = CharField(document=True, use_template=True)
     author = CharField(model_attr='author')
     pub_date = DateTimeField(model_attr='pubdate')
--- a/gpp/weblinks/search_indexes.py	Sun Jul 24 02:35:17 2011 +0000
+++ b/gpp/weblinks/search_indexes.py	Sun Jul 24 18:12:20 2011 +0000
@@ -1,12 +1,12 @@
 """Haystack search index for the weblinks application."""
 from haystack.indexes import *
 from haystack import site
-from queued_search.indexes import QueuedSearchIndex
+from custom_search import CondQueuedSearchIndex
 
 from weblinks.models import Link
 
 
-class LinkIndex(QueuedSearchIndex):
+class LinkIndex(CondQueuedSearchIndex):
     text = CharField(document=True, use_template=True)
     author = CharField(model_attr='user')
     pub_date = DateTimeField(model_attr='date_added')
@@ -17,5 +17,7 @@
     def get_updated_field(self):
         return 'update_date'
 
+    def can_index(self, instance):
+        return instance.is_public
 
 site.register(Link, LinkIndex)
--- a/gpp/ygroup/search_indexes.py	Sun Jul 24 02:35:17 2011 +0000
+++ b/gpp/ygroup/search_indexes.py	Sun Jul 24 18:12:20 2011 +0000
@@ -4,12 +4,12 @@
 """
 from haystack.indexes import *
 from haystack import site
-from queued_search.indexes import QueuedSearchIndex
+from custom_search import CondQueuedSearchIndex
 
 from ygroup.models import Post
 
 
-class PostIndex(QueuedSearchIndex):
+class PostIndex(CondQueuedSearchIndex):
     text = CharField(document=True, use_template=True)
     pub_date = DateTimeField(model_attr='creation_date')