From 318c9ff9ad50ebc9fd4da44c578c1fc6bb628c5d Mon Sep 17 00:00:00 2001 From: Skia Date: Wed, 15 Feb 2017 22:01:33 +0100 Subject: [PATCH] Add trigger to update Whoosh index on User update --- core/search_indexes.py | 16 +++++++++++++++- sith/settings.py | 2 ++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/core/search_indexes.py b/core/search_indexes.py index 03ececa4..37befbd6 100644 --- a/core/search_indexes.py +++ b/core/search_indexes.py @@ -1,4 +1,6 @@ -from haystack import indexes +from django.db import models + +from haystack import indexes, signals from core.models import User @@ -15,3 +17,15 @@ class UserIndex(indexes.SearchIndex, indexes.Indexable): def get_updated_field(self): return "last_update" + + +class UserOnlySignalProcessor(signals.BaseSignalProcessor): + def setup(self): + # Listen only to the ``User`` model. + models.signals.post_save.connect(self.handle_save, sender=User) + models.signals.post_delete.connect(self.handle_delete, sender=User) + + def teardown(self): + # Disconnect only for the ``User`` model. + models.signals.post_save.disconnect(self.handle_save, sender=User) + models.signals.post_delete.disconnect(self.handle_delete, sender=User) diff --git a/sith/settings.py b/sith/settings.py index 51abf596..b384e312 100644 --- a/sith/settings.py +++ b/sith/settings.py @@ -153,6 +153,8 @@ HAYSTACK_CONNECTIONS = { }, } +HAYSTACK_SIGNAL_PROCESSOR = 'core.search_indexes.UserOnlySignalProcessor' + WSGI_APPLICATION = 'sith.wsgi.application'