From 3127048be5e10ccffc8c4374aee5fd841804e6b9 Mon Sep 17 00:00:00 2001 From: Emanuel Schuetze Date: Mon, 12 Jan 2015 12:31:11 +0100 Subject: [PATCH] Fixed haystack indexing problem for m2m objects. Updated django-haystack to <2.4. --- CHANGELOG | 2 ++ openslides/assignment/models.py | 12 ++++++++++++ openslides/global_settings.py | 2 +- openslides/utils/haystack_processor.py | 23 +++++++++++++++++++++++ requirements_production.txt | 2 +- 5 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 openslides/utils/haystack_processor.py diff --git a/CHANGELOG b/CHANGELOG index 96ed68d64..89e2966ae 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -10,6 +10,8 @@ Version 1.7 (unreleased) Core: - New feature to tag motions, agenda and assignments. +- Fixed search index problem to index contents of many-to-many table + (e.g. tags of a motion). motion: - New Feature to create amendments, which are related to a parent motion. diff --git a/openslides/assignment/models.py b/openslides/assignment/models.py index 45ac1c463..d0a1ad8e0 100644 --- a/openslides/assignment/models.py +++ b/openslides/assignment/models.py @@ -33,6 +33,18 @@ class AssignmentCandidate(RelatedModelMixin, models.Model): class Meta: unique_together = ("assignment", "person") + # Will be removed in 2.0 (required for haystack fix) + def save(self, *args, **kwargs): + super(AssignmentCandidate, self).save(*args, **kwargs) + models.signals.m2m_changed.send(sender=self, action='post_add', + instance=self.assignment) + + # Will be removed in 2.0 (required for haystack fix) + def delete(self, *args, **kwargs): + super(AssignmentCandidate, self).delete(*args, **kwargs) + models.signals.m2m_changed.send(sender=self, action='post_remove', + instance=self.assignment) + def __unicode__(self): return unicode(self.person) diff --git a/openslides/global_settings.py b/openslides/global_settings.py index cd105bfcf..0869b297b 100644 --- a/openslides/global_settings.py +++ b/openslides/global_settings.py @@ -128,7 +128,7 @@ HAYSTACK_CONNECTIONS = { } # Haystack updates search index after each save/delete action by apps -HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor' +HAYSTACK_SIGNAL_PROCESSOR = 'openslides.utils.haystack_processor.OpenSlidesProcessor' # Adds all automaticly collected plugins INSTALLED_PLUGINS = collect_plugins() diff --git a/openslides/utils/haystack_processor.py b/openslides/utils/haystack_processor.py new file mode 100644 index 000000000..7b0141308 --- /dev/null +++ b/openslides/utils/haystack_processor.py @@ -0,0 +1,23 @@ +from django.db import models +from haystack.signals import RealtimeSignalProcessor + + +class OpenSlidesProcessor(RealtimeSignalProcessor): + def setup(self): + # Naive (listen to all model saves). + super(OpenSlidesProcessor, self).setup() + models.signals.m2m_changed.connect(self.handle_many_to_many) + + def teardown(self): + # Naive (listen to all model saves). + super(OpenSlidesProcessor, self).teardown() + models.signals.m2m_changed.disconnect(self.handle_many_to_many) + + def handle_many_to_many(self, sender, instance, **kwargs): + """ + Given an individual model instance, determine which backends the + update should be sent to & update the object on those backends. + """ + model_class = type(instance) + if kwargs['action'] == 'post_add' or kwargs['action'] == 'post_clear' or kwargs['action'] == 'post_remove': + self.handle_save(model_class, instance, **kwargs) diff --git a/requirements_production.txt b/requirements_production.txt index 87adfc1b8..5366b7687 100644 --- a/requirements_production.txt +++ b/requirements_production.txt @@ -3,7 +3,7 @@ Django>=1.5,<1.7 beautifulsoup4>=4.3,<4.4 bleach>=1.2,<1.5 django-ckeditor-updated>=4.2,<4.3 -django-haystack>=2.1,<2.2 +django-haystack>=2.1,<2.4 django-mptt>=0.6,<0.7 jsonfield>=0.9,<0.10 natsort>=3.2,<3.3