From 1d80f35638307e5df0352f528dacaea94c294323 Mon Sep 17 00:00:00 2001 From: Lacey Williams Henschel Date: Thu, 2 Feb 2023 11:23:49 -0800 Subject: [PATCH] :sparkles: Redirect library detail to versioned library detail Wasn't sure if we wanted to fully replace the URL or preserve the /libraries/ endpoint? It wasn't hard to add the redirect. --- libraries/tests/test_views.py | 31 ++++++++++++++++++++----------- libraries/views.py | 30 +++++++++++++----------------- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/libraries/tests/test_views.py b/libraries/tests/test_views.py index a56ac095..8aaafb26 100644 --- a/libraries/tests/test_views.py +++ b/libraries/tests/test_views.py @@ -61,24 +61,20 @@ def test_libraries_by_version(tp, library_version): assert excluded_library not in res.context["library_list"] -def test_library_detail(library, tp): - """GET /libraries/{repo}/""" - url = tp.reverse("library-detail", library.slug) - response = tp.get(url) - tp.response_200(response) - - -def test_library_detail_context_get_closed_prs_count(tp, library): +def test_library_detail_context_get_closed_prs_count(tp, library_version): """ GET /libraries/{repo}/ Test that the custom closed_prs_count var appears as expected """ + library = library_version.library + version = library_version.version + # Create open and closed PRs for this library, and another random PR lib2 = baker.make("libraries.Library", slug="sample") baker.make("libraries.PullRequest", library=library, is_open=True) baker.make("libraries.PullRequest", library=library, is_open=False) baker.make("libraries.PullRequest", library=lib2, is_open=True) - url = tp.reverse("library-detail", library.slug) + url = tp.reverse("libraries-by-version-detail", version.pk, library.slug) response = tp.get(url) tp.response_200(response) assert "closed_prs_count" in response.context @@ -86,19 +82,32 @@ def test_library_detail_context_get_closed_prs_count(tp, library): assert response.context["closed_prs_count"] == 1 -def test_library_detail_context_get_open_issues_count(tp, library): +def test_library_detail_context_get_open_issues_count(tp, library_version): """ GET /libraries/{repo}/ Test that the custom open_issues_count var appears as expected """ + library = library_version.library + version = library_version.version + # Create open and closed issues for this library, and another random issue lib2 = baker.make("libraries.Library", slug="sample") baker.make("libraries.Issue", library=library, is_open=True) baker.make("libraries.Issue", library=library, is_open=False) baker.make("libraries.Issue", library=lib2, is_open=True) - url = tp.reverse("library-detail", library.slug) + url = tp.reverse("libraries-by-version-detail", version.pk, library.slug) response = tp.get(url) tp.response_200(response) assert "open_issues_count" in response.context # Verify that the count only includes the one open issue for this library assert response.context["open_issues_count"] == 1 + + +def test_library_detail(library_version, tp): + """GET /libraries/{slug}/""" + library = library_version.library + version = library_version.version + url = tp.reverse("library-detail", library.slug) + response = tp.get(url) + tp.response_302(response) + assert response.url == f"/versions/{version.pk}/libraries/{library.slug}/" diff --git a/libraries/views.py b/libraries/views.py index 5fae0747..9db287e1 100644 --- a/libraries/views.py +++ b/libraries/views.py @@ -2,7 +2,7 @@ import structlog from django.http import Http404 from django.shortcuts import redirect -from django.views.generic import DetailView, ListView +from django.views.generic import DetailView, ListView, RedirectView from django.views.generic.edit import FormMixin from versions.models import Version @@ -156,21 +156,17 @@ class LibraryByCategory(CategoryMixin, ListView): ) -class LibraryDetail(CategoryMixin, DetailView): - """Display a single Library in insolation""" +class LibraryDetail(RedirectView): + """ + Redirect a request for a generic library to the most recent Boost version + of that library. + """ - model = Library - template_name = "libraries/detail.html" + permanent = False + query_string = True + pattern_name = "libraries-by-version-detail" - def get(self, request, *args, **kwargs): - self.object = self.get_object() - context = self.get_context_data(object=self.object) - context["closed_prs_count"] = self.get_closed_prs_count(self.object) - context["open_issues_count"] = self.get_open_issues_count(self.object) - return self.render_to_response(context) - - def get_closed_prs_count(self, obj): - return PullRequest.objects.filter(library=obj, is_open=True).count() - - def get_open_issues_count(self, obj): - return Issue.objects.filter(library=obj, is_open=True).count() + def get_redirect_url(self, *args, **kwargs): + # library = get_object_or_404(Library, slug=kwargs['slug']) + version = Version.objects.most_recent() + return super().get_redirect_url(version_pk=version.pk, slug=kwargs["slug"])