Skip to content

Commit 6816afe

Browse files
authored
Addons: prepare the backend for the new flyout (#10650)
* Addons: prepare the backend for the new flyout Return `addons.flyout.translations` and `addons.flyout.downloads`. Remove `addons.flyout.vcs` for now because we don't have a reliable way to implement it without injecting data into the build which we don't want to do anymore. Related readthedocs/addons#86 * Addons: create one feature flag per addon This allows us to have full control over each project. * Addons: decide whether or not enable the addon via feature flag * Test: update response for Addons V0 * Typo * Flyout: don't show hidden versions
1 parent 0a93d06 commit 6816afe

File tree

3 files changed

+106
-30
lines changed

3 files changed

+106
-30
lines changed

readthedocs/projects/models.py

+50-3
Original file line numberDiff line numberDiff line change
@@ -1835,9 +1835,26 @@ def add_features(sender, **kwargs):
18351835
INDEX_FROM_HTML_FILES = 'index_from_html_files'
18361836

18371837
# Build related features
1838-
HOSTING_INTEGRATIONS = "hosting_integrations"
18391838
SCALE_IN_PROTECTION = "scale_in_prtection"
18401839

1840+
# Addons related features
1841+
HOSTING_INTEGRATIONS = "hosting_integrations"
1842+
# NOTE: this is mainly temporal while we are rolling these features out.
1843+
# The idea here is to have more control over particular projects and do some testing.
1844+
# All these features will be enabled by default to all projects,
1845+
# and we can disable them if we want to
1846+
ADDONS_ANALYTICS_DISABLED = "addons_analytics_disabled"
1847+
ADDONS_DOC_DIFF_DISABLED = "addons_doc_diff_disabled"
1848+
ADDONS_ETHICALADS_DISABLED = "addons_ethicalads_disabled"
1849+
ADDONS_EXTERNAL_VERSION_WARNING_DISABLED = (
1850+
"addons_external_version_warning_disabled"
1851+
)
1852+
ADDONS_FLYOUT_DISABLED = "addons_flyout_disabled"
1853+
ADDONS_NON_LATEST_VERSION_WARNING_DISABLED = (
1854+
"addons_non_latest_version_warning_disabled"
1855+
)
1856+
ADDONS_SEARCH_DISABLED = "addons_search_disabled"
1857+
18411858
FEATURES = (
18421859
(
18431860
MKDOCS_THEME_RTD,
@@ -1949,15 +1966,45 @@ def add_features(sender, **kwargs):
19491966
"sources"
19501967
),
19511968
),
1969+
# Build related features.
1970+
(
1971+
SCALE_IN_PROTECTION,
1972+
_("Build: Set scale-in protection before/after building."),
1973+
),
1974+
# Addons related features.
19521975
(
19531976
HOSTING_INTEGRATIONS,
19541977
_(
19551978
"Proxito: Inject 'readthedocs-addons.js' as <script> HTML tag in responses."
19561979
),
19571980
),
19581981
(
1959-
SCALE_IN_PROTECTION,
1960-
_("Build: Set scale-in protection before/after building."),
1982+
ADDONS_ANALYTICS_DISABLED,
1983+
_("Addons: Disable Analytics."),
1984+
),
1985+
(
1986+
ADDONS_DOC_DIFF_DISABLED,
1987+
_("Addons: Disable Doc Diff."),
1988+
),
1989+
(
1990+
ADDONS_ETHICALADS_DISABLED,
1991+
_("Addons: Disable EthicalAds."),
1992+
),
1993+
(
1994+
ADDONS_EXTERNAL_VERSION_WARNING_DISABLED,
1995+
_("Addons: Disable External version warning."),
1996+
),
1997+
(
1998+
ADDONS_FLYOUT_DISABLED,
1999+
_("Addons: Disable Flyout."),
2000+
),
2001+
(
2002+
ADDONS_NON_LATEST_VERSION_WARNING_DISABLED,
2003+
_("Addons: Disable Non latest version warning."),
2004+
),
2005+
(
2006+
ADDONS_SEARCH_DISABLED,
2007+
_("Addons: Disable Search."),
19612008
),
19622009
)
19632010

readthedocs/proxito/tests/responses/v0.json

