Skip to content

Commit 2319268

Browse files
authored
Addons: update projects.translations API response (#11361)
Return "source language project" in `projects.translations` when accessing a translation of the "source language project". Closes #11292
1 parent a78efaa commit 2319268

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

readthedocs/proxito/tests/test_hosting.py

+23
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ def test_flyout_translations(self):
300300
)
301301
assert r.status_code == 200
302302

303+
# Hitting the English version of the docs, will return Japanese as translation
303304
assert len(r.json()["projects"]["translations"]) == 1
304305
assert r.json()["projects"]["translations"][0]["slug"] == "translation"
305306
assert r.json()["projects"]["translations"][0]["language"]["code"] == "ja"
@@ -308,6 +309,28 @@ def test_flyout_translations(self):
308309
== "https://project.dev.readthedocs.io/ja/latest/"
309310
)
310311

312+
# Hitting the Japanese version of the docs, will return English as translation
313+
r = self.client.get(
314+
reverse("proxito_readthedocs_docs_addons"),
315+
{
316+
"url": "https://project.dev.readthedocs.io/ja/latest/",
317+
"client-version": "0.6.0",
318+
"api-version": "1.0.0",
319+
},
320+
secure=True,
321+
headers={
322+
"host": "project.dev.readthedocs.io",
323+
},
324+
)
325+
assert r.status_code == 200
326+
assert len(r.json()["projects"]["translations"]) == 1
327+
assert r.json()["projects"]["translations"][0]["slug"] == "project"
328+
assert r.json()["projects"]["translations"][0]["language"]["code"] == "en"
329+
assert (
330+
r.json()["projects"]["translations"][0]["urls"]["documentation"]
331+
== "https://project.dev.readthedocs.io/en/latest/"
332+
)
333+
311334
def test_flyout_downloads(self):
312335
fixture.get(
313336
Version,

readthedocs/proxito/views/hosting.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,15 @@ def _v1(self, project, version, build, filename, url, user):
348348
)
349349

350350
main_project = project.main_language_project or project
351-
project_translations = main_project.translations.all().order_by("language")
351+
352+
# Exclude the current project since we don't want to return itself as a translation
353+
project_translations = main_project.translations.all().exclude(
354+
slug=project.slug
355+
)
356+
# Include main project as translation if the current project is one of the translations
357+
if project != main_project:
358+
project_translations |= Project.objects.filter(slug=main_project.slug)
359+
project_translations = project_translations.order_by("language")
352360

353361
data = {
354362
"api_version": "1",

0 commit comments

Comments
 (0)