view 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
line wrap: on
line source
"""Haystack search index for the weblinks application."""
from haystack.indexes import *
from haystack import site
from custom_search import CondQueuedSearchIndex

from forums.models import Forum, Topic, Post


class TopicIndex(CondQueuedSearchIndex):
    text = CharField(document=True, use_template=True)
    author = CharField(model_attr='user')
    pub_date = DateTimeField(model_attr='creation_date')

    def get_queryset(self):
        return Topic.objects.filter(forum__in=Forum.objects.public_forum_ids())

    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(CondQueuedSearchIndex):
    text = CharField(document=True, use_template=True)
    author = CharField(model_attr='user')
    pub_date = DateTimeField(model_attr='creation_date')

    def get_queryset(self):
        return Post.objects.filter(
                topic__forum__in=Forum.objects.public_forum_ids())

    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)