algolia / algolia/algoliasearch-django

'disable_auto_indexing()' connects receiver even if AUTO_INDEXING is False

Open
#286 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
180
Forks
61
PR merge metrics
No merged PRs in 30d

Description

- Django version: 2.2.1
- Algolia Django integration version: 1.7.1
- Algolia Client Version: 1.20.0
- Language Version: Python 3.6.8

### Description
`disable_auto_indexing()` in `decorators.py` makes connection even if `AUTO_INDEXING = False` .

In `AUTO_INDEXING = False` environment, once `disable_auto_indexing()` is called, AlgoliaEngine will try to index when model saving.

```
def __exit__(self, exc_type, exc_value, traceback):
for model in self.models:
post_save.connect(
algolia_engine._AlgoliaEngine__post_save_receiver,
sender=model
)
pre_delete.connect(
algolia_engine._AlgoliaEngine__pre_delete_receiver,
sender=model
)
```

I think it needs condition if AUTO_INDEXING is True, like bellow.

```
def __exit__(self, exc_type, exc_value, traceback):
if some_auto_indexing # this
for model in self.models:
post_save.connect(
algolia_engine._AlgoliaEngine__post_save_receiver,
sender=model
)
pre_delete.connect(
algolia_engine._AlgoliaEngine__pre_delete_receiver,
sender=model
)
```

### Steps To Reproduce

settings.py
```
ALGOLIA = {"APPLICATION_ID": "foo", "API_KEY": "bar", "AUTO_INDEXING": False}
```

index.py
```
@register(SomeModel)
class SomeModelIndex():
...
```

sample.py
```
from algoliasearch_django.decorators import disable_auto_indexing
with disable_auto_indexing():
pass
SomeModel().save() # -> Error will be Occured
```

a part of error message
```
algoliasearch.helpers.AlgoliaException: Unreachable hosts: {'foo.algolia.net': "ConnectionError: HTTPSConnectionPool(host='foo.algolia.net', port=443): Max retries exceeded with url: /1/indexes/SomeModel/236 (Caused by NewConnectionError(': Failed to establish a new connection: [Errno -2] Name or service not known',))", 'foo-1.algolianet.com': "ConnectionError: HTTPSConnectionPool(host='foo-1.algolianet.com', port=443): Max retries exceeded with url: /1/indexes/SomeModel/236 (Caused by NewConnectionError(': Failed to establish a new connection: [Errno -2] Name or service not known',))", 'foo-3.algolianet.com': "ConnectionError: HTTPSConnectionPool(host='foo-3.algolianet.com', port=443): Max retries exceeded with url: /1/indexes/SomeModel/236 (Caused by NewConnectionError(': Failed to establish a new connection: [Errno -2] Name or service not known',))", 'foo-2.algolianet.com': "ConnectionError: HTTPSConnectionPool(host='foo-2.algolianet.com', port=443): Max retries exceeded with url: /1/indexes/SomeModel/236 (Caused by NewConnectionError(': Failed to establish a new connection: [Errno -2] Name or service not known',))"}
```

Contributor guide

Open the contributing guide

Research direction

Start in decorators.py, then review the AUTO_INDEXING setting shown in settings.py and the disable_auto_indexing() usage in sample.py. Reproduce the issue with AUTO_INDEXING set to False and confirm that exiting the context does not reconnect the model signals or trigger indexing when SomeModel is saved.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.