16
16
from readthedocs .core .mixins import CDNCacheControlMixin
17
17
from readthedocs .core .resolver import resolver
18
18
from readthedocs .core .unresolver import UnresolverError , unresolver
19
+ from readthedocs .projects .models import Feature
19
20
20
21
log = structlog .get_logger (__name__ ) # noqa
21
22
@@ -173,11 +174,17 @@ def _v0(self, project, version, build, filename):
173
174
It tries to follow some similarity with the APIv3 for already-known resources
174
175
(Project, Version, Build, etc).
175
176
"""
176
- versions_active_built = (
177
+ versions_active_built_not_hidden = (
177
178
Version .internal .public (project = project , only_active = True , only_built = True )
179
+ .exclude (hidden = True )
178
180
.only ("slug" )
179
181
.order_by ("slug" )
180
182
)
183
+ project_translations = (
184
+ project .translations .all ().only ("language" ).order_by ("language" )
185
+ )
186
+ # Make one DB query here and then check on Python code
187
+ project_features = project .features .all ().values_list ("feature_id" , flat = True )
181
188
182
189
data = {
183
190
"comment" : (
@@ -208,30 +215,33 @@ def _v0(self, project, version, build, filename):
208
215
# serializer than the keys ``project``, ``version`` and ``build`` from the top level.
209
216
"addons" : {
210
217
"analytics" : {
211
- "enabled" : True ,
218
+ "enabled" : Feature .ADDONS_ANALYTICS_DISABLED
219
+ not in project_features ,
212
220
# TODO: consider adding this field into the ProjectSerializer itself.
213
221
# NOTE: it seems we are removing this feature,
214
222
# so we may not need the ``code`` attribute here
215
223
# https://github.com/readthedocs/readthedocs.org/issues/9530
216
224
"code" : project .analytics_code ,
217
225
},
218
226
"external_version_warning" : {
219
- "enabled" : True ,
227
+ "enabled" : Feature .ADDONS_EXTERNAL_VERSION_WARNING_DISABLED
228
+ not in project_features ,
220
229
# NOTE: I think we are moving away from these selectors
221
230
# since we are doing floating noticications now.
222
231
# "query_selector": "[role=main]",
223
232
},
224
233
"non_latest_version_warning" : {
225
- "enabled" : True ,
234
+ "enabled" : Feature .ADDONS_NON_LATEST_VERSION_WARNING_DISABLED
235
+ not in project_features ,
226
236
# NOTE: I think we are moving away from these selectors
227
237
# since we are doing floating noticications now.
228
238
# "query_selector": "[role=main]",
229
239
"versions" : list (
230
- versions_active_built .values_list ("slug" , flat = True )
240
+ versions_active_built_not_hidden .values_list ("slug" , flat = True )
231
241
),
232
242
},
233
243
"doc_diff" : {
234
- "enabled" : True ,
244
+ "enabled" : Feature . ADDONS_DOC_DIFF_DISABLED not in project_features ,
235
245
# "http://test-builds-local.devthedocs.org/en/latest/index.html"
236
246
"base_url" : resolver .resolve (
237
247
project = project ,
@@ -250,26 +260,50 @@ def _v0(self, project, version, build, filename):
250
260
"base_page" : "" ,
251
261
},
252
262
"flyout" : {
253
- "translations" : [],
263
+ "enabled" : Feature .ADDONS_FLYOUT_DISABLED not in project_features ,
264
+ "translations" : [
265
+ {
266
+ # TODO: name this field "display_name"
267
+ "slug" : translation .language ,
268
+ "url" : f"/{ translation .language } /" ,
269
+ }
270
+ for translation in project_translations
271
+ ],
254
272
"versions" : [
255
273
{
274
+ # TODO: name this field "display_name"
256
275
"slug" : version .slug ,
257
276
"url" : f"/{ project .language } /{ version .slug } /" ,
258
277
}
259
- for version in versions_active_built
278
+ for version in versions_active_built_not_hidden
260
279
],
261
- "downloads" : [],
262
- # TODO: get this values properly
263
- "vcs" : {
264
- "url" : "https://github.com" ,
265
- "username" : "readthedocs" ,
266
- "repository" : "test-builds" ,
267
- "branch" : version .identifier if version else None ,
268
- "filepath" : "/docs/index.rst" ,
269
- },
280
+ "downloads" : [
281
+ {
282
+ # TODO: name this field "display_name"
283
+ "name" : name ,
284
+ "url" : url ,
285
+ }
286
+ for name , url in version .get_downloads (pretty = True ).items ()
287
+ ],
288
+ # TODO: find a way to get this data in a reliably way.
289
+ # We don't have a simple way to map a URL to a file in the repository.
290
+ # This feature may be deprecated/removed in this implementation since it relies
291
+ # on data injected at build time and sent as `docroot=`, `source_suffix=` and `page=`.
292
+ # Example URL:
293
+ # /_/api/v2/footer_html/?project=weblate&version=latest&page=index&theme=furo&docroot=/docs/&source_suffix=.rst
294
+ # Data injected at:
295
+ # https://github.com/rtfd/readthedocs-sphinx-ext/blob/7c60d1646c12ac0b83d61abfbdd5bcd77d324124/readthedocs_ext/_templates/readthedocs-insert.html.tmpl#L23
296
+ #
297
+ # "vcs": {
298
+ # "url": "https://github.com",
299
+ # "username": "readthedocs",
300
+ # "repository": "test-builds",
301
+ # "branch": version.identifier if version else None,
302
+ # "filepath": "/docs/index.rst",
303
+ # },
270
304
},
271
305
"search" : {
272
- "enabled" : True ,
306
+ "enabled" : Feature . ADDONS_SEARCH_DISABLED not in project_features ,
273
307
"project" : project .slug ,
274
308
"version" : version .slug if version else None ,
275
309
"api_endpoint" : "/_/api/v3/search/" ,
@@ -310,7 +344,8 @@ def _v0(self, project, version, build, filename):
310
344
data ["addons" ].update (
311
345
{
312
346
"ethicalads" : {
313
- "enabled" : True ,
347
+ "enabled" : Feature .ADDONS_ETHICALADS_DISABLED
348
+ not in project_features ,
314
349
# NOTE: this endpoint is not authenticated, the user checks are done over an annonymous user for now
315
350
#
316
351
# NOTE: it requires ``settings.USE_PROMOS=True`` to return ``ad_free=false`` here
0 commit comments