Skip to content

Commit bd410e9

Browse files
clemfromspacePLNech
authored andcommitted
feat(engine) Add the auto-discover of the index.py files (#256)
- The `index.py` file is now auto-discovered when it exists - A new `register` decorator is now available close #229
1 parent 3e05c90 commit bd410e9

File tree

9 files changed

+71
-33
lines changed

9 files changed

+71
-33
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,3 @@ class OverrideSettingsTestCase(TestCase):
412412
```
413413

414414

415-

algoliasearch_django/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
http://www.algolia.com
44
"""
55

6+
from django.utils.module_loading import autodiscover_modules
7+
68
from . import models
79
from . import registration
810
from . import settings
@@ -40,4 +42,10 @@ def emit(self, record):
4042
pass
4143

4244

45+
def autodiscover():
46+
autodiscover_modules('index')
47+
48+
4349
logging.getLogger(__name__.split('.')[0]).addHandler(NullHandler())
50+
51+
default_app_config = 'algoliasearch_django.apps.AlgoliaConfig'

algoliasearch_django/apps.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AlgoliaConfig(AppConfig):
5+
"""Simple AppConfig which does not do automatic discovery."""
6+
7+
name = 'algoliasearch_django'
8+
9+
def ready(self):
10+
super(AlgoliaConfig, self).ready()
11+
self.module.autodiscover()

algoliasearch_django/decorators.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def register(model):
2+
"""
3+
Register the given model class and wrapped AlgoliaIndex class with the Algolia engine:
4+
5+
@register(Author)
6+
class AuthorIndex(AlgoliaIndex):
7+
pass
8+
9+
"""
10+
from algoliasearch_django import AlgoliaIndex, register
11+
12+
def _algolia_engine_wrapper(index_class):
13+
if not issubclass(index_class, AlgoliaIndex):
14+
raise ValueError('Wrapped class must subclass AlgoliaIndex.')
15+
16+
register(model, index_class)
17+
18+
return index_class
19+
return _algolia_engine_wrapper

tests/index.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from algoliasearch_django import register
2+
from algoliasearch_django.models import AlgoliaIndex
3+
from algoliasearch_django.decorators import register as register_decorator
4+
5+
from .models import User, Website
6+
7+
8+
@register_decorator(User)
9+
class UserIndex(AlgoliaIndex):
10+
pass
11+
12+
13+
class WebsiteIndex(AlgoliaIndex):
14+
pass
15+
16+
17+
register(Website, WebsiteIndex)

tests/test_commands.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from django.core.management import call_command
44

55
from algoliasearch_django import algolia_engine
6-
from algoliasearch_django import register
7-
from algoliasearch_django import unregister
86
from algoliasearch_django import get_adapter
97
from algoliasearch_django import clear_index
108

@@ -13,17 +11,10 @@
1311

1412

1513
class CommandsTestCase(TestCase):
16-
@classmethod
17-
def setUpClass(cls):
18-
register(Website, auto_indexing=False)
19-
register(User, auto_indexing=False)
20-
2114
@classmethod
2215
def tearDownClass(cls):
2316
algolia_engine.client.delete_index(get_adapter(User).index_name)
2417
algolia_engine.client.delete_index(get_adapter(Website).index_name)
25-
unregister(Website)
26-
unregister(User)
2718

2819
def setUp(self):
2920
# Create some records

tests/test_engine.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import six
2+
13
from django.conf import settings
2-
from django.test import TestCase, override_settings
4+
from django.test import TestCase
35

46
from algoliasearch_django import algolia_engine
57
from algoliasearch_django import AlgoliaIndex
@@ -27,6 +29,18 @@ def test_init_exception(self):
2729
with self.assertRaises(AlgoliaEngineError):
2830
AlgoliaEngine(settings=settings.ALGOLIA)
2931

32+
def test_auto_discover_indexes(self):
33+
"""Test that the `index` module was auto-discovered and the models registered"""
34+
35+
six.assertCountEqual(
36+
self,
37+
[
38+
User, # Registered using the `register` decorator
39+
Website, # Registered using the `register` method
40+
],
41+
algolia_engine.get_registered_models()
42+
)
43+
3044
def test_is_register(self):
3145
self.engine.register(Website)
3246
self.assertTrue(self.engine.is_registered(Website))
@@ -91,19 +105,3 @@ def test_unregister_exception(self):
91105

92106
with self.assertRaises(RegistrationError):
93107
self.engine.unregister(Website)
94-
95-
96-
class OverrideSettingsTestCase(TestCase):
97-
def setUp(self):
98-
with self.settings(ALGOLIA={
99-
'APPLICATION_ID': 'foo',
100-
'API_KEY': 'bar',
101-
'AUTO_INDEXING': False
102-
}):
103-
algolia_engine.reset(settings.ALGOLIA)
104-
105-
def tearDown(self):
106-
algolia_engine.reset(settings.ALGOLIA)
107-
108-
def test_no_indexing(self):
109-
self.assertFalse(algolia_engine.__dict__["_AlgoliaEngine__auto_indexing"], "AUTO_INDEXING should be disabled for this test.")

tests/test_signal.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
from algoliasearch_django import algolia_engine
66
from algoliasearch_django import get_adapter
7-
from algoliasearch_django import register
8-
from algoliasearch_django import unregister
97
from algoliasearch_django import raw_search
108
from algoliasearch_django import clear_index
119
from algoliasearch_django import update_records
@@ -14,14 +12,10 @@
1412

1513

1614
class SignalTestCase(TestCase):
17-
@classmethod
18-
def setUpClass(cls):
19-
register(Website)
2015

2116
@classmethod
2217
def tearDownClass(cls):
2318
algolia_engine.client.delete_index(get_adapter(Website).index_name)
24-
unregister(Website)
2519

2620
def tearDown(self):
2721
clear_index(Website)

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ skip_missing_interpreters = True
1111

1212
[testenv]
1313
deps =
14+
six
1415
django17: Django>=1.7,<1.8
1516
django18: Django>=1.8,<1.9
1617
django19: Django>=1.9,<1.10

0 commit comments

Comments
 (0)