Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed back button library select persistance #1329

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
)
from versions.api import ImportVersionsView, VersionViewSet
from versions.feeds import AtomVersionFeed, RSSVersionFeed
from versions.views import VersionCurrentReleaseDetail, VersionDetail
from versions.views import VersionDetail

router = routers.SimpleRouter()

Expand Down Expand Up @@ -131,12 +131,8 @@
# Boost community calendar
path("calendar/", CalendarView.as_view(), name="calendar"),
# Boost versions views
path("releases/", VersionDetail.as_view(), name="releases-most-recent"),
path("releases/<slug:slug>/", VersionDetail.as_view(), name="release-detail"),
path(
"releases/",
VersionCurrentReleaseDetail.as_view(),
name="releases-most-recent",
),
path(
"donate/",
TemplateView.as_view(template_name="donate/donate.html"),
Expand Down
5 changes: 2 additions & 3 deletions core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,7 @@ def get(self, request, requested_version):

# Handle the special case for "release" versions to redirect to the
# most recent Boost release
if requested_version == "release":
requested_version = Version.objects.most_recent().slug

new_path = f"/libraries/?version=boost-{ requested_version }"
if requested_version == "release":
new_path = "/libraries/"
return HttpResponseRedirect(new_path)
67 changes: 47 additions & 20 deletions libraries/mixins.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from urllib.parse import urlencode

import structlog
from django.urls import reverse

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


logger = structlog.get_logger()


Expand All @@ -11,25 +14,49 @@ class VersionAlertMixin:

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)

latest_version = Version.objects.most_recent()
version_slug = self.request.GET.get(
"version", latest_version.slug if latest_version else None
current_release = Version.objects.most_recent()
url_name = self.request.resolver_match.url_name
url_names_version_slug_override = {
"library-detail": "version_slug",
"library-detail-by-version": "version_slug",
}
version_slug_name = url_names_version_slug_override.get(url_name, "slug")
version_slug = self.kwargs.get(
version_slug_name,
self.request.GET.get("version", LATEST_RELEASE_URL_PATH_STR),
)

selected_version = None
if version_slug:
try:
selected_version = Version.objects.get(slug=version_slug)
except Version.DoesNotExist:
selected_version = latest_version

context["latest_version"] = latest_version
req_version = version_slug or LATEST_RELEASE_URL_PATH_STR

try:
selected_version = Version.objects.get(slug=version_slug)
except Version.DoesNotExist:
selected_version = current_release

def generate_reverse(url_name):
if url_name in {
"libraries-mini",
"libraries-by-category",
"libraries-grid",
}:
url = reverse(url_name)
params = {"version": LATEST_RELEASE_URL_PATH_STR}
return f"{url}?{urlencode(params)}"
elif url_name == "library-detail-by-version":
library_slug = self.kwargs.get(
"slug"
) # only really accurately set on library detail page
return reverse("library-detail", args=[library_slug])
elif url_name == "release-detail":
return reverse(url_name, args=[LATEST_RELEASE_URL_PATH_STR])

# 'version_str' is representative of what the user has chosen, while 'version'
# is the actual version instance that will be used in the template. We use the
# value of LATEST_RELEASE_URL_PATH_STR as the default in order to normalize
# behavior
context["version"] = selected_version

if selected_version and latest_version:
context["version_alert"] = selected_version != latest_version
else:
context["version_alert"] = False

context["version_str"] = req_version
context["LATEST_RELEASE_URL_PATH_STR"] = LATEST_RELEASE_URL_PATH_STR
context["current_release"] = current_release
context["version_alert"] = context["version_str"] != LATEST_RELEASE_URL_PATH_STR
context["version_alert_url"] = generate_reverse(url_name)
return context
10 changes: 2 additions & 8 deletions libraries/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,7 @@ def test_library_detail_context_get_documentation_url_no_docs_link(
response = tp.get(url)
tp.response_200(response)
assert "documentation_url" in response.context
assert (
response.context["documentation_url"]
== library_version.version.documentation_url
)
assert response.context["documentation_url"] == "/doc/libs/release"


def test_library_detail_context_get_documentation_url_missing_docs_bool(
Expand All @@ -243,10 +240,7 @@ def test_library_detail_context_get_documentation_url_missing_docs_bool(
response = tp.get(url)
tp.response_200(response)
assert "documentation_url" in response.context
assert (
response.context["documentation_url"]
== library_version.version.documentation_url
)
assert response.context["documentation_url"] == "/doc/libs/release"


def test_library_detail_context_get_documentation_url_docs_present(
Expand Down
41 changes: 39 additions & 2 deletions libraries/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import random
import string
import re

daveoconnor marked this conversation as resolved.
Show resolved Hide resolved
import structlog
import tempfile
from datetime import datetime
Expand All @@ -15,6 +17,7 @@
DEFAULT_LIBRARIES_LANDING_VIEW,
SELECTED_BOOST_VERSION_COOKIE_NAME,
SELECTED_LIBRARY_VIEW_COOKIE_NAME,
LATEST_RELEASE_URL_PATH_STR,
)
from versions.models import Version

Expand Down Expand Up @@ -147,7 +150,7 @@ def build_view_query_params_from_request(request):
query_params = {}
version = get_prioritized_version(request)
category = get_category(request)
if version and version != "latest":
if version and version != LATEST_RELEASE_URL_PATH_STR:
query_params["version"] = version
if category:
query_params["category"] = category
Expand Down Expand Up @@ -182,7 +185,41 @@ def set_selected_boost_version(version_slug: str, response) -> None:
valid_versions = Version.objects.version_dropdown_strict()
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":
elif version_slug == LATEST_RELEASE_URL_PATH_STR:
response.delete_cookie(SELECTED_BOOST_VERSION_COOKIE_NAME)
else:
logger.warning(f"Attempted to set invalid version slug: {version_slug}")


def library_doc_latest_transform(url):
p = re.compile(r"(/doc/libs/)[a-zA-Z0-9_]+([//\S]*)$")
if p.match(url):
url = p.sub(r"\1release\2", url)
return url


def get_documentation_url(library_version, latest):
"""Get the documentation URL for the current library."""

def find_documentation_url(library_version):
version = library_version.version
docs_url = version.documentation_url

# If we know the library-version docs are missing, return the version docs
if library_version.missing_docs:
return docs_url
# If we have the library-version docs and they are valid, return those
elif library_version.documentation_url:
return library_version.documentation_url
# If we wind up here, return the version docs
else:
return docs_url

# Get the URL for the version.
url = find_documentation_url(library_version)
# Remove the "boost_" prefix from the URL.
url = url.replace("boost_", "")
if latest:
url = library_doc_latest_transform(url)

return url
Loading