Skip to content

Commit

Permalink
Fixed a bizarre bug where backends may see a different site object th…
Browse files Browse the repository at this point in the history
…an the rest of the application code. THIS REQUIRES SEARCH & REPLACING ALL INSTANCES OF `from haystack.sites import site` TO `from haystack import site`.

No changes needed if you've been using `haystack.autodiscover()`.
  • Loading branch information
toastdriven committed Jun 26, 2009
1 parent d2563c7 commit 95d40e4
Show file tree
Hide file tree
Showing 21 changed files with 108 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docs/searchindex_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ For the impatient::

import datetime
from haystack import indexes
from haystack.sites import site
from haystack import site
from myapp.models import Note
Expand Down
2 changes: 1 addition & 1 deletion docs/searchsite_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ you don't want.::

Alternatively, you can manually register only the indexes you want.::

from haystack.sites import site
from haystack import site
from ratings.models import Rating
from ratings.search_indexes import RatingIndex
Expand Down
6 changes: 3 additions & 3 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ default ``SearchIndex``.
If autodiscovery and inclusion of all indexes is not desirable, you can manually
register models in the following manner::

from haystack.sites import site
from haystack import site
site.register(Note)

Expand All @@ -97,7 +97,7 @@ model gets registered with a standard ``SearchIndex`` class. If you need to over
this class and provide additional functionality, you can manually register your
own indexes like::

from haystack.sites import site
from haystack import site
site.register(Note, NoteIndex)

Expand Down Expand Up @@ -125,7 +125,7 @@ include our own ``SearchIndex`` to exclude indexing future-dated notes::

import datetime
from haystack import indexes
from haystack.sites import site
from haystack import site
from myapp.models import Note
Expand Down
1 change: 1 addition & 0 deletions haystack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from haystack.sites import site


__author__ = 'Daniel Lindsley'
Expand Down
2 changes: 1 addition & 1 deletion haystack/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, site=None):
if site is not None:
self.site = site
else:
from haystack.sites import site
from haystack import site
self.site = site

def get_identifier(self, obj):
Expand Down
7 changes: 4 additions & 3 deletions haystack/backends/solr_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class SearchBackend(BaseSearchBackend):
'[', ']', '^', '"', '~', '*', '?', ':',
)

def __init__(self):
def __init__(self, site=None):
super(SearchBackend, self).__init__(site)

if not hasattr(settings, 'HAYSTACK_SOLR_URL'):
raise ImproperlyConfigured('You must specify a HAYSTACK_SOLR_URL in your settings.')

Expand Down Expand Up @@ -125,8 +127,7 @@ def search(self, query_string, sort_by=None, start_offset=0, end_offset=None,
return self._process_results(raw_results, highlight=highlight)

def more_like_this(self, model_instance):
from haystack.sites import site, NotRegistered
index = site.get_index(model_instance.__class__)
index = self.site.get_index(model_instance.__class__)
field_name = index.get_content_field()
raw_results = self.conn.more_like_this("id:%s" % self.get_identifier(model_instance), field_name, fl='*,score')
return self._process_results(raw_results)
Expand Down
2 changes: 1 addition & 1 deletion haystack/management/commands/build_solr_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def handle_noargs(self, **options):
# Cause the default site to load.
from django.conf import settings
__import__(settings.ROOT_URLCONF)
from haystack.sites import site
from haystack import site

default_operator = getattr(settings, 'HAYSTACK_DEFAULT_OPERATOR', DEFAULT_OPERATOR)
content_field_name, fields = site.build_unified_schema()
Expand Down
2 changes: 1 addition & 1 deletion haystack/management/commands/clear_search_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def handle_noargs(self, **options):
# Cause the default site to load.
from django.conf import settings
__import__(settings.ROOT_URLCONF)
from haystack.sites import site
from haystack import site

print
print "WARNING: This will irreparably remove EVERYTHING from your search index."
Expand Down
2 changes: 1 addition & 1 deletion haystack/management/commands/haystack_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def handle_noargs(self, **options):
# Cause the default site to load.
from django.conf import settings
__import__(settings.ROOT_URLCONF)
from haystack.sites import site
from haystack import site

index_count = len(site.get_indexed_models())
print "Loaded URLconf to initialize SearchSite..."
Expand Down
3 changes: 2 additions & 1 deletion haystack/management/commands/reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def handle_app(self, app, **options):
handle_registrations()

from django.db.models import get_models
from haystack.sites import site, NotRegistered
from haystack import site
from haystack.exceptions import NotRegistered

for model in get_models(app):
try:
Expand Down
2 changes: 1 addition & 1 deletion haystack/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, site=None, query=None):
if site is not None:
self.site = site
else:
from haystack.sites import site as main_site
from haystack import site as main_site
self.site = main_site

