Skip to content

Commit

Permalink
Addons: get translation from main project (#10952)
Browse files Browse the repository at this point in the history
* Addons: get translation from main project

Closes readthedocs/addons#218

* Addons: return original language as flyout's translation

Include the main project's language in `addons.flyout.translations` in the first
position of the list so it's shown at the beginning.
This follows the actual behavior.
However, we may want to change the ordering to be A-Z.

Closes readthedocs/addons#218

* Test: update addons flyout languages to match changes

* Test: adapt more tests
  • Loading branch information
humitos committed Jan 3, 2024
1 parent 06abe5d commit 12ac09f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
13 changes: 9 additions & 4 deletions readthedocs/proxito/tests/test_hosting.py
Expand Up @@ -317,6 +317,7 @@ def test_flyout_translations(self):
assert r.status_code == 200

expected = [
{"slug": "en", "url": "https://project.dev.readthedocs.io/en/latest/"},
{"slug": "ja", "url": "https://project.dev.readthedocs.io/ja/latest/"},
]
assert r.json()["addons"]["flyout"]["translations"] == expected
Expand Down Expand Up @@ -540,6 +541,10 @@ def test_flyout_subproject_urls(self):
assert r.json()["addons"]["flyout"]["versions"] == expected_versions

expected_translations = [
{
"slug": "en",
"url": "https://project.dev.readthedocs.io/projects/subproject/en/latest/",
},
{
"slug": "es",
"url": "https://project.dev.readthedocs.io/projects/subproject/es/latest/",
Expand Down Expand Up @@ -673,7 +678,7 @@ def test_number_of_queries_project_version_slug(self):
active=True,
)

with self.assertNumQueries(16):
with self.assertNumQueries(17):
r = self.client.get(
reverse("proxito_readthedocs_docs_addons"),
{
Expand Down Expand Up @@ -702,7 +707,7 @@ def test_number_of_queries_url(self):
active=True,
)

with self.assertNumQueries(16):
with self.assertNumQueries(17):
r = self.client.get(
reverse("proxito_readthedocs_docs_addons"),
{
Expand Down Expand Up @@ -738,7 +743,7 @@ def test_number_of_queries_url_subproject(self):
active=True,
)

with self.assertNumQueries(20):
with self.assertNumQueries(21):
r = self.client.get(
reverse("proxito_readthedocs_docs_addons"),
{
Expand All @@ -764,7 +769,7 @@ def test_number_of_queries_url_translations(self):
language=language,
)

with self.assertNumQueries(20):
with self.assertNumQueries(21):
r = self.client.get(
reverse("proxito_readthedocs_docs_addons"),
{
Expand Down
13 changes: 10 additions & 3 deletions readthedocs/proxito/views/hosting.py
@@ -1,5 +1,6 @@
"""Views for hosting features."""

import itertools
from functools import lru_cache

import packaging
Expand Down Expand Up @@ -268,9 +269,17 @@ def _v0(self, project, version, build, filename, url, user):
if version:
version_downloads = version.get_downloads(pretty=True).items()

main_project = project.main_language_project or project
project_translations = (
project.translations.all().only("language").order_by("language")
main_project.translations.all().only("language").order_by("language")
)
if project_translations.exists():
# Always prefix the list of translations with the main project's language,
# when there are translations present.
# Example: a project with Russian and Spanish translations will be showns as:
# en (original), es, ru
project_translations = itertools.chain([main_project], project_translations)

# Make one DB query here and then check on Python code
# TODO: make usage of ``Project.addons.<name>_enabled`` to decide if enabled
#
Expand All @@ -288,8 +297,6 @@ def _v0(self, project, version, build, filename, url, user):
" AND IT'S GOING TO CHANGE COMPLETELY -- DO NOT USE IT!"
),
"projects": {
# TODO: return the "parent" project here when the "current"
# project is a subproject/translation.
"current": ProjectSerializerNoLinks(project).data,
},
"versions": {
Expand Down

0 comments on commit 12ac09f

Please sign in to comment.