Repository: readthedocs/readthedocs.org

Issue:
Addons: don't return current translation and include source language in `projects.translations` field
Currently, `project.translations` is returning all the translations from `main_project`. I realized that we don't always want this. This is only useful when browsing the _source_ language of the documentation. However, when reading any of its translations `project.translations` **should not show** the current translation (the one the user is reading) and **should show** the _source_ language as translation as well.

### Example

If the project has English (source language), Spanish and Japanese translations, `projects.translations` should contain:

- Spanish and Japanese, when hitting the English translation
- English and Japanese, when hitting the Spanish translation

### How to reproduce it

1. Go to https://cpython-previews.readthedocs.io/es/latest/
2. Open the flyout
3. You will see that `es` is duplicated
![Screenshot_2024-04-18_13-28-04](https://github.com/readthedocs/readthedocs.org/assets/244656/9b9dabf4-f894-4bf9-80d4-e8ce8d8e9b82)



Discussion:
I think something like this could work:

```diff
diff --git a/readthedocs/proxito/views/hosting.py b/readthedocs/proxito/views/hosting.py
index 4b598e21b..780e09091 100644
--- a/readthedocs/proxito/views/hosting.py
+++ b/readthedocs/proxito/views/hosting.py
@@ -342,7 +342,12 @@ class AddonsResponse:
                 )
 
         main_project = project.main_language_project or project
-        project_translations = main_project.translations.all().order_by("language")
+        # Exclude the current project since we don't want to return itself as a translation
+        project_translations = main_project.translations.all().exclude(slug=project.slug).order_by("language")
+
+        # Include main project as translation if the current project is one of the translations
+        if project != main_project:
+            project_translations |= Project.objects.filter(slug=main_project.slug)
 
         data = {
             "api_version": "1",
```
I tested the previous diff locally and it works fine 👍🏼 . I need to open a PR and write some tests.

Resolving patch:
diff --git a/readthedocs/proxito/views/hosting.py b/readthedocs/proxito/views/hosting.py
--- a/readthedocs/proxito/views/hosting.py
+++ b/readthedocs/proxito/views/hosting.py
@@ -348,7 +348,15 @@ def _v1(self, project, version, build, filename, url, user):
                 )
 
         main_project = project.main_language_project or project
-        project_translations = main_project.translations.all().order_by("language")
+
+        # Exclude the current project since we don't want to return itself as a translation
+        project_translations = main_project.translations.all().exclude(
+            slug=project.slug
+        )
+        # Include main project as translation if the current project is one of the translations
+        if project != main_project:
+            project_translations |= Project.objects.filter(slug=main_project.slug)
+        project_translations = project_translations.order_by("language")
 
         data = {
             "api_version": "1",


Test patch:
diff --git a/readthedocs/proxito/tests/test_hosting.py b/readthedocs/proxito/tests/test_hosting.py
--- a/readthedocs/proxito/tests/test_hosting.py
+++ b/readthedocs/proxito/tests/test_hosting.py
@@ -300,6 +300,7 @@ def test_flyout_translations(self):
         )
         assert r.status_code == 200
 
+        # Hitting the English version of the docs, will return Japanese as translation
         assert len(r.json()["projects"]["translations"]) == 1
         assert r.json()["projects"]["translations"][0]["slug"] == "translation"
         assert r.json()["projects"]["translations"][0]["language"]["code"] == "ja"
@@ -308,6 +309,28 @@ def test_flyout_translations(self):
             == "https://project.dev.readthedocs.io/ja/latest/"
         )
 
+        # Hitting the Japanese version of the docs, will return English as translation
+        r = self.client.get(
+            reverse("proxito_readthedocs_docs_addons"),
+            {
+                "url": "https://project.dev.readthedocs.io/ja/latest/",
+                "client-version": "0.6.0",
+                "api-version": "1.0.0",
+            },
+            secure=True,
+            headers={
+                "host": "project.dev.readthedocs.io",
+            },
+        )
+        assert r.status_code == 200
+        assert len(r.json()["projects"]["translations"]) == 1
+        assert r.json()["projects"]["translations"][0]["slug"] == "project"
+        assert r.json()["projects"]["translations"][0]["language"]["code"] == "en"
+        assert (
+            r.json()["projects"]["translations"][0]["urls"]["documentation"]
+            == "https://project.dev.readthedocs.io/en/latest/"
+        )
+
     def test_flyout_downloads(self):
         fixture.get(
             Version,