Skip to content

Commit

Permalink
upgrade haystack dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmulley committed Jan 21, 2016
1 parent 3226c21 commit fc718d2
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 56 deletions.
9 changes: 4 additions & 5 deletions parliament/bills/search_indexes.py
@@ -1,14 +1,12 @@
#coding: utf-8

from haystack import site
from haystack import indexes

from parliament.search.index import SearchIndex
from parliament.bills.models import Bill
from parliament.core.models import Session


class BillIndex(SearchIndex):
class BillIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, model_attr='get_text')
searchtext = indexes.CharField(use_template=True)
boosted = indexes.CharField(stored=False, use_template=True)
Expand Down Expand Up @@ -39,9 +37,10 @@ def prepare_title(self, obj):
else:
return obj.name[:140] + u'…'

def get_queryset(self):
def index_queryset(self):
return Bill.objects.all().prefetch_related(
'sponsor_politician', 'sponsor_member', 'sponsor_member__party'
)

site.register(Bill, BillIndex)
def get_model(self):
return Bill
12 changes: 5 additions & 7 deletions parliament/core/search_indexes.py
@@ -1,10 +1,8 @@
from haystack import site
from haystack import indexes

from parliament.core.models import Politician
from parliament.search.index import SearchIndex

class PolIndex(SearchIndex):
class PolIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
boosted = indexes.CharField(use_template=True, stored=False)
politician = indexes.CharField(model_attr='name', indexed=False)
Expand All @@ -13,12 +11,12 @@ class PolIndex(SearchIndex):
url = indexes.CharField(model_attr='get_absolute_url', indexed=False)
doctype = indexes.CharField(default='mp')

def get_queryset(self):
def index_queryset(self):
return Politician.objects.elected()

def get_model(self):
return Politician

def should_obj_be_indexed(self, obj):
# Currently used only in live updates, not batch indexing
return bool(obj.latest_member)


site.register(Politician, PolIndex)
3 changes: 0 additions & 3 deletions parliament/default_settings.py
Expand Up @@ -10,9 +10,6 @@

PROJ_ROOT = os.path.dirname(os.path.realpath(__file__))

HAYSTACK_SEARCH_ENGINE = 'solr'
HAYSTACK_SITECONF = 'parliament.search_sites'

CACHE_MIDDLEWARE_KEY_PREFIX = 'parl'
CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True

Expand Down
4 changes: 2 additions & 2 deletions parliament/hansards/admin.py
Expand Up @@ -8,9 +8,9 @@ class DocumentOptions(admin.ModelAdmin):

class StatementOptions(admin.ModelAdmin):
list_display = ('politician', 'time', 'document', 'wordcount', 'procedural')
list_filter = ('time', 'procedural')
#list_filter = ('time', 'procedural')
raw_id_fields = ('document', 'member', 'politician', 'bills', 'mentioned_politicians')
ordering = ('-time',)
#ordering = ('-time',)

admin.site.register(Document, DocumentOptions)
admin.site.register(Statement, StatementOptions)
11 changes: 5 additions & 6 deletions parliament/hansards/search_indexes.py
@@ -1,10 +1,8 @@
from haystack import site
from haystack import indexes

from parliament.search.index import SearchIndex
from parliament.hansards.models import Statement

class StatementIndex(SearchIndex):
class StatementIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, model_attr='text_plain')
searchtext = indexes.CharField(stored=False, use_template=True)
date = indexes.DateTimeField(model_attr='time')
Expand All @@ -19,8 +17,11 @@ class StatementIndex(SearchIndex):
committee = indexes.CharField(model_attr='committee_name')
committee_slug = indexes.CharField(model_attr='committee_slug')
doctype = indexes.CharField(null=True)

def get_model(self):
return Statement

