diff --git a/readthedocs/proxito/tests/test_hosting.py b/readthedocs/proxito/tests/test_hosting.py index ee1d53f6d83..f35f2004d8d 100644 --- a/readthedocs/proxito/tests/test_hosting.py +++ b/readthedocs/proxito/tests/test_hosting.py @@ -384,6 +384,18 @@ def test_flyout_single_version_project(self): self.version.has_htmlzip = True self.version.save() + # Add extra built and active versions to emulate a project that went + # from multiple versions to single version. + # These versions shouldn't be included in the `versions.active` field. + for i in range(5): + fixture.get( + Version, + privacy_level=PUBLIC, + active=True, + built=True, + project=self.project, + ) + self.project.versioning_scheme = SINGLE_VERSION_WITHOUT_TRANSLATIONS self.project.save() @@ -400,9 +412,8 @@ def test_flyout_single_version_project(self): }, ) assert r.status_code == 200 - - expected = [] - assert r.json()["versions"]["active"] == expected + expected = ["latest"] + assert [v["slug"] for v in r.json()["versions"]["active"]] == expected expected = { "pdf": "https://project.dev.readthedocs.io/_/downloads/en/latest/pdf/", diff --git a/readthedocs/proxito/views/hosting.py b/readthedocs/proxito/views/hosting.py index 370e2e4a0c8..108fe7f344d 100644 --- a/readthedocs/proxito/views/hosting.py +++ b/readthedocs/proxito/views/hosting.py @@ -342,14 +342,21 @@ def _v1(self, project, version, build, filename, url, request): # projects that don't have one already AddonsConfig.objects.get_or_create(project=project) - if project.supports_multiple_versions: - versions_active_built_not_hidden = ( - self._get_versions(request, project) - .select_related("project") - .order_by("-slug") + versions_active_built_not_hidden = ( + self._get_versions(request, project) + .select_related("project") + .order_by("-slug") + ) + sorted_versions_active_built_not_hidden = versions_active_built_not_hidden + if not project.supports_multiple_versions: + # Return only one version when the project doesn't support multiple versions. + # That version is the only one the project serves. + sorted_versions_active_built_not_hidden = ( + sorted_versions_active_built_not_hidden.filter( + slug=project.get_default_version() + ) ) - sorted_versions_active_built_not_hidden = versions_active_built_not_hidden - + else: if ( project.addons.flyout_sorting == ADDONS_FLYOUT_SORTING_SEMVER_READTHEDOCS_COMPATIBLE