Skip to content

Commit 45b9418

Browse files
committed
Added support for /lib/{LIBRARY} to redirect to library detail page (#1939)
1 parent ef9839e commit 45b9418

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

config/urls.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
RedirectToDocsView,
2727
RedirectToHTMLDocsView,
2828
RedirectToHTMLToolsView,
29-
RedirectToLibraryView,
29+
RedirectToLibrariesView,
3030
RedirectToReleaseView,
3131
RedirectToToolsView,
3232
StaticContentTemplateView,
3333
UserGuideTemplateView,
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>.*)/?$",
@@ -404,7 +410,7 @@
404410
),
405411
re_path(
406412
r"^doc/libs/(?P<requested_version>[^/]+)/?$",
407-
RedirectToLibraryView.as_view(),
413+
RedirectToLibrariesView.as_view(),
408414
name="redirect-to-library-page",
409415
),
410416
re_path(

core/tests/test_views.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,18 @@ def test_qrc_logs_plausible_error_but_still_redirects(tp, caplog):
319319
tp.response_302(res)
320320
assert res["Location"] == "/library/"
321321
assert any("Plausible event post failed" in r.message for r in caplog.records)
322+
323+
324+
def test_redirect_to_library_detail_view(tp):
325+
"""Test that /lib/<library_slug>/ redirects to library detail page with prioritized version."""
326+
response = tp.get("redirect-to-library-view", library_slug="algorithm")
327+
tp.response_302(response)
328+
assert response["Location"] == "/library/latest/algorithm/"
329+
330+
331+
def test_redirect_to_library_detail_view_with_cookie(tp):
332+
"""Test that /lib/<library_slug>/ redirects using version from cookie."""
333+
tp.client.cookies["boost_version"] = "boost-1-86-0"
334+
response = tp.get("redirect-to-library-view", library_slug="algorithm")
335+
tp.response_302(response)
336+
assert response["Location"] == "/library/1.86.0/algorithm/"

core/views.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
from config.settings import ENABLE_DB_CACHE
3131
from libraries.constants import LATEST_RELEASE_URL_PATH_STR
32-
from libraries.utils import legacy_path_transform
32+
from libraries.utils import legacy_path_transform, get_prioritized_version
3333
from versions.models import Version
3434

3535
from .asciidoc import convert_adoc_to_html
@@ -900,7 +900,22 @@ def get(self, request, requested_version):
900900
return HttpResponseRedirect(new_path)
901901

902902

903-
class RedirectToLibraryView(BaseRedirectView):
903+
class RedirectToLibraryDetailView(BaseRedirectView):
904+
"""View to redirect to a library's detail page"""
905+
906+
def get(self, request, library_slug):
907+
return redirect(
908+
reverse(
909+
"library-detail",
910+
kwargs={
911+
"library_slug": library_slug,
912+
"version_slug": get_prioritized_version(request),
913+
},
914+
)
915+
)
916+
917+
918+
class RedirectToLibrariesView(BaseRedirectView):
904919
"""View to redirect to a versioned libraries page."""
905920

906921
def get(self, request, requested_version):

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)