Skip to content

Commit 3aff599

Browse files
committed
✨(backend) refactor indexation signals and fix circular import issues
Signed-off-by: Fabre Florian <[email protected]>
1 parent e6468f6 commit 3aff599

File tree

7 files changed

+56
-39
lines changed

7 files changed

+56
-39
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ and this project adheres to
3232
### Added
3333

3434
- ✨(api) add API route to fetch document content #1206
35+
- ✨(backend) add async indexation of documents with Find API #1276
36+
- ✨(api) add API route to search indexed documents in Find #1276
3537

3638
### Changed
3739

src/backend/core/apps.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
"""Impress Core application"""
2-
# from django.apps import AppConfig
3-
# from django.utils.translation import gettext_lazy as _
42

3+
from django.apps import AppConfig
4+
from django.utils.translation import gettext_lazy as _
55

6-
# class CoreConfig(AppConfig):
7-
# """Configuration class for the impress core app."""
86

9-
# name = "core"
10-
# app_label = "core"
11-
# verbose_name = _("impress core application")
7+
class CoreConfig(AppConfig):
8+
"""Configuration class for the impress core app."""
9+
10+
name = "core"
11+
app_label = "core"
12+
verbose_name = _("Impress core application")
13+
14+
def ready(self):
15+
"""
16+
Import signals when the app is ready.
17+
"""
18+
# pylint: disable=import-outside-toplevel, unused-import
19+
from . import signals # noqa: PLC0415

src/backend/core/models.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
from django.core.files.storage import default_storage
2121
from django.core.mail import send_mail
2222
from django.db import models, transaction
23-
from django.db.models import signals
2423
from django.db.models.functions import Left, Length
25-
from django.dispatch import receiver
2624
from django.template.loader import render_to_string
2725
from django.utils import timezone
2826
from django.utils.functional import cached_property
@@ -41,7 +39,6 @@
4139
RoleChoices,
4240
get_equivalent_link_definition,
4341
)
44-
from .tasks.find import trigger_document_indexer
4542
from .validators import sub_validator
4643

4744
logger = getLogger(__name__)
@@ -950,16 +947,6 @@ def restore(self):
950947
)
951948

952949

953-
@receiver(signals.post_save, sender=Document)
954-
def document_post_save(sender, instance, **kwargs): # pylint: disable=unused-argument
955-
"""
956-
Asynchronous call to the document indexer at the end of the transaction.
957-
Note : Within the transaction we can have an empty content and a serialization
958-
error.
959-
"""
960-
trigger_document_indexer(instance, on_commit=True)
961-
962-
963950
class LinkTrace(BaseModel):
964951
"""
965952
Relation model to trace accesses to a document via a link by a logged-in user.
@@ -1185,15 +1172,6 @@ def get_abilities(self, user):
11851172
}
11861173

11871174

1188-
@receiver(signals.post_save, sender=DocumentAccess)
1189-
def document_access_post_save(sender, instance, created, **kwargs): # pylint: disable=unused-argument
1190-
"""
1191-
Asynchronous call to the document indexer at the end of the transaction.
1192-
"""
1193-
if not created:
1194-
trigger_document_indexer(instance.document, on_commit=True)
1195-
1196-
11971175
class DocumentAskForAccess(BaseModel):
11981176
"""Relation model to ask for access to a document."""
11991177

src/backend/core/signals.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Declare and configure the signals for the impress core application
3+
"""
4+
5+
from django.db.models import signals
6+
from django.dispatch import receiver
7+
8+
from . import models
9+
from .tasks.find import trigger_document_indexer
10+
11+
12+
@receiver(signals.post_save, sender=models.Document)
13+
def document_post_save(sender, instance, **kwargs): # pylint: disable=unused-argument
14+
"""
15+
Asynchronous call to the document indexer at the end of the transaction.
16+
Note : Within the transaction we can have an empty content and a serialization
17+
error.
18+
"""
19+
trigger_document_indexer(instance, on_commit=True)
20+
21+
22+
@receiver(signals.post_save, sender=models.DocumentAccess)
23+
def document_access_post_save(sender, instance, created, **kwargs): # pylint: disable=unused-argument
24+
"""
25+
Asynchronous call to the document indexer at the end of the transaction.
26+
"""
27+
if not created:
28+
trigger_document_indexer(instance.document, on_commit=True)

src/backend/core/tasks/find.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77
from django.core.cache import cache
88
from django.db import transaction
99

10-
from core import models
11-
from core.services.search_indexers import (
12-
get_batch_accesses_by_users_and_teams,
13-
get_document_indexer_class,
14-
)
15-
1610
from impress.celery_app import app
1711

1812
logger = getLogger(__file__)
@@ -52,6 +46,13 @@ def document_indexer_task(document_id):
5246
logger.info("Skip document %s indexation", document_id)
5347
return
5448

49+
# pylint: disable=import-outside-toplevel
50+
from core import models # noqa: PLC0415
51+
from core.services.search_indexers import ( # noqa: PLC0415
52+
get_batch_accesses_by_users_and_teams,
53+
get_document_indexer_class,
54+
)
55+
5556
doc = models.Document.objects.get(pk=document_id)
5657
indexer = get_document_indexer_class()()
5758
accesses = get_batch_accesses_by_users_and_teams((doc.path,))

src/backend/core/tests/test_models_documents.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
"""
44
# pylint: disable=too-many-lines
55

6-
from operator import itemgetter
76
import random
87
import smtplib
98
import time
109
from logging import Logger
10+
from operator import itemgetter
1111
from unittest import mock
1212

1313
from django.contrib.auth.models import AnonymousUser
@@ -1445,13 +1445,13 @@ def test_models_documents_post_save_indexer(mock_push, settings):
14451445

14461446
indexer = FindDocumentIndexer()
14471447

1448-
assert sorted(data, key=itemgetter('id')) == sorted(
1448+
assert sorted(data, key=itemgetter("id")) == sorted(
14491449
[
14501450
indexer.serialize_document(doc1, accesses),
14511451
indexer.serialize_document(doc2, accesses),
14521452
indexer.serialize_document(doc3, accesses),
14531453
],
1454-
key=itemgetter('id'),
1454+
key=itemgetter("id"),
14551455
)
14561456

14571457
# The debounce counters should be reset

src/backend/core/tests/test_services_search_indexers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ def format_response(self, data: dict):
3636
return {}
3737

3838

39-
4039
@pytest.fixture(name="fake_indexer_settings")
4140
def fake_indexer_settings_fixture(settings):
4241
"""Fixture to switch the indexer to the FakeDocumentIndexer."""
@@ -103,6 +102,7 @@ def test_services_search_indexer_class(fake_indexer_settings):
103102
"core.tests.test_services_search_indexers.FakeDocumentIndexer"
104103
)
105104

105+
106106
def test_services_search_indexer_url_is_none(settings):
107107
"""
108108
Indexer should raise RuntimeError if SEARCH_INDEXER_URL is None or empty.

0 commit comments

Comments
 (0)