Skip to content

Commit

Permalink
Simplified/centralised version dropdowns population (#1500) (#1510)
Browse files Browse the repository at this point in the history
  • Loading branch information
daveoconnor authored Dec 10, 2024
1 parent 52e7ea7 commit 91f36c3
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 636 deletions.
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exclude: .*migrations\/.*|static\/img\/.*|static\/animations\/.*|static\/js\/boo

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v5.0.0
hooks:
- id: check-case-conflict
- id: check-merge-conflict
Expand All @@ -15,16 +15,16 @@ repos:
- id: trailing-whitespace
exclude: ^core/tests/content/boost_release[a-zA-Z_]+.html
- repo: https://github.com/ambv/black
rev: 23.3.0
rev: 24.10.0
hooks:
- id: black
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.272
rev: v0.8.2
hooks:
- id: ruff
args: [--fix]
- repo: https://github.com/rtts/djhtml
rev: 3.0.6
rev: 3.0.7
hooks:
- id: djhtml
entry: djhtml --tabwidth 2
Expand Down
18 changes: 0 additions & 18 deletions ak/tests/test_default_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,6 @@ def test_homepage(library, version, tp):
assert "featured_library" in response.context


def test_homepage_beta(db, tp):
"""Ensure we can hit the beta homepage"""
url = tp.reverse("home-beta")
tp.get_check_200(url)


def test_homepage_beta_context(db, tp):
"""Ensure have the expected context data on the homepage"""
# Use any page that is named 'home' otherwise use /
url = tp.reverse("home-beta")
response = tp.get_check_200(url)

# Check that "entries" is in the context
assert "entries" in response.context
response = tp.get(url)
tp.response_200(response)


def test_200_page(db, tp):
"""Test a 200 OK page"""

Expand Down
80 changes: 1 addition & 79 deletions ak/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import requests
import structlog
from django.conf import settings
from django.core.cache import cache
Expand All @@ -8,10 +7,9 @@
from django.views import View
from django.views.generic import TemplateView

from config.settings import JDOODLE_API_CLIENT_ID, JDOODLE_API_CLIENT_SECRET
from core.calendar import extract_calendar_events, events_by_month, get_calendar
from libraries.constants import LATEST_RELEASE_URL_PATH_STR
from libraries.models import Category, Library
from libraries.models import Library
from news.models import Entry
from versions.models import Version

Expand Down Expand Up @@ -80,82 +78,6 @@ def get_featured_library(self, latest_version):
return library


class HomepageBetaView(TemplateView):
"""
Boost homepage
"""

template_name = "homepage_beta.html"

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
libraries, categories = [], []
if "category" in self.request.GET and self.request.GET["category"] != "":
context["category"] = Category.objects.get(
slug=self.request.GET["category"]
)
libraries = Library.objects.filter(categories=context["category"]).order_by(
"name"
)
else:
context["category"] = None
libraries = Library.objects.all().order_by("name")
categories = Category.objects.all().order_by("name")
if "version" in self.request.GET:
context["version"] = Version.objects.filter(
slug=self.request.GET["version"]
).first()
else:
context["version"] = Version.objects.most_recent()
if "library" in self.request.GET:
context["library"] = Library.objects.filter(
slug=self.request.GET["library"]
).first()
categories = context["library"].categories.order_by("name")
if "category" in self.request.GET and self.request.GET["category"] != "":
context["category"] = categories.filter(
slug=self.request.GET["category"]
).first()
libraries = (
Library.objects.filter(categories__in=categories)
.order_by("name")
.all()
)
else:
context["library"] = (
libraries[0] if libraries else Library.objects.order_by("name").first()
)
if "category" in self.request.GET and self.request.GET["category"] != "":
context["category"] = categories.filter(
slug=self.request.GET["category"]
).first()

context["versions"] = Version.objects.active().order_by("-release_date")
context["libraries"] = libraries
context["categories"] = categories

context["entries"] = Entry.objects.published().order_by("-publish_at")[:2]
return context

def post(self, request, *args, **kwargs):
code = request.POST.get("code", "")
if not code:
return render(request, self.template_name)
api_url = "https://api.jdoodle.com/v1/execute"
data = {
"clientId": JDOODLE_API_CLIENT_ID,
"clientSecret": JDOODLE_API_CLIENT_SECRET,
"script": code,
"language": "cpp",
"versionIndex": "3",
}
response = requests.post(api_url, json=data)
result = response.json()
output = result.get("output", "")
error = result.get("error", "")
return render(request, self.template_name, {"output": output, "error": error})


class ForbiddenView(View):
"""
This view raises an exception to test our 403.html template
Expand Down
2 changes: 0 additions & 2 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from ak.views import (
ForbiddenView,
HomepageBetaView,
HomepageView,
InternalServerErrorView,
NotFoundView,
Expand Down Expand Up @@ -89,7 +88,6 @@
urlpatterns = (
[
path("", HomepageView.as_view(), name="home"),
path("homepage-beta/", HomepageBetaView.as_view(), name="home-beta"),
path("admin/", admin.site.urls),
path("oauth2/", include("oauth2_provider.urls", namespace="oauth2_provider")),
path("feed/downloads.rss", RSSVersionFeed(), name="downloads_feed_rss"),
Expand Down
6 changes: 3 additions & 3 deletions libraries/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ class CreateReportForm(CreateReportFullForm):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields[
"library_1"
].help_text = "If none are selected, all libraries will be selected."
self.fields["library_1"].help_text = (
"If none are selected, all libraries will be selected."
)

@property
def cache_key(self):
Expand Down
43 changes: 29 additions & 14 deletions libraries/mixins.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import structlog
from django.shortcuts import get_object_or_404
from django.urls import reverse

from libraries.constants import LATEST_RELEASE_URL_PATH_STR
from libraries.models import Library
from versions.models import Version

logger = structlog.get_logger()
Expand All @@ -25,24 +27,37 @@ def get_context_data(self, **kwargs):


class BoostVersionMixin:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# todo: replace get_current_library_version on LibraryDetail with this +
# prefetch_related
context.update(
def dispatch(self, request, *args, **kwargs):
if not self.extra_context:
self.extra_context = {}

if not self.extra_context.get("current_version"):
self.extra_context["current_version"] = Version.objects.most_recent()

self.extra_context.update(
{
"version_str": self.kwargs.get("version_slug"),
"LATEST_RELEASE_URL_PATH_STR": LATEST_RELEASE_URL_PATH_STR,
}
)
if not context.get("current_version"):
context["current_version"] = Version.objects.most_recent()

if context["version_str"] == LATEST_RELEASE_URL_PATH_STR:
context["selected_version"] = context["current_version"]
elif context["version_str"]:
context["selected_version"] = Version.objects.get(
slug=context["version_str"]
if self.extra_context["version_str"] == LATEST_RELEASE_URL_PATH_STR:
self.extra_context["selected_version"] = self.extra_context[
"current_version"
]
elif self.extra_context["version_str"]:
self.extra_context["selected_version"] = get_object_or_404(
Version, slug=self.extra_context["version_str"]
)

return context
version_path_kwargs = {}
if self.request.resolver_match.view_name == "library-detail":
version_path_kwargs["flag_versions_without_library"] = get_object_or_404(
Library, slug=self.kwargs.get("library_slug")
)

self.extra_context["versions"] = Version.objects.get_dropdown_versions(
**version_path_kwargs
)
# here we hack extra_context into the request so we can access for cookie checks
request.extra_context = self.extra_context
return super().dispatch(request, *args, **kwargs)
20 changes: 10 additions & 10 deletions libraries/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ def test_library_list(library_version, tp, url_name="libraries", request_kwargs=
)
baker.make("libraries.LibraryVersion", library=lib2, version=v2)

# Create a version with no libraries
v_no_libraries = baker.make(
"versions.Version", name="boost-1.0.0", release_date=last_year, beta=False
)
url = tp.reverse(url_name, **request_kwargs)
res = tp.get(url, **request_kwargs)

Expand All @@ -38,7 +34,6 @@ def test_library_list(library_version, tp, url_name="libraries", request_kwargs=
assert "object_list" in res.context
assert library_version in res.context["object_list"]
assert lib2 not in res.context["object_list"]
assert v_no_libraries not in res.context["versions"]


def test_library_root_redirect_to_grid(tp):
Expand Down Expand Up @@ -167,17 +162,22 @@ def test_library_detail(library_version, tp):
tp.response_200(response)


def test_library_detail_404(library, tp):
"""GET /libraries/latest/{bad_library_slug}/"""
def test_library_detail_404(library, old_version, tp):
"""GET /library/latest/{bad_library_slug}/"""
# 404 due to bad slug
url = tp.reverse("library-detail", "latest", "bananas")
response = tp.get(url)
tp.response_404(response)

# 404 due to no existing version
url = tp.reverse("library-detail", "latest", library.slug)

def test_library_detail_missing_version(library, old_version, tp):
# custom error due to no existing version
url = tp.reverse("library-detail", old_version.display_name, library.slug)
response = tp.get(url)
tp.response_404(response)
assert (
"There was no version of the Boost.MultiArray library in the 1.70.0 version of "
"Boost." in response.content.decode("utf-8")
)


def test_library_docs_redirect(tp, library, library_version):
Expand Down
7 changes: 5 additions & 2 deletions libraries/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ def get_category(request):


def determine_selected_boost_version(request_value, request):
valid_versions = Version.objects.version_dropdown_strict()
# use the versions in the request if they are available otherwise fall back to DB
valid_versions = getattr(request, "extra_context", {}).get(
"versions", Version.objects.get_dropdown_versions()
)
version_slug = request_value or get_version_from_cookie(request)
if version_slug in [v.slug for v in valid_versions] + [LATEST_RELEASE_URL_PATH_STR]:
return version_slug
Expand All @@ -154,7 +157,7 @@ def determine_selected_boost_version(request_value, request):

def set_selected_boost_version(version_slug: str, response) -> None:
"""Update the selected version in the cookies."""
valid_versions = Version.objects.version_dropdown_strict()
valid_versions = Version.objects.get_dropdown_versions()
if version_slug in [v.slug for v in valid_versions]:
response.set_cookie(SELECTED_BOOST_VERSION_COOKIE_NAME, version_slug)
elif version_slug == LATEST_RELEASE_URL_PATH_STR:
Expand Down
Loading

0 comments on commit 91f36c3

Please sign in to comment.