def get_queryset(self):
def index_queryset(self):
return Statement.objects.all().prefetch_related(
'member__politician', 'member__party', 'member__riding', 'document',
'document__committeemeeting__committee'
Expand All @@ -31,5 +32,3 @@ def prepare_doctype(self, obj):
return 'committee'
else:
return 'debate'

site.register(Statement, StatementIndex)
35 changes: 16 additions & 19 deletions parliament/search/index.py
@@ -1,22 +1,22 @@
from django.conf import settings
from django.db.models import signals

from haystack import indexes
from haystack import connections
from haystack.exceptions import NotHandled
from haystack.utils import get_identifier
from haystack.signals import BaseSignalProcessor

class QueuedSearchIndex(indexes.SearchIndex):
from parliament.search.models import IndexingTask

def _setup_save(self, model):
signals.post_save.connect(self.enqueue_save, sender=model)

def _setup_delete(self, model):
signals.post_delete.connect(self.enqueue_delete, sender=model)
class QueuedIndexSignalProcessor(BaseSignalProcessor):

def _teardown_save(self, model):
signals.post_save.disconnect(self.enqueue_save, sender=model)
def setup(self):
signals.post_save.connect(self.enqueue_save)
signals.post_delete.connect(self.enqueue_delete)

def _teardown_delete(self, model):
signals.post_delete.disconnect(self.enqueue_delete, sender=model)
def teardown(self):
signals.post_save.disconnect(self.enqueue_save)
signals.post_delete.disconnect(self.enqueue_delete)

def enqueue_save(self, instance, **kwargs):
return self.enqueue('update', instance)
Expand All @@ -25,7 +25,11 @@ def enqueue_delete(self, instance, **kwargs):
return self.enqueue('delete', instance)

def enqueue(self, action, instance):
from parliament.search.models import IndexingTask
try:
connections['default'].get_unified_index().get_index(instance.__class__)
except NotHandled:
return False

it = IndexingTask(
action=action,
identifier=get_identifier(instance)
Expand All @@ -34,10 +38,3 @@ def enqueue(self, action, instance):
it.content_object = instance

it.save()

if getattr(settings, 'PARLIAMENT_AUTOUPDATE_SEARCH_INDEXES', False):
SearchIndex = indexes.RealTimeSearchIndex
elif getattr(settings, 'PARLIAMENT_QUEUE_SEARCH_INDEXING', False):
SearchIndex = QueuedSearchIndex
else:
SearchIndex = indexes.SearchIndex
Expand Up @@ -4,7 +4,7 @@
from django.conf import settings
from django.core.management.base import BaseCommand

from haystack import site
from haystack import connections
import pysolr

logger = logging.getLogger(__name__)
Expand All @@ -24,15 +24,15 @@ def handle(self, **options):
IndexingTask.objects.filter(action='update').prefetch_related('content_object')
)

solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL, timeout=600)
solr = pysolr.Solr(settings.HAYSTACK_CONNECTIONS['default']['URL'], timeout=600)

if update_tasks:
update_objs = [t.content_object for t in update_tasks if t.content_object]

update_objs.sort(key=lambda o: o.__class__.__name__)
for cls, objs in itertools.groupby(update_objs, lambda o: o.__class__):
logger.debug("Indexing %s" % cls)
index = site.get_index(cls)
index = connections['default'].get_unified_index().get_index(cls)
if hasattr(index, 'should_obj_be_indexed'):
objs = filter(index.should_obj_be_indexed, objs)
prepared_objs = [index.prepare(o) for o in objs]
Expand Down
2 changes: 1 addition & 1 deletion parliament/search/solr.py
Expand Up @@ -35,7 +35,7 @@ def autohighlight(results):
doc[field] = mark_safe(r_hl.sub(r'<\1em>', val))
return results

solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
solr = pysolr.Solr(settings.HAYSTACK_CONNECTIONS['default']['URL'])


class SearchQuery(BaseSearchQuery):
Expand Down
2 changes: 0 additions & 2 deletions parliament/search_sites.py

This file was deleted.

16 changes: 9 additions & 7 deletions parliament/settings.py.example
Expand Up @@ -30,20 +30,22 @@ CACHE_MIDDLEWARE_SECONDS = 60 * 60 * 3
# For search to work, you need to have a running instance of Apache Solr
# (If you just leave this as an invalid URL, the site will work with the
# exception of search features.)
HAYSTACK_SOLR_URL = 'http://127.0.0.1:8983/solr'
# If set to True, Solr will be sent an update command whenever new searchable
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
'URL': 'http://127.0.0.1:8983/solr'
},
}

# If uncommented, Solr will be sent an update command whenever new searchable
# data is added.
PARLIAMENT_AUTOUPDATE_SEARCH_INDEXES = False
#HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'

TWITTER_USERNAME = 'openparlca'
TWITTER_LIST_NAME = 'mps'

PARLIAMENT_WORDCLOUD_COMMAND = ('/usr/bin/java', '-jar' ,"/path/to/ibm-word-cloud.jar", '-c', os.path.join(PROJ_ROOT, '..', 'config', 'wordcloud.conf'), '-w', '800')

PARLIAMENT_SEND_EMAIL = False

PARLIAMENT_HTV_API_KEY = '' # Key for the How'd They Vote API, howdtheyvote.ca

# Make this unique, and don't share it with anybody.
SECRET_KEY = 'verysecretindeed'

2 changes: 1 addition & 1 deletion requirements.txt
Expand Up @@ -6,7 +6,7 @@ Django>1.8,<1.9
Markdown==2.3.1
PIL>=1.1.6
django-extensions==1.2.5
django-haystack==1.0.1-final
django-haystack==2.4.1
#psycopg2>=2.0.7
pysolr==2.0.9
sorl-thumbnail==3.2.5
Expand Down

0 comments on commit fc718d2

Please sign in to comment.