bgneal@469: """ bgneal@469: This module contains custom search indexes to tailor the Haystack search bgneal@469: application to our needs. bgneal@469: bgneal@469: """ bgneal@469: from queued_search.indexes import QueuedSearchIndex bgneal@469: bgneal@469: bgneal@469: class CondQueuedSearchIndex(QueuedSearchIndex): bgneal@469: """ bgneal@469: This customized version of QueuedSearchIndex conditionally enqueues items bgneal@469: to be indexed by calling the can_index() method. bgneal@469: bgneal@469: """ bgneal@469: def can_index(self, instance): bgneal@469: """ bgneal@469: The default is to index all instances. Override this method to bgneal@469: customize the behavior. This will be called on all update operations. bgneal@469: bgneal@469: """ bgneal@469: return True bgneal@469: bgneal@469: def enqueue(self, action, instance): bgneal@469: """ bgneal@469: This method enqueues the instance only if the can_index() method bgneal@469: returns True. bgneal@469: bgneal@469: """ bgneal@469: if (action == 'update' and self.can_index(instance) or bgneal@469: action == 'delete'): bgneal@469: super(CondQueuedSearchIndex, self).enqueue(action, instance) bgneal@677: bgneal@677: bgneal@677: class PublicQueuedSearchIndex(QueuedSearchIndex): bgneal@677: """QueuedSearchIndex for models with is_public attributes.""" bgneal@677: bgneal@677: def enqueue(self, action, instance): bgneal@677: """Conditionally enqueue actions as follows. bgneal@677: bgneal@677: For update actions: if is_public is True, enqueue the update. If bgneal@677: is_public is False, enqueue a delete action. bgneal@677: bgneal@677: Delete actions are always enqueued. bgneal@677: bgneal@677: """ bgneal@677: if action == 'update' and instance.is_public: bgneal@677: super(PublicQueuedSearchIndex, self).enqueue(action, instance) bgneal@677: elif (action == 'update' and not instance.is_public) or action == 'delete': bgneal@677: super(PublicQueuedSearchIndex, self).enqueue('delete', instance)