+2-8
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,10 @@
9595
"base_page": ""
9696
},
9797
"flyout": {
98+
"enabled": true,
9899
"translations": [],
99100
"versions": [{ "slug": "latest", "url": "/en/latest/" }],
100-
"downloads": [],
101-
"vcs": {
102-
"url": "https://github.com",
103-
"username": "readthedocs",
104-
"repository": "test-builds",
105-
"branch": "a1b2c3",
106-
"filepath": "/docs/index.rst"
107-
}
101+
"downloads": []
108102
},
109103
"search": {
110104
"enabled": true,

readthedocs/proxito/views/hosting.py

+54-19
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from readthedocs.core.mixins import CDNCacheControlMixin
1717
from readthedocs.core.resolver import resolver
1818
from readthedocs.core.unresolver import UnresolverError, unresolver
19+
from readthedocs.projects.models import Feature
1920

2021
log = structlog.get_logger(__name__) # noqa
2122

@@ -173,11 +174,17 @@ def _v0(self, project, version, build, filename):
173174
It tries to follow some similarity with the APIv3 for already-known resources
174175
(Project, Version, Build, etc).
175176
"""
176-
versions_active_built = (
177+
versions_active_built_not_hidden = (
177178
Version.internal.public(project=project, only_active=True, only_built=True)
179+
.exclude(hidden=True)
178180
.only("slug")
179181
.order_by("slug")
180182
)
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)
181188

182189
data = {
183190
"comment": (
@@ -208,30 +215,33 @@ def _v0(self, project, version, build, filename):
208215
# serializer than the keys ``project``, ``version`` and ``build`` from the top level.
209216
"addons": {
210217
"analytics": {
211-
"enabled": True,
218+
"enabled": Feature.ADDONS_ANALYTICS_DISABLED
219+
not in project_features,
212220
# TODO: consider adding this field into the ProjectSerializer itself.
213221
# NOTE: it seems we are removing this feature,
214222
# so we may not need the ``code`` attribute here
215223
# https://github.com/readthedocs/readthedocs.org/issues/9530
216224
"code": project.analytics_code,
217225
},
218226
"external_version_warning": {
219-
"enabled": True,
227+
"enabled": Feature.ADDONS_EXTERNAL_VERSION_WARNING_DISABLED
228+
not in project_features,
220229
# NOTE: I think we are moving away from these selectors
221230
# since we are doing floating noticications now.
222231
# "query_selector": "[role=main]",
223232
},
224233
"non_latest_version_warning": {
225-
"enabled": True,
234+
"enabled": Feature.ADDONS_NON_LATEST_VERSION_WARNING_DISABLED
235+
not in project_features,
226236
# NOTE: I think we are moving away from these selectors
227237
# since we are doing floating noticications now.
228238
# "query_selector": "[role=main]",
229239
"versions": list(
230-
versions_active_built.values_list("slug", flat=True)
240+
versions_active_built_not_hidden.values_list("slug", flat=True)
231241
),
232242
},
233243
"doc_diff": {
234-
"enabled": True,
244+
"enabled": Feature.ADDONS_DOC_DIFF_DISABLED not in project_features,
235245
# "http://test-builds-local.devthedocs.org/en/latest/index.html"
236246
"base_url": resolver.resolve(
237247
project=project,
@@ -250,26 +260,50 @@ def _v0(self, project, version, build, filename):
250260
"base_page": "",
251261
},
252262
"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+
],
254272
"versions": [
255273
{
274+
# TODO: name this field "display_name"
256275
"slug": version.slug,
257276
"url": f"/{project.language}/{version.slug}/",
258277
}
259-
for version in versions_active_built
278+
for version in versions_active_built_not_hidden
260279
],
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+
# },
270304
},
271305
"search": {
272-
"enabled": True,
306+
"enabled": Feature.ADDONS_SEARCH_DISABLED not in project_features,
273307
"project": project.slug,
274308
"version": version.slug if version else None,
275309
"api_endpoint": "/_/api/v3/search/",
@@ -310,7 +344,8 @@ def _v0(self, project, version, build, filename):
310344
data["addons"].update(
311345
{
312346
"ethicalads": {
313-
"enabled": True,
347+
"enabled": Feature.ADDONS_ETHICALADS_DISABLED
348+
not in project_features,
314349
# NOTE: this endpoint is not authenticated, the user checks are done over an annonymous user for now
315350
#
316351
# NOTE: it requires ``settings.USE_PROMOS=True`` to return ``ad_free=false`` here

0 commit comments

Comments
 (0)