From da798035551302d158d99c669d4e6ae180e68304 Mon Sep 17 00:00:00 2001 From: Lacey Williams Henschel Date: Sat, 11 Feb 2023 08:19:57 -0800 Subject: [PATCH] :wrench: Library detail URL uses most recent active Boost version --- libraries/tests/test_views.py | 28 ++++++++++++++++++++++------ libraries/views.py | 18 +++++++++++++++++- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/libraries/tests/test_views.py b/libraries/tests/test_views.py index 36ed43c7..427ab94a 100644 --- a/libraries/tests/test_views.py +++ b/libraries/tests/test_views.py @@ -38,18 +38,33 @@ def test_libraries_by_category(tp, library, category): assert res.context["category"] == category -def test_library_detail(library, tp): - """GET /libraries/{repo}/""" +def test_library_detail(library_version, tp): + """GET /libraries/{slug}/""" + library = library_version.library 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_404(library, tp): + """GET /libraries/{slug}/""" + # 404 due to bad slug + url = tp.reverse("library-detail", "bananas") + response = tp.get(url) + tp.response_404(response) + + # 404 due to no existing version + url = tp.reverse("library-detail", library.slug) + response = tp.get(url) + tp.response_404(response) + + +def test_library_detail_context_get_closed_prs_count(tp, library_version): """ - GET /libraries/{repo}/ + GET /libraries/{slug}/ Test that the custom closed_prs_count var appears as expected """ + library = library_version.library # 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) @@ -63,11 +78,12 @@ 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}/ + GET /libraries/{slug}/ Test that the custom open_issues_count var appears as expected """ + library = library_version.library # 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) diff --git a/libraries/views.py b/libraries/views.py index 300e505e..03c37b2a 100644 --- a/libraries/views.py +++ b/libraries/views.py @@ -1,12 +1,13 @@ import structlog +from django.http import Http404 from django.shortcuts import redirect from django.views.generic import DetailView, ListView from django.views.generic.edit import FormMixin from versions.models import Version from .forms import LibraryForm -from .models import Category, Issue, Library, PullRequest +from .models import Category, Issue, Library, LibraryVersion, PullRequest logger = structlog.get_logger() @@ -79,6 +80,21 @@ class LibraryDetail(CategoryMixin, DetailView): model = Library template_name = "libraries/detail.html" + def get_object(self): + slug = self.kwargs.get("slug") + version = Version.objects.most_recent() + + if not LibraryVersion.objects.filter( + version=version, library__slug=slug + ).exists(): + raise Http404("No library found matching the query") + + try: + obj = self.get_queryset().get(slug=slug) + except queryset.model.DoesNotExist: + raise Http404("No library found matching the query") + return obj + def get(self, request, *args, **kwargs): self.object = self.get_object() context = self.get_context_data(object=self.object)