def __getstate__(self):
Expand Down
4 changes: 2 additions & 2 deletions haystack/sites.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from django.db.models.base import ModelBase
from haystack.exceptions import AlreadyRegistered, NotRegistered
from haystack.indexes import BasicSearchIndex
from haystack.fields import *
try:
set
except NameError:
Expand Down Expand Up @@ -37,6 +35,7 @@ def register(self, model, index_class=None):
to the model.
"""
if not index_class:
from haystack.indexes import BasicSearchIndex
index_class = BasicSearchIndex

if not isinstance(model, ModelBase):
Expand Down Expand Up @@ -91,6 +90,7 @@ def build_unified_schema(self):
With no arguments, it will pull in the main site to discover the available
SearchIndexes.
"""
from haystack.fields import *
content_field_name = ''
fields = []
field_names = set()
Expand Down
Empty file.
8 changes: 8 additions & 0 deletions tests/site_registration/auto_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.conf.urls.defaults import *

import haystack
haystack.autodiscover()

urlpatterns = patterns('',
(r'^search/', include('haystack.urls')),
)
15 changes: 15 additions & 0 deletions tests/site_registration/manual_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.conf.urls.defaults import *

from haystack import indexes
from haystack import site
from site_registration.models import Foo, Bar

class FooIndex(indexes.SearchIndex):
text = indexes.CharField(document=True, model_attr='body')

site.register(Foo, FooIndex)
site.register(Bar)

urlpatterns = patterns('',
(r'^search/', include('haystack.urls')),
)
17 changes: 17 additions & 0 deletions tests/site_registration/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.db import models


class Foo(models.Model):
title = models.CharField(max_length=255)
body = models.TextField()

def __unicode__(self):
return self.title


class Bar(models.Model):
author = models.CharField(max_length=255)
content = models.TextField()

def __unicode__(self):
return self.author
11 changes: 11 additions & 0 deletions tests/site_registration/search_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from haystack import indexes
from haystack import site
from site_registration.models import Foo, Bar


class FooIndex(indexes.SearchIndex):
text = indexes.CharField(document=True, model_attr='body')


site.register(Foo, FooIndex)
site.register(Bar)
2 changes: 2 additions & 0 deletions tests/site_registration/templates/search/indexes/bar_text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ object.title }}
{{ object.body }}
22 changes: 22 additions & 0 deletions tests/site_registration/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.test import TestCase

class ManualSiteRegistrationTestCase(TestCase):
def test_registrations(self):
from haystack import backend
sb = backend.SearchBackend()
self.assertEqual(len(sb.site.get_indexed_models()), 2)

from haystack import site
self.assertEqual(len(site.get_indexed_models()), 2)


class AutoSiteRegistrationTestCase(TestCase):
urls = 'site_registration.auto_urls'

def test_registrations(self):
from haystack import backend
sb = backend.SearchBackend()
self.assertEqual(len(sb.site.get_indexed_models()), 2)

from haystack import site
self.assertEqual(len(site.get_indexed_models()), 2)
12 changes: 12 additions & 0 deletions tests/site_registration_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os
from settings import *

INSTALLED_APPS += [
'site_registration',
]

ROOT_URLCONF = 'site_registration.manual_urls'

HAYSTACK_SEARCH_ENGINE = 'whoosh'
HAYSTACK_WHOOSH_PATH = os.path.join('tmp', 'test_whoosh_query')
HAYSTACK_INCLUDE_SPELLING = True
4 changes: 2 additions & 2 deletions tests/solr_tests/tests/solr_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def setUp(self):
self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
self.raw_solr.delete(q='*:*')

self.sb = SearchBackend()
self.smmi = SolrMockSearchIndex(MockModel, backend=self.sb)
self.site = SolrSearchSite()
self.sb = SearchBackend(site=self.site)
self.smmi = SolrMockSearchIndex(MockModel, backend=self.sb)
self.site.register(MockModel, SolrMockSearchIndex)

# Stow.
Expand Down

0 comments on commit 95d40e4

Please sign in to comment.