Created
October 7, 2009 19:28
-
-
Save notanumber/204344 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 95eeb67..5f467a8 100644 | |
--- a/haystack/indexes.py | |
+++ b/haystack/indexes.py | |
@@ -1,4 +1,4 @@ | |
-from django.db.models import signals | |
+from django.db.models import signals, FieldDoesNotExist | |
import haystack | |
from haystack.fields import * | |
@@ -58,7 +58,12 @@ class SearchIndex(object): | |
for field_name, field in self.fields.items(): | |
if field.document is True: | |
content_fields.append(field_name) | |
- | |
+ if field.model_attr: | |
+ try: | |
+ model._meta.get_field(field.model_attr) | |
+ except FieldDoesNotExist: | |
+ raise SearchFieldError("The model_attr %s is invalid for model %s" % (field.model_attr, model)) | |
+ | |
if not len(content_fields) == 1: | |
raise SearchFieldError("An index must have one (and only one) SearchField with document=True.") | |
@@ -95,7 +100,7 @@ class SearchIndex(object): | |
if hasattr(self, "prepare_%s" % field_name): | |
value = getattr(self, "prepare_%s" % field_name)(obj) | |
self.prepared_data[field_name] = value | |
- | |
+ | |
# Remove any fields that lack a value and are `null=True`. | |
for field_name, field in self.fields.items(): | |
if field.null is True: | |
diff --git a/tests/core/fixtures/initial_data.json b/tests/core/fixtures/initial_data.json | |
index ab58d28..246e2c1 100644 | |
--- a/tests/core/fixtures/initial_data.json | |
+++ b/tests/core/fixtures/initial_data.json | |
@@ -17,7 +17,7 @@ | |
"pk": 1, | |
"model": "core.mockmodel", | |
"fields": { | |
- "user": "daniel1", | |
+ "author": "daniel1", | |
"foo": "bar", | |
"pub_date": "2009-03-17 06:00:00", | |
"tag": 1 | |
@@ -27,7 +27,7 @@ | |
"pk": 2, | |
"model": "core.mockmodel", | |
"fields": { | |
- "user": "daniel2", | |
+ "author": "daniel2", | |
"foo": "bar", | |
"pub_date": "2009-03-17 07:00:00", | |
"tag": 1 | |
@@ -37,7 +37,7 @@ | |
"pk": 3, | |
"model": "core.mockmodel", | |
"fields": { | |
- "user": "daniel3", | |
+ "author": "daniel3", | |
"foo": "bar", | |
"pub_date": "2009-03-17 08:00:00", | |
"tag": 2 | |
diff --git a/tests/core/models.py b/tests/core/models.py | |
index acf08be..feb0d9b 100644 | |
--- a/tests/core/models.py | |
+++ b/tests/core/models.py | |
@@ -8,7 +8,7 @@ class MockTag(models.Model): | |
class MockModel(models.Model): | |
- user = models.CharField(max_length=255) | |
+ author = models.CharField(max_length=255) | |
foo = models.CharField(max_length=255, blank=True) | |
pub_date = models.DateTimeField(default=datetime.datetime.now) | |
tag = models.ForeignKey(MockTag) | |
@@ -18,7 +18,7 @@ class MockModel(models.Model): | |
class AnotherMockModel(models.Model): | |
- user = models.CharField(max_length=255) | |
+ author = models.CharField(max_length=255) | |
pub_date = models.DateTimeField(default=datetime.datetime.now) | |
def __unicode__(self): | |
diff --git a/tests/core/tests/indexes.py b/tests/core/tests/indexes.py | |
index 8ec9396..8fdbcb2 100644 | |
--- a/tests/core/tests/indexes.py | |
+++ b/tests/core/tests/indexes.py | |
@@ -6,32 +6,39 @@ from core.tests.mocks import MockSearchBackend | |
class BadSearchIndex1(indexes.SearchIndex): | |
- author = indexes.CharField(model_attr='user') | |
+ author = indexes.CharField(model_attr='author') | |
pub_date = indexes.DateTimeField(model_attr='pub_date') | |
class BadSearchIndex2(indexes.SearchIndex): | |
content = indexes.CharField(document=True, use_template=True) | |
content2 = indexes.CharField(document=True, use_template=True) | |
- author = indexes.CharField(model_attr='user') | |
+ author = indexes.CharField(model_attr='author') | |
pub_date = indexes.DateTimeField(model_attr='pub_date') | |
+class BadSearchIndex3(indexes.SearchIndex): | |
+ content = indexes.CharField(document=True, use_template=True) | |
+ author = indexes.CharField(model_attr='author') | |
+ pub_date = indexes.DateTimeField(model_attr='pub_date') | |
+ invalid = indexes.CharField(model_attr='invalid') | |
+ | |
+ | |
class GoodMockSearchIndex(indexes.SearchIndex): | |
content = indexes.CharField(document=True, use_template=True) | |
- author = indexes.CharField(model_attr='user') | |
+ author = indexes.CharField(model_attr='author') | |
pub_date = indexes.DateTimeField(model_attr='pub_date') | |
extra = indexes.CharField(indexed=False, use_template=True) | |
# For testing inheritance... | |
class AltGoodMockSearchIndex(GoodMockSearchIndex): | |
- additional = indexes.CharField(model_attr='user') | |
+ additional = indexes.CharField(model_attr='author') | |
class GoodCustomMockSearchIndex(indexes.SearchIndex): | |
content = indexes.CharField(document=True, use_template=True) | |
- author = indexes.CharField(model_attr='user') | |
+ author = indexes.CharField(model_attr='author') | |
pub_date = indexes.DateTimeField(model_attr='pub_date') | |
extra = indexes.CharField(indexed=False, use_template=True) | |
@@ -49,7 +56,7 @@ class GoodCustomMockSearchIndex(indexes.SearchIndex): | |
class GoodNullableMockSearchIndex(indexes.SearchIndex): | |
content = indexes.CharField(document=True, use_template=True) | |
- author = indexes.CharField(model_attr='user', null=True) | |
+ author = indexes.CharField(model_attr='author', null=True) | |
class SearchIndexTestCase(TestCase): | |
@@ -94,6 +101,9 @@ class SearchIndexTestCase(TestCase): | |
def test_too_many_contentfields_present(self): | |
self.assertRaises(indexes.SearchFieldError, BadSearchIndex2, MockModel, MockSearchBackend()) | |
+ | |
+ def test_invalid_model_attr_present(self): | |
+ self.assertRaises(indexes.SearchFieldError, BadSearchIndex3, MockModel, MockSearchBackend()) | |
def test_contentfield_present(self): | |
try: | |
@@ -127,7 +137,7 @@ class SearchIndexTestCase(TestCase): | |
def test_custom_prepare(self): | |
mock = MockModel() | |
mock.pk = 20 | |
- mock.user = 'daniel%s' % mock.id | |
+ mock.author = 'daniel%s' % mock.id | |
mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) | |
self.assertEqual(len(self.cmi.prepare(mock)), 5) | |
@@ -136,7 +146,7 @@ class SearchIndexTestCase(TestCase): | |
def test_custom_prepare_author(self): | |
mock = MockModel() | |
mock.pk = 20 | |
- mock.user = 'daniel%s' % mock.id | |
+ mock.author = 'daniel%s' % mock.id | |
mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) | |
self.assertEqual(len(self.cmi.prepare(mock)), 5) | |
@@ -156,7 +166,7 @@ class SearchIndexTestCase(TestCase): | |
mock = MockModel() | |
mock.pk = 20 | |
- mock.user = 'daniel%s' % mock.id | |
+ mock.author = 'daniel%s' % mock.id | |
mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) | |
self.mi.update_object(mock) | |
@@ -233,19 +243,19 @@ class BasicModelSearchIndex(indexes.ModelSearchIndex): | |
class FieldsModelSearchIndex(indexes.ModelSearchIndex): | |
class Meta: | |
- fields = ['user', 'pub_date'] | |
+ fields = ['author', 'pub_date'] | |
class ExcludesModelSearchIndex(indexes.ModelSearchIndex): | |
class Meta: | |
- excludes = ['user', 'foo'] | |
+ excludes = ['author', 'foo'] | |
class FieldsWithOverrideModelSearchIndex(indexes.ModelSearchIndex): | |
foo = indexes.IntegerField(model_attr='foo') | |
class Meta: | |
- fields = ['user', 'foo'] | |
+ fields = ['author', 'foo'] | |
class ModelSearchIndexTestCase(TestCase): | |
@@ -262,9 +272,9 @@ class ModelSearchIndexTestCase(TestCase): | |
self.assert_('foo' in self.bmsi.fields) | |
self.assert_(isinstance(self.bmsi.fields['foo'], indexes.CharField)) | |
self.assertEqual(self.bmsi.fields['foo'].null, False) | |
- self.assert_('user' in self.bmsi.fields) | |
- self.assert_(isinstance(self.bmsi.fields['user'], indexes.CharField)) | |
- self.assertEqual(self.bmsi.fields['user'].null, False) | |
+ self.assert_('author' in self.bmsi.fields) | |
+ self.assert_(isinstance(self.bmsi.fields['author'], indexes.CharField)) | |
+ self.assertEqual(self.bmsi.fields['author'].null, False) | |
self.assert_('pub_date' in self.bmsi.fields) | |
self.assert_(isinstance(self.bmsi.fields['pub_date'], indexes.DateTimeField)) | |
self.assert_(isinstance(self.bmsi.fields['pub_date'].default, datetime.datetime)) | |
@@ -275,8 +285,8 @@ class ModelSearchIndexTestCase(TestCase): | |
def test_fields(self): | |
self.assertEqual(len(self.fmsi.fields), 3) | |
- self.assert_('user' in self.fmsi.fields) | |
- self.assert_(isinstance(self.fmsi.fields['user'], indexes.CharField)) | |
+ self.assert_('author' in self.fmsi.fields) | |
+ self.assert_(isinstance(self.fmsi.fields['author'], indexes.CharField)) | |
self.assert_('pub_date' in self.fmsi.fields) | |
self.assert_(isinstance(self.fmsi.fields['pub_date'], indexes.DateTimeField)) | |
self.assert_('text' in self.fmsi.fields) | |
@@ -291,8 +301,8 @@ class ModelSearchIndexTestCase(TestCase): | |
def test_fields_with_override(self): | |
self.assertEqual(len(self.fwomsi.fields), 3) | |
- self.assert_('user' in self.fwomsi.fields) | |
- self.assert_(isinstance(self.fwomsi.fields['user'], indexes.CharField)) | |
+ self.assert_('author' in self.fwomsi.fields) | |
+ self.assert_(isinstance(self.fwomsi.fields['author'], indexes.CharField)) | |
self.assert_('foo' in self.fwomsi.fields) | |
self.assert_(isinstance(self.fwomsi.fields['foo'], indexes.IntegerField)) | |
self.assert_('text' in self.fwomsi.fields) | |
diff --git a/tests/core/tests/models.py b/tests/core/tests/models.py | |
index a0857e7..fd09043 100644 | |
--- a/tests/core/tests/models.py | |
+++ b/tests/core/tests/models.py | |
@@ -76,7 +76,7 @@ class SearchResultTestCase(TestCase): | |
from haystack import indexes | |
class TestSearchIndex(indexes.SearchIndex): | |
- stored = indexes.CharField(model_attr='stored', document=True) | |
+ stored = indexes.CharField(model_attr='author', document=True) | |
# Register the index & try again. | |
haystack.site.register(MockModel, TestSearchIndex) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment