Skip to content

Commit

Permalink
Consolidate version dropdown code and fix library detail dropdown (#1498
Browse files Browse the repository at this point in the history
)
  • Loading branch information
GregKaleka authored Nov 27, 2024
1 parent 350119e commit f85c533
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 54 deletions.
11 changes: 0 additions & 11 deletions libraries/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ class Meta:
fields = ["categories"]


class VersionSelectionForm(Form):
queryset = Version.objects.active().defer("data")
queryset = queryset.exclude(name__in=["develop", "master", "head"])

version = ModelChoiceField(
queryset=queryset,
label="Select a version",
empty_label="Choose a version...",
)


class CreateReportFullForm(Form):
"""Form for creating a report over all releases."""

Expand Down
17 changes: 1 addition & 16 deletions libraries/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
from ..forms import LibraryForm, VersionSelectionForm
from ..forms import LibraryForm


def test_library_form_success(tp, library, category):
form = LibraryForm(data={"categories": [category]})
assert form.is_valid() is True


def test_version_selection_form(library_version):
# Test with a valid version
valid_version = library_version.version
form = VersionSelectionForm(data={"version": valid_version.pk})
assert form.is_valid()

# Test with an invalid version
form = VersionSelectionForm(data={"version": 9999})
assert form.is_valid() is False

# Test with no version selected
form = VersionSelectionForm(data={"version": None})
assert form.is_valid() is False
17 changes: 4 additions & 13 deletions libraries/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
from django.views import View
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import DetailView, ListView
from django.views.generic.edit import FormMixin

from core.githubhelper import GithubAPIClient
from versions.models import Version

from .constants import README_MISSING
from .forms import VersionSelectionForm
from .mixins import VersionAlertMixin, BoostVersionMixin
from .models import (
Category,
Expand Down Expand Up @@ -122,12 +120,12 @@ def get_versions(self, current_version):
"""
Return a queryset of all versions to display in the version dropdown.
"""
versions = Version.objects.version_dropdown().order_by("-name")
versions = Version.objects.version_dropdown_strict()

# Annotate each version with the number of libraries it has
versions = versions.annotate(
library_count=Count("library_version", distinct=True)
).order_by("-name")
)

# Filter out versions with no libraries
versions = versions.filter(library_count__gt=0)
Expand All @@ -136,9 +134,6 @@ def get_versions(self, current_version):
if current_version and current_version not in versions:
versions = versions | Version.objects.filter(pk=current_version.pk)

# Manually exclude the master and develop branches.
# todo: confirm is redundant with version_dropdown()'s matching exclude
# versions = versions.exclude(name__in=["develop", "master", "head"])
versions.prefetch_related("library_version")
return versions

Expand Down Expand Up @@ -237,10 +232,9 @@ def get_results_by_category(self, version: Version | None):


@method_decorator(csrf_exempt, name="dispatch")
class LibraryDetail(FormMixin, VersionAlertMixin, BoostVersionMixin, DetailView):
class LibraryDetail(VersionAlertMixin, BoostVersionMixin, DetailView):
"""Display a single Library in insolation"""

form_class = VersionSelectionForm
model = Library
template_name = "libraries/detail.html"
redirect_to_docs = False
Expand All @@ -250,12 +244,9 @@ def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# Get fields related to Boost versions
context["versions"] = (
Version.objects.active()
Version.objects.version_dropdown_strict()
.filter(library_version__library=self.object)
.distinct()
.exclude(name__in=["develop", "master", "head"])
.exclude(beta=True)
.order_by("-release_date")
)
context["LATEST_RELEASE_URL_PATH_NAME"] = LATEST_RELEASE_URL_PATH_STR
# Get general data and version-sensitive data
Expand Down
14 changes: 5 additions & 9 deletions versions/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def minor_versions(self):
"""
return self.get_queryset().with_version_split().filter(patch=0)

def version_dropdown(self):
def version_dropdown(self, exclude_branches=True):
"""Return the versions that should show in the version drop-down"""
all_versions = self.active().filter(beta=False)
most_recent = self.most_recent()
Expand All @@ -95,29 +95,25 @@ def should_show_beta(most_recent, most_recent_beta):
)

include_beta = should_show_beta(most_recent, most_recent_beta)
exclude_branches = True

if include_beta:
beta_queryset = self.active().filter(models.Q(name=most_recent_beta.name))
queryset = (all_versions | beta_queryset).order_by("-name")
queryset = all_versions | beta_queryset
else:
queryset = all_versions.order_by("-name")
queryset = all_versions

if exclude_branches:
queryset = queryset.exclude(name__in=["develop", "master", "head"])

return queryset
return queryset.order_by("-name").defer("data")

def version_dropdown_strict(self, *, exclude_branches=True):
"""Returns the versions to be shown in the drop-down, but does not include
the development branches"""
versions = self.version_dropdown()
versions = self.version_dropdown(exclude_branches=exclude_branches)
# exclude if full_release is False and beta is False
versions = versions.exclude(full_release=False, beta=False)

if exclude_branches:
versions = versions.exclude(name__in=["develop", "master", "head"])

return versions


Expand Down
6 changes: 1 addition & 5 deletions versions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from django.db.models import Q, Count
from django.views.generic import DetailView, TemplateView, ListView
from django.views.generic.edit import FormMixin
from django.shortcuts import redirect, get_object_or_404
from django.contrib import messages
from django.conf import settings
Expand All @@ -13,7 +12,6 @@

from core.models import RenderedContent
from libraries.constants import LATEST_RELEASE_URL_PATH_STR
from libraries.forms import VersionSelectionForm
from libraries.mixins import VersionAlertMixin, BoostVersionMixin
from libraries.models import Commit, CommitAuthor
from libraries.utils import (
Expand All @@ -25,12 +23,10 @@


@method_decorator(csrf_exempt, name="dispatch")
class VersionDetail(FormMixin, BoostVersionMixin, VersionAlertMixin, DetailView):
class VersionDetail(BoostVersionMixin, VersionAlertMixin, DetailView):
"""Web display of list of Versions"""

form_class = VersionSelectionForm
model = Version
queryset = Version.objects.active().defer("data")
template_name = "versions/detail.html"

def get_context_data(self, **kwargs):
Expand Down

0 comments on commit f85c533

Please sign in to comment.