Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add auto_indexing param to register decorator function #305

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions algoliasearch_django/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions algoliasearch_django/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
38 changes: 38 additions & 0 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import six

from django.conf import settings
from mock import patch
from django.test import TestCase

from algoliasearch_django import algolia_engine
from algoliasearch_django import AlgoliaIndex
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

Expand Down Expand Up @@ -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
Expand Down