Skip to content

Commit

Permalink
Addons: resolve versions/translations URLs properly (#10821)
Browse files Browse the repository at this point in the history
* Addons: resolve versions/translations URLs properly

Use our `Resolver.resolve` logic to resolve versions/translations URLs for the
flyout menu. This makes it work URLs for subprojects and translations as well.

Closes readthedocs/addons#158

* Addons: update tests to match changes

* Update readthedocs/proxito/views/hosting.py

Co-authored-by: Manuel Kaufmann <[email protected]>

---------

Co-authored-by: Eric Holscher <[email protected]>
  • Loading branch information
humitos and ericholscher authored Oct 17, 2023
1 parent 3f6e0e8 commit 1e6e633
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
2 changes: 1 addition & 1 deletion readthedocs/proxito/tests/responses/v0.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
"flyout": {
"enabled": true,
"translations": [],
"versions": [{ "slug": "latest", "url": "/en/latest/" }],
"versions": [{ "slug": "latest", "url": "https://project.dev.readthedocs.io/en/latest/" }],
"downloads": []
},
"search": {
Expand Down
74 changes: 71 additions & 3 deletions readthedocs/proxito/tests/test_hosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,11 @@ def test_flyout_versions(self):
assert r.status_code == 200

expected = [
{"slug": "latest", "url": "/en/latest/"},
{"slug": "public-built", "url": "/en/public-built/"},
{"slug": "latest", "url": "https://project.dev.readthedocs.io/en/latest/"},
{
"slug": "public-built",
"url": "https://project.dev.readthedocs.io/en/public-built/",
},
]
assert r.json()["addons"]["flyout"]["versions"] == expected

Expand All @@ -310,7 +313,7 @@ def test_flyout_translations(self):
assert r.status_code == 200

expected = [
{"slug": "ja", "url": "/ja/"},
{"slug": "ja", "url": "https://project.dev.readthedocs.io/ja/latest/"},
]
assert r.json()["addons"]["flyout"]["translations"] == expected

Expand Down Expand Up @@ -426,3 +429,68 @@ def test_project_subproject(self):
r.json()["projects"]["current"]["repository"]["url"]
== "https://github.com/readthedocs/subproject"
)

def test_flyout_subproject_urls(self):
translation = fixture.get(
Project,
slug="translation",
language="es",
repo="https://github.com/readthedocs/subproject",
)
translation.versions.update(
built=True,
active=True,
)
subproject = fixture.get(
Project, slug="subproject", repo="https://github.com/readthedocs/subproject"
)
self.project.add_subproject(subproject)
subproject.translations.add(translation)
subproject.save()

fixture.get(Version, slug="v1", project=subproject)
fixture.get(Version, slug="v2.3", project=subproject)
subproject.versions.update(
privacy_level=PUBLIC,
built=True,
active=True,
hidden=False,
)

r = self.client.get(
reverse("proxito_readthedocs_docs_addons"),
{
"url": "https://project.dev.readthedocs.io/projects/subproject/en/latest/",
"client-version": "0.6.0",
"api-version": "0.1.0",
},
secure=True,
headers={
"host": "project.dev.readthedocs.io",
},
)
assert r.status_code == 200

expected_versions = [
{
"slug": "latest",
"url": "https://project.dev.readthedocs.io/projects/subproject/en/latest/",
},
{
"slug": "v1",
"url": "https://project.dev.readthedocs.io/projects/subproject/en/v1/",
},
{
"slug": "v2.3",
"url": "https://project.dev.readthedocs.io/projects/subproject/en/v2.3/",
},
]
assert r.json()["addons"]["flyout"]["versions"] == expected_versions

expected_translations = [
{
"slug": "es",
"url": "https://project.dev.readthedocs.io/projects/subproject/es/latest/",
},
]
assert r.json()["addons"]["flyout"]["translations"] == expected_translations
15 changes: 12 additions & 3 deletions readthedocs/proxito/views/hosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
ProjectSerializer,
VersionSerializer,
)
from readthedocs.builds.constants import LATEST
from readthedocs.builds.constants import EXTERNAL, LATEST
from readthedocs.builds.models import Version
from readthedocs.core.resolver import resolver
from readthedocs.core.unresolver import UnresolverError, unresolver
Expand Down Expand Up @@ -329,15 +329,24 @@ def _v0(self, project, version, build, filename, user):
{
# TODO: name this field "display_name"
"slug": translation.language,
"url": f"/{translation.language}/",
"url": resolver.resolve(
project=project,
version_slug=version.slug,
language=translation.language,
external=version.type == EXTERNAL,
),
}
for translation in project_translations
],
"versions": [
{
# TODO: name this field "display_name"
"slug": version.slug,
"url": f"/{project.language}/{version.slug}/",
"url": resolver.resolve(
project=project,
version_slug=version.slug,
external=version.type == EXTERNAL,
),
}
for version in versions_active_built_not_hidden
],
Expand Down

0 comments on commit 1e6e633

Please sign in to comment.