annotate gpp/forums/search_indexes.py @ 469:3b30286adba5

Smarter search index updating for forums. This work is for #227.
author Brian Neal <bgneal@gmail.com>
date Wed, 17 Aug 2011 01:02:08 +0000
parents b910cc1460c8
children d9b6c4ec1977
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@469 4 from custom_search.indexes import CondQueuedSearchIndex
bgneal@222 5
bgneal@414 6 from forums.models import Forum, Topic, Post
bgneal@469 7 from forums.signals import topic_content_update, post_content_update
bgneal@414 8
bgneal@414 9
bgneal@467 10 class TopicIndex(CondQueuedSearchIndex):
bgneal@414 11 text = CharField(document=True, use_template=True)
bgneal@414 12 author = CharField(model_attr='user')
bgneal@414 13 pub_date = DateTimeField(model_attr='creation_date')
bgneal@414 14
bgneal@414 15 def get_queryset(self):
bgneal@414 16 return Topic.objects.filter(forum__in=Forum.objects.public_forum_ids())
bgneal@414 17
bgneal@414 18 def get_updated_field(self):
bgneal@414 19 return 'update_date'
bgneal@222 20
bgneal@469 21 def _setup_save(self, model):
bgneal@469 22 topic_content_update.connect(self.enqueue_save)
bgneal@469 23
bgneal@469 24 def _teardown_save(self, model):
bgneal@469 25 topic_content_update.disconnect(self.enqueue_save)
bgneal@469 26
bgneal@467 27 def can_index(self, instance):
bgneal@467 28 return instance.forum.id in Forum.objects.public_forum_ids()
bgneal@222 29
bgneal@467 30
bgneal@467 31 class PostIndex(CondQueuedSearchIndex):
bgneal@222 32 text = CharField(document=True, use_template=True)
bgneal@222 33 author = CharField(model_attr='user')
bgneal@222 34 pub_date = DateTimeField(model_attr='creation_date')
bgneal@222 35
bgneal@222 36 def get_queryset(self):
bgneal@222 37 return Post.objects.filter(
bgneal@414 38 topic__forum__in=Forum.objects.public_forum_ids())
bgneal@222 39
bgneal@277 40 def get_updated_field(self):
bgneal@277 41 return 'update_date'
bgneal@277 42
bgneal@469 43 def _setup_save(self, model):
bgneal@469 44 post_content_update.connect(self.enqueue_save)
bgneal@469 45
bgneal@469 46 def _teardown_save(self, model):
bgneal@469 47 post_content_update.disconnect(self.enqueue_save)
bgneal@469 48
bgneal@467 49 def can_index(self, instance):
bgneal@467 50 return instance.topic.forum.id in Forum.objects.public_forum_ids()
bgneal@467 51
bgneal@222 52
bgneal@414 53 site.register(Topic, TopicIndex)
bgneal@222 54 site.register(Post, PostIndex)