8
8
from django .conf import settings
9
9
from django .contrib .auth .models import AnonymousUser
10
10
from django .core .exceptions import ImproperlyConfigured
11
+ from django .db .models import Subquery
11
12
from django .utils .module_loading import import_string
12
13
13
14
import requests
17
18
logger = logging .getLogger (__name__ )
18
19
19
20
21
+ @cache
22
+ def is_document_indexer_configured () -> bool :
23
+ """Returns true if the indexer service is enabled and properly configured."""
24
+ try :
25
+ get_document_indexer_class ()
26
+ except ImproperlyConfigured :
27
+ return False
28
+
29
+ return True
30
+
31
+
20
32
@cache
21
33
def get_document_indexer_class () -> "BaseDocumentIndexer" :
22
34
"""Return the indexer backend class based on the settings."""
@@ -65,7 +77,7 @@ def get_batch_accesses_by_users_and_teams(paths):
65
77
return dict (access_by_document_path )
66
78
67
79
68
- def get_visited_document_ids_of (user ):
80
+ def get_visited_document_ids_of (queryset , user ):
69
81
"""
70
82
Returns the ids of the documents that have a linktrace to the user and NOT owned.
71
83
It will be use to limit the opensearch responses to the public documents already
@@ -74,11 +86,18 @@ def get_visited_document_ids_of(user):
74
86
if isinstance (user , AnonymousUser ):
75
87
return []
76
88
77
- qs = models .LinkTrace .objects .filter (user = user ).exclude (
78
- document__accesses__user = user ,
89
+ qs = models .LinkTrace .objects .filter (user = user )
90
+
91
+ docs = (
92
+ queryset .exclude (accesses__user = user )
93
+ .filter (
94
+ deleted_at__isnull = True ,
95
+ ancestors_deleted_at__isnull = True ,
96
+ )
97
+ .filter (pk__in = Subquery (qs .values ("document_id" )))
79
98
)
80
99
81
- return list ({str (id ) for id in qs .values_list ("document_id " , flat = True )})
100
+ return list ({str (id ) for id in docs .values_list ("pk " , flat = True )})
82
101
83
102
84
103
class BaseDocumentIndexer (ABC ):
@@ -159,22 +178,23 @@ def push(self, data):
159
178
Must be implemented by subclasses.
160
179
"""
161
180
162
- def search (self , text , user , token ):
181
+ # pylint: disable-next=too-many-arguments,too-many-positional-arguments
182
+ def search (self , text , token , visited = (), page = 1 , page_size = 50 ):
163
183
"""
164
184
Search for documents in Find app.
165
185
"""
166
- visited_ids = get_visited_document_ids_of (user )
167
-
168
186
response = self .search_query (
169
187
data = {
170
188
"q" : text ,
171
- "visited" : visited_ids ,
189
+ "visited" : visited ,
172
190
"services" : ["docs" ],
191
+ "page_number" : page ,
192
+ "page_size" : page_size ,
173
193
},
174
194
token = token ,
175
195
)
176
196
177
- return self . format_response ( response )
197
+ return [ d [ "_id" ] for d in response ]
178
198
179
199
@abstractmethod
180
200
def search_query (self , data , token ) -> dict :
@@ -184,14 +204,6 @@ def search_query(self, data, token) -> dict:
184
204
Must be implemented by subclasses.
185
205
"""
186
206
187
- @abstractmethod
188
- def format_response (self , data : dict ):
189
- """
190
- Convert the JSON response from Find app as document queryset.
191
-
192
- Must be implemented by subclasses.
193
- """
194
-
195
207
196
208
class FindDocumentIndexer (BaseDocumentIndexer ):
197
209
"""
@@ -253,12 +265,6 @@ def search_query(self, data, token) -> requests.Response:
253
265
logger .error ("HTTPError: %s" , e )
254
266
raise
255
267
256
- def format_response (self , data : dict ):
257
- """
258
- Retrieve documents ids from Find app response and return a queryset.
259
- """
260
- return models .Document .objects .filter (pk__in = [d ["_id" ] for d in data ])
261
-
262
268
def push (self , data ):
263
269
"""
264
270
Push a batch of documents to the Find backend.
0 commit comments