diff --git a/readthedocs/proxito/tests/test_hosting.py b/readthedocs/proxito/tests/test_hosting.py index fdf9de4f151..0d577a0db4f 100644 --- a/readthedocs/proxito/tests/test_hosting.py +++ b/readthedocs/proxito/tests/test_hosting.py @@ -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 @@ -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/", @@ -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"), { @@ -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"), { @@ -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"), { @@ -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"), { diff --git a/readthedocs/proxito/views/hosting.py b/readthedocs/proxito/views/hosting.py index a5d52e85119..71f7d89ee50 100644 --- a/readthedocs/proxito/views/hosting.py +++ b/readthedocs/proxito/views/hosting.py @@ -1,5 +1,6 @@ """Views for hosting features.""" +import itertools from functools import lru_cache import packaging @@ -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._enabled`` to decide if enabled # @@ -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": {