Skip to content

Commit 0d4dcf3

Browse files
committed
Added support for /lib/{LIBRARY} to redirect to library detail page (#1939)
1 parent 089ec54 commit 0d4dcf3

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

config/urls.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
BoostDevelopmentView,
3535
ModernizedDocsView,
3636
QRCodeView,
37+
RedirectToLibraryDetailView,
3738
)
3839
from libraries.api import LibrarySearchView
3940
from libraries.views import (
@@ -381,6 +382,11 @@
381382
),
382383
]
383384
+ [
385+
re_path(
386+
r"^lib/(?P<library_slug>[^/]+)/?$",
387+
RedirectToLibraryDetailView.as_view(),
388+
name="redirect-to-library-view",
389+
),
384390
# Redirects for old boost.org urls.
385391
re_path(
386392
r"^libs/(?P<libname>[^/]+)/(?P<path>.*)/?$",

core/tests/test_views.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,18 @@ def test_qrc_logs_plausible_error_but_still_redirects(tp, caplog):
329329
tp.response_302(res)
330330
assert res["Location"] == "/library/"
331331
assert any("Plausible event post failed" in r.message for r in caplog.records)
332+
333+
334+
def test_redirect_to_library_detail_view(tp):
335+
"""Test that /lib/<library_slug>/ redirects to library detail page with prioritized version."""
336+
response = tp.get("redirect-to-library-view", library_slug="algorithm")
337+
tp.response_302(response)
338+
assert response["Location"] == "/library/latest/algorithm/"
339+
340+
341+
def test_redirect_to_library_detail_view_with_cookie(tp):
342+
"""Test that /lib/<library_slug>/ redirects using version from cookie."""
343+
tp.client.cookies["boost_version"] = "boost-1-86-0"
344+
response = tp.get("redirect-to-library-view", library_slug="algorithm")
345+
tp.response_302(response)
346+
assert response["Location"] == "/library/1.86.0/algorithm/"

core/views.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
from libraries.constants import LATEST_RELEASE_URL_PATH_STR
3131
from libraries.utils import (
3232
legacy_path_transform,
33-
get_prioritized_library_view,
3433
generate_canonical_library_uri,
34+
get_prioritized_library_view,
35+
get_prioritized_version,
3536
)
3637
from versions.models import Version
3738

@@ -891,6 +892,21 @@ def get(self, request, requested_version):
891892
return HttpResponseRedirect(new_path)
892893

893894

895+
class RedirectToLibraryDetailView(BaseRedirectView):
896+
"""View to redirect to a library's detail page"""
897+
898+
def get(self, request, library_slug):
899+
return redirect(
900+
reverse(
901+
"library-detail",
902+
kwargs={
903+
"library_slug": library_slug,
904+
"version_slug": get_prioritized_version(request),
905+
},
906+
)
907+
)
908+
909+
894910
class RedirectToLibrariesView(BaseRedirectView):
895911
"""View to redirect to a versioned libraries page."""
896912

libraries/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def get_prioritized_version(request):
124124
"""
125125
url_version = get_version_from_url(request)
126126
cookie_version = get_version_from_cookie(request)
127-
default_version = None
127+
default_version = LATEST_RELEASE_URL_PATH_STR
128128
return url_version or cookie_version or default_version
129129

130130

0 commit comments

Comments
 (0)