14
14
from readthedocs .projects .models import Feature , Project
15
15
from readthedocs .search import tasks
16
16
from readthedocs .search .api .pagination import SearchPagination
17
- from readthedocs .search .api .v2 .serializers import (
18
- PageSearchSerializer ,
19
- ProjectData ,
20
- VersionData ,
21
- )
17
+ from readthedocs .search .api .v2 .serializers import PageSearchSerializer
22
18
from readthedocs .search .faceted_search import PageSearch
23
19
from readthedocs .core .utils .extend import SettingsOverrideObject
24
20
@@ -81,45 +77,15 @@ def _validate_query_params(self):
81
77
raise ValidationError (errors )
82
78
83
79
@lru_cache (maxsize = 1 )
84
- def _get_all_projects_data (self ):
85
- """
86
- Return a dictionary of the project itself and all its subprojects.
87
-
88
- Example:
89
-
90
- .. code::
91
-
92
- {
93
- "requests": ProjectData(
94
- alias='alias',
95
- version=VersionData(
96
- "latest",
97
- "https://requests.readthedocs.io/en/latest/",
98
- ),
99
- ),
100
- "requests-oauth": ProjectData(
101
- alias=None,
102
- version=VersionData(
103
- "latest",
104
- "https://requests-oauth.readthedocs.io/en/latest/",
105
- ),
106
- ),
107
- }
108
-
109
- .. note:: The response is cached into the instance.
110
-
111
- :rtype: A dictionary of project slugs mapped to a `VersionData` object.
112
- """
80
+ def _get_projects_to_search (self ):
81
+ """Get all projects to search."""
113
82
main_version = self ._get_version ()
114
83
main_project = self ._get_project ()
115
84
116
85
if not self ._has_permission (self .request .user , main_version ):
117
- return {}
118
-
119
- projects_data = {
120
- main_project .slug : self ._get_project_data (main_project , main_version ),
121
- }
86
+ return []
122
87
88
+ projects_to_search = [(main_project , main_version )]
123
89
subprojects = Project .objects .filter (superprojects__parent_id = main_project .id )
124
90
for subproject in subprojects :
125
91
version = self ._get_project_version (
@@ -137,24 +103,9 @@ def _get_all_projects_data(self):
137
103
)
138
104
139
105
if version and self ._has_permission (self .request .user , version ):
140
- projects_data [subproject .slug ] = self ._get_project_data (
141
- subproject , version
142
- )
106
+ projects_to_search .append ((subproject , version ))
143
107
144
- return projects_data
145
-
146
- def _get_project_data (self , project , version ):
147
- """Build a `ProjectData` object given a project and its version."""
148
- url = project .get_docs_url (version_slug = version .slug )
149
- project_alias = project .superprojects .values_list ("alias" , flat = True ).first ()
150
- version_data = VersionData (
151
- slug = version .slug ,
152
- docs_url = url ,
153
- )
154
- return ProjectData (
155
- alias = project_alias ,
156
- version = version_data ,
157
- )
108
+ return projects_to_search
158
109
159
110
def _get_project_version (self , project , version_slug , include_hidden = True ):
160
111
"""
@@ -220,8 +171,8 @@ def get_queryset(self):
220
171
is compatible with DRF's paginator.
221
172
"""
222
173
projects = {
223
- project : project_data . version .slug
224
- for project , project_data in self ._get_all_projects_data (). items ()
174
+ project . slug : version .slug
175
+ for project , version in self ._get_projects_to_search ()
225
176
}
226
177
# Check to avoid searching all projects in case it's empty.
227
178
if not projects :
@@ -237,11 +188,6 @@ def get_queryset(self):
237
188
)
238
189
return queryset
239
190
240
- def get_serializer_context (self ):
241
- context = super ().get_serializer_context ()
242
- context ["projects_data" ] = self ._get_all_projects_data ()
243
- return context
244
-
245
191
def get (self , request , * args , ** kwargs ):
246
192
self ._validate_query_params ()
247
193
result = self .list ()
@@ -256,7 +202,9 @@ def list(self):
256
202
self .request ,
257
203
view = self ,
258
204
)
259
- serializer = self .get_serializer (page , many = True )
205
+ serializer = self .get_serializer (
206
+ page , many = True , projects = self ._get_projects_to_search ()
207
+ )
260
208
return self .paginator .get_paginated_response (serializer .data )
261
209
262
210
0 commit comments