diff --git a/algoliasearch_django/decorators.py b/algoliasearch_django/decorators.py index 23c4eae..f019e48 100644 --- a/algoliasearch_django/decorators.py +++ b/algoliasearch_django/decorators.py @@ -34,7 +34,7 @@ def inner(*args, **kwargs): return inner -def register(model): +def register(model, auto_indexing=None): """ Register the given model class and wrapped AlgoliaIndex class with the Algolia engine: @@ -49,7 +49,7 @@ def _algolia_engine_wrapper(index_class): if not issubclass(index_class, AlgoliaIndex): raise ValueError('Wrapped class must subclass AlgoliaIndex.') - register(model, index_class) + register(model, index_class, auto_indexing) return index_class return _algolia_engine_wrapper diff --git a/algoliasearch_django/registration.py b/algoliasearch_django/registration.py index f36ae59..b5d1ed7 100644 --- a/algoliasearch_django/registration.py +++ b/algoliasearch_django/registration.py @@ -69,12 +69,14 @@ def register(self, model, index_cls=AlgoliaIndex, auto_indexing=None): index_obj = index_cls(model, self.client, self.__settings) self.__registered_models[model] = index_obj - if (isinstance(auto_indexing, bool) and - auto_indexing) or self.__auto_indexing: + enable_auto_indexing = auto_indexing if isinstance(auto_indexing, bool) else self.__auto_indexing + + if enable_auto_indexing: # Connect to the signalling framework. post_save.connect(self.__post_save_receiver, model) pre_delete.connect(self.__pre_delete_receiver, model) - logger.info('REGISTER %s', model) + + logger.info('REGISTER %s', model) def unregister(self, model): """ diff --git a/tests/test_engine.py b/tests/test_engine.py index a0baa76..a6e7c57 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -1,6 +1,7 @@ import six from django.conf import settings +from mock import patch from django.test import TestCase from algoliasearch_django import algolia_engine @@ -8,6 +9,7 @@ from algoliasearch_django import AlgoliaEngine from algoliasearch_django.registration import AlgoliaEngineError from algoliasearch_django.registration import RegistrationError +from django.db.models import signals from .models import Website, User @@ -83,6 +85,42 @@ class WebsiteIndex(AlgoliaIndex): self.assertEqual(WebsiteIndex.__name__, self.engine.get_adapter(Website).__class__.__name__) + @patch.object(signals.post_save, 'connect') + @patch.object(signals.pre_delete, 'connect') + def test_register_with_implicit_autoindexing(self, mock_pre_delete_connect, mock_post_save_connect): + class WebsiteIndex(AlgoliaIndex): + pass + + engine = AlgoliaEngine() + engine.register(Website, WebsiteIndex) + + self.assertTrue(mock_post_save_connect.called) + self.assertTrue(mock_pre_delete_connect.called) + + @patch.object(signals.post_save, 'connect') + @patch.object(signals.pre_delete, 'connect') + def test_register_with_autoindexing(self, mock_pre_delete_connect, mock_post_save_connect): + class WebsiteIndex(AlgoliaIndex): + pass + + engine = AlgoliaEngine() + engine.register(Website, WebsiteIndex, auto_indexing=True) + + self.assertTrue(mock_post_save_connect.called) + self.assertTrue(mock_pre_delete_connect.called) + + @patch.object(signals.post_save, 'connect') + @patch.object(signals.pre_delete, 'connect') + def test_register_without_autoindexing(self, mock_pre_delete_connect, mock_post_save_connect): + class WebsiteIndex(AlgoliaIndex): + pass + + engine = AlgoliaEngine() + engine.register(Website, WebsiteIndex, auto_indexing=False) + + self.assertFalse(mock_post_save_connect.called) + self.assertFalse(mock_pre_delete_connect.called) + def test_register_with_custom_index_exception(self): class WebsiteIndex(object): pass