Merge pull request #116 from revsys/refactor-library-urls

Refactor library urls to use the most recent Boost version
This commit is contained in:
Lacey Williams Henschel
2023-02-14 10:45:32 -08:00
committed by GitHub
4 changed files with 135 additions and 8 deletions

View File

@@ -1,24 +1,110 @@
import datetime
from model_bakery import baker
def test_library_list(library, tp):
def test_library_list(library_version, tp):
"""GET /libraries/"""
last_year = datetime.date.today() - datetime.timedelta(days=365)
v2 = baker.make("versions.Version", name="Version 1.78.0", release_date=last_year)
lib2 = baker.make(
"libraries.Library",
name="sample",
)
baker.make("libraries.LibraryVersion", library=lib2, version=v2)
res = tp.get("libraries")
tp.response_200(res)
assert "library_list" in res.context
assert library_version.library in res.context["library_list"]
assert lib2 not in res.context["library_list"]
def test_library_detail(library, tp):
"""GET /libraries/{repo}/"""
def test_library_list_select_category(library, category, tp):
"""POST /libraries/ to submit a category redirects to the libraries-by-category page"""
res = tp.post("libraries", data={"categories": category.pk})
tp.response_302(res)
assert res.url == f"/libraries-by-category/{category.slug}/"
def test_library_list_by_category(library_version, category, tp):
"""
GET /libraries-by-category/{category_slug}/
A category with libraries
"""
library = library_version.library
version = library_version.version
library.categories.add(category)
res = tp.get("libraries-by-category", category.slug)
tp.response_200(res)
assert "library_list" in res.context
assert len(res.context["library_list"]) == 1
assert library in res.context["library_list"]
def test_library_list_by_category_no_results(library_version, category, tp):
"""
GET /libraries-by-category/{category_slug}/
A category with no libraries
"""
library = library_version.library
version = library_version.version
res = tp.get("libraries-by-category", category.slug)
tp.response_200(res)
assert "library_list" in res.context
assert len(res.context["library_list"]) == 0
def test_library_list_by_category_no_results_for_active_version(library, category, tp):
"""
GET /libraries-by-category/{category_slug}/
A category with a library, but the library isn't attached to the active Boost version
"""
res = tp.get("libraries-by-category", category.slug)
tp.response_200(res)
assert "library_list" in res.context
assert len(res.context["library_list"]) == 0
def test_libraries_by_category(tp, library, category):
"""GET /libraries-by-category/{slug}/"""
baker.make("libraries.Library", name="Sample")
library.categories.add(category)
res = tp.get("libraries-by-category", category.slug)
tp.response_200(res)
assert "library_list" in res.context
assert len(res.context["library_list"]) == 1
assert library in res.context["library_list"]
assert "category" in res.context
assert res.context["category"] == category
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)
@@ -32,11 +118,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)

View File

@@ -29,6 +29,13 @@ class LibraryList(CategoryMixin, FormMixin, ListView):
)
template_name = "libraries/list.html"
def get_queryset(self):
queryset = super().get_queryset()
version = Version.objects.most_recent()
return (
super().get_queryset().filter(library_version__version=version).distinct()
)
def post(self, request):
"""User has submitted a form and will be redirected to the right results"""
form = self.get_form()
@@ -46,6 +53,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)
@@ -63,6 +85,7 @@ class LibraryDetail(CategoryMixin, DetailView):
class LibraryByCategory(CategoryMixin, ListView):
"""List all of our libraries in a certain category"""
form_class = LibraryForm
paginate_by = 25
template_name = "libraries/list.html"
@@ -79,11 +102,16 @@ class LibraryByCategory(CategoryMixin, ListView):
def get_queryset(self):
category = self.kwargs.get("category")
version = Version.objects.most_recent()
return (
Library.objects.prefetch_related("categories")
.filter(categories__slug=category)
.filter(
categories__slug=category,
versions__library_version__version=version,
)
.order_by("name")
.distinct()
)

View File

@@ -6,6 +6,10 @@ class VersionQuerySet(models.QuerySet):
"""Return active versions"""
return self.filter(active=True)
def most_recent(self):
"""Return most recent active version"""
return self.active().order_by("-release_date").first()
class VersionManager(models.Manager):
def get_queryset(self):
@@ -15,6 +19,10 @@ class VersionManager(models.Manager):
"""Return active versions"""
return self.get_queryset().active()
def most_recent(self):
"""Return most recent active version"""
return self.get_queryset().most_recent()
class VersionFileQuerySet(models.QuerySet):
def active(self):

View File

@@ -12,6 +12,10 @@ def test_active_manager(version):
assert Version.objects.active().count() == 1
def test_most_recent_manager(version, inactive_version, old_version):
assert Version.objects.most_recent() == version
def test_active_file_manager(version, inactive_version):
assert Version.objects.active().count() == 1