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