annotate gpp/forums/search_indexes.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 b1f939b1fb01
children 3b30286adba5
rev   line source
bgneal@222 1 """Haystack search index for the weblinks application."""
bgneal@222 2 from haystack.indexes import *
bgneal@222 3 from haystack import site
bgneal@467 4 from custom_search import CondQueuedSearchIndex
bgneal@222 5
bgneal@414 6 from forums.models import Forum, Topic, Post
bgneal@414 7
bgneal@414 8
bgneal@467 9 class TopicIndex(CondQueuedSearchIndex):
bgneal@414 10 text = CharField(document=True, use_template=True)
bgneal@414 11 author = CharField(model_attr='user')
bgneal@414 12 pub_date = DateTimeField(model_attr='creation_date')
bgneal@414 13
bgneal@414 14 def get_queryset(self):
bgneal@414 15 return Topic.objects.filter(forum__in=Forum.objects.public_forum_ids())
bgneal@414 16
bgneal@414 17 def get_updated_field(self):
bgneal@414 18 return 'update_date'
bgneal@222 19
bgneal@467 20 def can_index(self, instance):
bgneal@467 21 return instance.forum.id in Forum.objects.public_forum_ids()
bgneal@222 22
bgneal@467 23
bgneal@467 24 class PostIndex(CondQueuedSearchIndex):
bgneal@222 25 text = CharField(document=True, use_template=True)
bgneal@222 26 author = CharField(model_attr='user')
bgneal@222 27 pub_date = DateTimeField(model_attr='creation_date')
bgneal@222 28
bgneal@222 29 def get_queryset(self):
bgneal@222 30 return Post.objects.filter(
bgneal@414 31 topic__forum__in=Forum.objects.public_forum_ids())
bgneal@222 32
bgneal@277 33 def get_updated_field(self):
bgneal@277 34 return 'update_date'
bgneal@277 35
bgneal@467 36 def can_index(self, instance):
bgneal@467 37 return instance.topic.forum.id in Forum.objects.public_forum_ids()
bgneal@467 38
bgneal@222 39
bgneal@414 40 site.register(Topic, TopicIndex)
bgneal@222 41 site.register(Post, PostIndex)