Add URL for library detail by version

This commit is contained in:
Lacey Williams Henschel
2023-02-02 10:43:26 -08:00
parent 7de45597fe
commit 95743b9f56
3 changed files with 82 additions and 6 deletions

View File

@@ -21,6 +21,7 @@ from libraries.views import (
LibraryByCategory,
LibraryDetail,
LibraryByVersion,
LibraryByVersionDetail,
)
from support.views import SupportView, ContactView
from versions.api import VersionViewSet
@@ -43,6 +44,11 @@ urlpatterns = [
path("403", ForbiddenView.as_view(), name="forbidden"),
path("404", NotFoundView.as_view(), name="not_found"),
path("500", InternalServerErrorView.as_view(), name="internal_server_error"),
path(
"about/",
TemplateView.as_view(template_name="boost/about.html"),
name="boost-about",
),
path("health/", include("health_check.urls")),
path("forum/", include(machina_urls)),
path(
@@ -66,11 +72,6 @@ urlpatterns = [
LibraryDetail.as_view(),
name="library-detail",
),
path(
"about/",
TemplateView.as_view(template_name="boost/about.html"),
name="boost-about",
),
path(
"people/detail/",
TemplateView.as_view(template_name="boost/people_detail.html"),
@@ -139,6 +140,11 @@ urlpatterns = [
),
path("contact/", ContactView.as_view(), name="contact"),
# Boost versions views
path(
"versions/<int:version_pk>/libraries/<slug:slug>/",
LibraryByVersionDetail.as_view(),
name="libraries-by-version-detail",
),
path(
"versions/<int:version_pk>/libraries/",
LibraryByVersion.as_view(),

View File

@@ -26,6 +26,27 @@ def test_libraries_by_category(tp, library, category):
assert res.context["category"] == category
def test_libraries_by_version_detail(tp, library_version):
"""GET /versions/{version_identifier}/libraries/{slug}/"""
res = tp.get(
"libraries-by-version-detail",
library_version.version.pk,
library_version.library.slug,
)
tp.response_200(res)
assert "version" in res.context
def test_libraries_by_version_detail_no_library_found(tp, library_version):
"""GET /versions/{version_identifier}/libraries/{slug}/"""
res = tp.get(
"libraries-by-version-detail",
library_version.version.pk,
"coffee",
)
tp.response_404(res)
def test_libraries_by_version(tp, library_version):
"""GET /versions/{version_identifier}/libraries/"""
# Create a new library_version

View File

@@ -1,11 +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()
@@ -64,6 +66,53 @@ class LibraryByVersion(CategoryMixin, FormMixin, ListView):
return super().get(request)
class LibraryByVersionDetail(CategoryMixin, DetailView):
"""Display a single Library for a specific Boost version"""
model = Library
template_name = "libraries/detail.html"
def get_object(self):
version_pk = self.kwargs.get("version_pk")
slug = self.kwargs.get("slug")
if not LibraryVersion.objects.filter(
version__pk=version_pk, 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)
context["closed_prs_count"] = self.get_closed_prs_count(self.object)
context["open_issues_count"] = self.get_open_issues_count(self.object)
context["version"] = self.get_version()
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_version(self):
version_pk = self.kwargs.get("version_pk")
if version_pk:
try:
return Version.objects.get(pk=version_pk)
except Version.DoesNotExist:
logger.info("libraries_by_version_detail_view_version_not_found")
return
else:
return Version.objects.most_recent()
class LibraryByLetter(CategoryMixin, ListView):
"""List all of our libraries that begin with a certain letter"""