annotate gpp/custom_search.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 524fd1b3919a
children
rev   line source
bgneal@415 1 """
bgneal@415 2 This module contains custom code to tailor the Haystack search application to
bgneal@415 3 our needs.
bgneal@415 4
bgneal@415 5 """
bgneal@415 6 import urllib
bgneal@415 7
bgneal@415 8 from django import forms
bgneal@415 9 from haystack.forms import ModelSearchForm
bgneal@467 10 from queued_search.indexes import QueuedSearchIndex
bgneal@415 11
bgneal@415 12
bgneal@415 13 MODEL_CHOICES = (
bgneal@415 14 ('forums.topic', 'Forum Topics'),
bgneal@415 15 ('forums.post', 'Forum Posts'),
bgneal@415 16 ('news.story', 'News Stories'),
bgneal@415 17 ('bio.userprofile', 'User Profiles'),
bgneal@415 18 ('weblinks.link', 'Links'),
bgneal@415 19 ('downloads.download', 'Downloads'),
bgneal@415 20 ('podcast.item', 'Podcasts'),
bgneal@415 21 ('ygroup.post', 'Yahoo Group Archives'),
bgneal@415 22 )
bgneal@415 23
bgneal@415 24
bgneal@415 25 class CustomModelSearchForm(ModelSearchForm):
bgneal@415 26 """
bgneal@415 27 This customized ModelSearchForm allows us to explictly label and order
bgneal@415 28 the model choices.
bgneal@415 29
bgneal@415 30 """
bgneal@415 31 q = forms.CharField(required=False, label='',
bgneal@415 32 widget=forms.TextInput(attrs={'class': 'text', 'size': 48}))
bgneal@415 33
bgneal@415 34 def __init__(self, *args, **kwargs):
bgneal@415 35 super(CustomModelSearchForm, self).__init__(*args, **kwargs)
bgneal@415 36 self.fields['models'] = forms.MultipleChoiceField(choices=MODEL_CHOICES,
bgneal@415 37 label='', widget=forms.CheckboxSelectMultiple)
bgneal@467 38
bgneal@467 39
bgneal@467 40 class CondQueuedSearchIndex(QueuedSearchIndex):
bgneal@467 41 """
bgneal@467 42 This customized version of QueuedSearchIndex conditionally enqueues items
bgneal@467 43 to be indexed by calling the can_index() method.
bgneal@467 44
bgneal@467 45 """
bgneal@467 46 def can_index(self, instance):
bgneal@467 47 """
bgneal@467 48 The default is to index all instances. Override this method to
bgneal@467 49 customize the behavior. This will be called on all update operations.
bgneal@467 50
bgneal@467 51 """
bgneal@467 52 return True
bgneal@467 53
bgneal@467 54 def enqueue(self, action, instance):
bgneal@467 55 """
bgneal@467 56 This method enqueues the instance only if the can_index() method
bgneal@467 57 returns True.
bgneal@467 58
bgneal@467 59 """
bgneal@467 60 if (action == 'update' and self.can_index(instance) or
bgneal@467 61 action == 'delete'):
bgneal@467 62 super(CondQueuedSearchIndex, self).enqueue(action, instance)