annotate custom_search/indexes.py @ 677:858ce870c854

For #47, create custom search index for models with is_public.
author Brian Neal <bgneal@gmail.com>
date Wed, 14 Aug 2013 13:56:43 -0500
parents ee87ea74d46b
children
rev   line source
bgneal@469 1 """
bgneal@469 2 This module contains custom search indexes to tailor the Haystack search
bgneal@469 3 application to our needs.
bgneal@469 4
bgneal@469 5 """
bgneal@469 6 from queued_search.indexes import QueuedSearchIndex
bgneal@469 7
bgneal@469 8
bgneal@469 9 class CondQueuedSearchIndex(QueuedSearchIndex):
bgneal@469 10 """
bgneal@469 11 This customized version of QueuedSearchIndex conditionally enqueues items
bgneal@469 12 to be indexed by calling the can_index() method.
bgneal@469 13
bgneal@469 14 """
bgneal@469 15 def can_index(self, instance):
bgneal@469 16 """
bgneal@469 17 The default is to index all instances. Override this method to
bgneal@469 18 customize the behavior. This will be called on all update operations.
bgneal@469 19
bgneal@469 20 """
bgneal@469 21 return True
bgneal@469 22
bgneal@469 23 def enqueue(self, action, instance):
bgneal@469 24 """
bgneal@469 25 This method enqueues the instance only if the can_index() method
bgneal@469 26 returns True.
bgneal@469 27
bgneal@469 28 """
bgneal@469 29 if (action == 'update' and self.can_index(instance) or
bgneal@469 30 action == 'delete'):
bgneal@469 31 super(CondQueuedSearchIndex, self).enqueue(action, instance)
bgneal@677 32
bgneal@677 33
bgneal@677 34 class PublicQueuedSearchIndex(QueuedSearchIndex):
bgneal@677 35 """QueuedSearchIndex for models with is_public attributes."""
bgneal@677 36
bgneal@677 37 def enqueue(self, action, instance):
bgneal@677 38 """Conditionally enqueue actions as follows.
bgneal@677 39
bgneal@677 40 For update actions: if is_public is True, enqueue the update. If
bgneal@677 41 is_public is False, enqueue a delete action.
bgneal@677 42
bgneal@677 43 Delete actions are always enqueued.
bgneal@677 44
bgneal@677 45 """
bgneal@677 46 if action == 'update' and instance.is_public:
bgneal@677 47 super(PublicQueuedSearchIndex, self).enqueue(action, instance)
bgneal@677 48 elif (action == 'update' and not instance.is_public) or action == 'delete':
bgneal@677 49 super(PublicQueuedSearchIndex, self).enqueue('delete', instance)