# HG changeset patch # User Brian Neal # Date 1311531140 0 # Node ID b910cc1460c888141d5dbcf5b52d969219c6fb10 # Parent c78c6e007e6162e4621286b9e93d2ed8dbfa32c1 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. diff -r c78c6e007e61 -r b910cc1460c8 gpp/bio/search_indexes.py --- 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) diff -r c78c6e007e61 -r b910cc1460c8 gpp/custom_search.py --- 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) diff -r c78c6e007e61 -r b910cc1460c8 gpp/downloads/search_indexes.py --- 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) diff -r c78c6e007e61 -r b910cc1460c8 gpp/forums/search_indexes.py --- 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) diff -r c78c6e007e61 -r b910cc1460c8 gpp/news/search_indexes.py --- 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') diff -r c78c6e007e61 -r b910cc1460c8 gpp/podcast/search_indexes.py --- 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') diff -r c78c6e007e61 -r b910cc1460c8 gpp/weblinks/search_indexes.py --- 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) diff -r c78c6e007e61 -r b910cc1460c8 gpp/ygroup/search_indexes.py --- 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')