Mercurial > public > sg101
comparison 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 |
comparison
equal
deleted
inserted
replaced
466:c78c6e007e61 | 467:b910cc1460c8 |
---|---|
5 """ | 5 """ |
6 import urllib | 6 import urllib |
7 | 7 |
8 from django import forms | 8 from django import forms |
9 from haystack.forms import ModelSearchForm | 9 from haystack.forms import ModelSearchForm |
10 from queued_search.indexes import QueuedSearchIndex | |
10 | 11 |
11 | 12 |
12 MODEL_CHOICES = ( | 13 MODEL_CHOICES = ( |
13 ('forums.topic', 'Forum Topics'), | 14 ('forums.topic', 'Forum Topics'), |
14 ('forums.post', 'Forum Posts'), | 15 ('forums.post', 'Forum Posts'), |
32 | 33 |
33 def __init__(self, *args, **kwargs): | 34 def __init__(self, *args, **kwargs): |
34 super(CustomModelSearchForm, self).__init__(*args, **kwargs) | 35 super(CustomModelSearchForm, self).__init__(*args, **kwargs) |
35 self.fields['models'] = forms.MultipleChoiceField(choices=MODEL_CHOICES, | 36 self.fields['models'] = forms.MultipleChoiceField(choices=MODEL_CHOICES, |
36 label='', widget=forms.CheckboxSelectMultiple) | 37 label='', widget=forms.CheckboxSelectMultiple) |
38 | |
39 | |
40 class CondQueuedSearchIndex(QueuedSearchIndex): | |
41 """ | |
42 This customized version of QueuedSearchIndex conditionally enqueues items | |
43 to be indexed by calling the can_index() method. | |
44 | |
45 """ | |
46 def can_index(self, instance): | |
47 """ | |
48 The default is to index all instances. Override this method to | |
49 customize the behavior. This will be called on all update operations. | |
50 | |
51 """ | |
52 return True | |
53 | |
54 def enqueue(self, action, instance): | |
55 """ | |
56 This method enqueues the instance only if the can_index() method | |
57 returns True. | |
58 | |
59 """ | |
60 if (action == 'update' and self.can_index(instance) or | |
61 action == 'delete'): | |
62 super(CondQueuedSearchIndex, self).enqueue(action, instance) |