From e9144e76555454e46da97b92180bcf5b4660b6e8 Mon Sep 17 00:00:00 2001 From: Karim Tayie Date: Wed, 13 Jan 2021 17:41:19 +0200 Subject: [PATCH 1/3] feat: add auto_indexing param to register decorator function --- algoliasearch_django/decorators.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From 98018ee51518e0040071dca8581b61d1aa584d65 Mon Sep 17 00:00:00 2001 From: Karim Tayie Date: Wed, 13 Jan 2021 18:16:29 +0200 Subject: [PATCH 2/3] feat: take auto_indexing if its a bool value else take from settings --- algoliasearch_django/registration.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/algoliasearch_django/registration.py b/algoliasearch_django/registration.py index f36ae59..f669415 100644 --- a/algoliasearch_django/registration.py +++ b/algoliasearch_django/registration.py @@ -69,8 +69,9 @@ 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) From 5bc61e160c64601ce88c44e0ebb00b40f360ffb0 Mon Sep 17 00:00:00 2001 From: Youcef Mammar Date: Tue, 9 Mar 2021 15:18:54 +0100 Subject: [PATCH 3/3] tests: add tests for auto_indexing --- algoliasearch_django/registration.py | 3 ++- tests/test_engine.py | 38 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/algoliasearch_django/registration.py b/algoliasearch_django/registration.py index f669415..b5d1ed7 100644 --- a/algoliasearch_django/registration.py +++ b/algoliasearch_django/registration.py @@ -75,7 +75,8 @@ def register(self, model, index_cls=AlgoliaIndex, auto_indexing=None): # 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