Created
November 4, 2009 20:27
-
-
Save notanumber/226348 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/haystack/indexes.py b/haystack/indexes.py | |
index 0fc5cd9..441d16d 100644 | |
--- a/haystack/indexes.py | |
+++ b/haystack/indexes.py | |
@@ -62,18 +62,6 @@ class SearchIndex(object): | |
if not len(content_fields) == 1: | |
raise SearchFieldError("An index must have one (and only one) SearchField with document=True.") | |
- def _setup_save(self, model): | |
- signals.post_save.connect(self.update_object, sender=model) | |
- | |
- def _setup_delete(self, model): | |
- signals.post_delete.connect(self.remove_object, sender=model) | |
- | |
- def _teardown_save(self, model): | |
- signals.post_save.disconnect(self.update_object, sender=model) | |
- | |
- def _teardown_delete(self, model): | |
- signals.post_delete.disconnect(self.remove_object, sender=model) | |
- | |
def get_queryset(self): | |
""" | |
Get the default QuerySet to index when doing a full update. | |
@@ -175,7 +163,25 @@ class SearchIndex(object): | |
return self.model._default_manager.all() | |
-class BasicSearchIndex(SearchIndex): | |
+class SignalOnChange(object): | |
+ """ | |
+ Signals that can be attached to an index to allow automatic reindexing | |
+ on save and delete | |
+ """ | |
+ def _setup_save(self, model): | |
+ signals.post_save.connect(self.update_object, sender=model) | |
+ | |
+ def _setup_delete(self, model): | |
+ signals.post_delete.connect(self.remove_object, sender=model) | |
+ | |
+ def _teardown_save(self, model): | |
+ signals.post_save.disconnect(self.update_object, sender=model) | |
+ | |
+ def _teardown_delete(self, model): | |
+ signals.post_delete.disconnect(self.remove_object, sender=model) | |
+ | |
+ | |
+class BasicSearchIndex(SearchIndex, SignalOnChange): | |
text = CharField(document=True, use_template=True) | |
diff --git a/tests/solr_tests/tests/solr_backend.py b/tests/solr_tests/tests/solr_backend.py | |
index fd6e009..1acfb42 100644 | |
--- a/tests/solr_tests/tests/solr_backend.py | |
+++ b/tests/solr_tests/tests/solr_backend.py | |
@@ -15,13 +15,13 @@ except NameError: | |
from sets import Set as set | |
-class SolrMockSearchIndex(indexes.SearchIndex): | |
+class SolrMockSearchIndex(indexes.SearchIndex, indexes.SignalOnChange): | |
text = indexes.CharField(document=True, use_template=True) | |
name = indexes.CharField(model_attr='author') | |
pub_date = indexes.DateField(model_attr='pub_date') | |
-class SolrMaintainTypeMockSearchIndex(indexes.SearchIndex): | |
+class SolrMaintainTypeMockSearchIndex(indexes.SearchIndex, indexes.SignalOnChange): | |
text = indexes.CharField(document=True, use_template=True) | |
month = indexes.CharField(indexed=False) | |
pub_date = indexes.DateField(model_attr='pub_date') | |
diff --git a/tests/whoosh_tests/tests/whoosh_backend.py b/tests/whoosh_tests/tests/whoosh_backend.py | |
index b426e67..c893fe6 100644 | |
--- a/tests/whoosh_tests/tests/whoosh_backend.py | |
+++ b/tests/whoosh_tests/tests/whoosh_backend.py | |
@@ -18,13 +18,13 @@ except NameError: | |
from sets import Set as set | |
-class WhooshMockSearchIndex(indexes.SearchIndex): | |
+class WhooshMockSearchIndex(indexes.SearchIndex, indexes.SignalOnChange): | |
text = indexes.CharField(document=True, use_template=True) | |
name = indexes.CharField(model_attr='author') | |
pub_date = indexes.DateField(model_attr='pub_date') | |
-class AllTypesWhooshMockSearchIndex(indexes.SearchIndex): | |
+class AllTypesWhooshMockSearchIndex(indexes.SearchIndex, indexes.SignalOnChange): | |
text = indexes.CharField(document=True, use_template=True) | |
name = indexes.CharField(model_attr='author', indexed=False) | |
pub_date = indexes.DateField(model_attr='pub_date') | |
@@ -32,7 +32,7 @@ class AllTypesWhooshMockSearchIndex(indexes.SearchIndex): | |
seen_count = indexes.IntegerField(indexed=False) | |
-class WhooshMaintainTypeMockSearchIndex(indexes.SearchIndex): | |
+class WhooshMaintainTypeMockSearchIndex(indexes.SearchIndex, indexes.SignalOnChange): | |
text = indexes.CharField(document=True, use_template=True) | |
month = indexes.CharField(indexed=False) | |
pub_date = indexes.DateField(model_attr='pub_date') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment