Skip to content

Commit 765ebec

Browse files
committed
Search: refactor api view
Mainly this was done to allow us support searching on different versions of subprojects. Currently, we assume we search the same version for all subprojects. - _get_all_projects_data and _get_all_projects were merged into just one method. And instead of returning a list of projects we return a dictionary of project slugs mapped to a VersionData object. - There is some duplication in .com. `_has_permission` and `_get_subproject_versions_queryset` are overridden in .com. - ProjectData was renamed to VersionData since it has more attributes related to a version than a project.
1 parent 9cacb2c commit 765ebec

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

readthedocs/search/api.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,16 @@ def _get_all_projects_data(self):
229229
)
230230
for project in subprojects:
231231
version = self._get_subproject_version(
232-
version_slug=main_version,
232+
version_slug=main_version.slug,
233233
subproject=project,
234234
)
235+
# Fallback to the default version of the subproject.
236+
if not version and project.default_version:
237+
version = self._get_subproject_version(
238+
version_slug=project.default_version,
239+
subproject=project,
240+
)
241+
235242
if version and self._has_permission(self.request.user, version):
236243
url = project.get_docs_url(version_slug=version.slug)
237244
projects_data[project.slug] = VersionData(
@@ -290,22 +297,20 @@ def get_queryset(self):
290297
calling ``search.execute().hits``. This is why an DSL search object
291298
is compatible with DRF's paginator.
292299
"""
293-
filters = {}
294-
filters['project'] = list(self._get_all_projects_data().keys())
295-
filters['version'] = self._get_version().slug
300+
projects = {
301+
project: version.slug
302+
for project, version in self._get_all_projects_data().items()
303+
}
296304

297-
# Check to avoid searching all projects in case these filters are empty.
298-
if not filters['project']:
299-
log.info('Unable to find a project to search')
300-
return []
301-
if not filters['version']:
305+
# Check to avoid searching all projects in case it's empty.
306+
if not projects:
302307
log.info('Unable to find a version to search')
303308
return []
304309

305310
query = self.request.query_params['q']
306311
queryset = PageSearch(
307312
query=query,
308-
filters=filters,
313+
projects=projects,
309314
user=self.request.user,
310315
# We use a permission class to control authorization
311316
filter_by_user=False,

0 commit comments

Comments
 (0)