|
2 | 2 | from enum import StrEnum |
3 | 3 |
|
4 | 4 | from django.conf import settings |
5 | | -from django.urls import reverse |
| 5 | +from django.urls import NoReverseMatch, reverse |
6 | 6 |
|
| 7 | +from libraries.constants import LATEST_RELEASE_URL_PATH_STR |
| 8 | +from libraries.utils import get_version_from_cookie |
| 9 | +from versions.converters import BoostVersionSlugConverter |
7 | 10 | from versions.models import Version |
8 | 11 |
|
9 | 12 |
|
| 13 | +_BOOST_VERSION_SLUG_ROUTE_TOKEN = ( |
| 14 | + f"<{BoostVersionSlugConverter.URL_TYPE_NAME}:version_slug>" |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +def _get_header_version_data(request): |
| 19 | + """Per-request shared accessor so `current_version` and `selected_version` |
| 20 | + share one DB fetch within a single request.""" |
| 21 | + cached = getattr(request, "_header_version_data", None) |
| 22 | + if cached is not None: |
| 23 | + return cached |
| 24 | + data = Version.objects.get_header_dropdown_data() |
| 25 | + request._header_version_data = data |
| 26 | + return data |
| 27 | + |
| 28 | + |
| 29 | +def _is_non_latest_version(url_version_slug, cookie_slug): |
| 30 | + """Whether the user's active version is a specific, non-latest one. |
| 31 | +
|
| 32 | + Drives the dropdown button label: non-latest → show version number, |
| 33 | + otherwise → show "Latest". URL wins over cookie. |
| 34 | + """ |
| 35 | + if url_version_slug: |
| 36 | + return url_version_slug != LATEST_RELEASE_URL_PATH_STR |
| 37 | + if cookie_slug: |
| 38 | + return cookie_slug != LATEST_RELEASE_URL_PATH_STR |
| 39 | + return False |
| 40 | + |
| 41 | + |
10 | 42 | def current_version(request): |
11 | 43 | """Custom context processor that adds the current release to the context""" |
12 | | - return {"current_version": Version.objects.most_recent()} |
| 44 | + return {"current_version": _get_header_version_data(request).most_recent} |
| 45 | + |
| 46 | + |
| 47 | +def selected_version(request): |
| 48 | + """User's active Boost version + data to render the navbar version dropdown. |
| 49 | +
|
| 50 | + Resolution priority for `selected_version`: |
| 51 | + 1. URL `version_slug` kwarg (on `/releases/`, `/libraries/`, `/library/` routes) |
| 52 | + 2. `boost_version` cookie |
| 53 | + 3. Most recent release (fallback) |
| 54 | +
|
| 55 | + When the version comes from the URL, the dropdown renders anchor links that |
| 56 | + swap the version segment of the current path — shareable. Otherwise it |
| 57 | + renders POST forms that write the cookie without navigating. |
| 58 | +
|
| 59 | + Examples |
| 60 | + -------- |
| 61 | + GET /releases/1.88.0/ (no cookie) |
| 62 | + selected_version -> Version(slug="boost-1-88-0") |
| 63 | + selected_version_is_url_driven -> True (URL mode: render <a>s) |
| 64 | + selected_version_is_non_latest -> True (button reads "1.88.0") |
| 65 | + selected_version_label -> "1.88.0" |
| 66 | + version_dropdown_options[i] -> Version with `.href` attached, |
| 67 | + e.g. "/releases/1.89.0/" |
| 68 | + latest_href -> "/releases/latest/" |
| 69 | +
|
| 70 | + GET / (cookie boost_version="boost-1-87-0") |
| 71 | + selected_version -> Version(slug="boost-1-87-0") |
| 72 | + selected_version_is_url_driven -> False (cookie mode: render <form>s) |
| 73 | + selected_version_is_non_latest -> True (button reads "1.87.0") |
| 74 | + selected_version_label -> "1.87.0" |
| 75 | + version_dropdown_options[i] -> Version (no `.href` needed) |
| 76 | + latest_href -> "" |
| 77 | +
|
| 78 | + GET / (no cookie) |
| 79 | + selected_version -> Version.objects.most_recent() |
| 80 | + selected_version_is_url_driven -> False |
| 81 | + selected_version_is_non_latest -> False (button reads "Latest") |
| 82 | + selected_version_label -> "Latest" |
| 83 | +
|
| 84 | + GET /library/1.88.0/foo/ (target version "boost-1-70-0" predates "foo") |
| 85 | + version_dropdown_options[…].href for that version |
| 86 | + -> "/library/1.70.0/foo/" |
| 87 | + (plain version swap; target view |
| 88 | + renders the missing-version state) |
| 89 | + """ |
| 90 | + url_version_slug = None |
| 91 | + resolver_match = getattr(request, "resolver_match", None) |
| 92 | + if resolver_match and _BOOST_VERSION_SLUG_ROUTE_TOKEN in ( |
| 93 | + resolver_match.route or "" |
| 94 | + ): |
| 95 | + url_version_slug = resolver_match.kwargs.get("version_slug") |
| 96 | + |
| 97 | + is_url_driven = bool(url_version_slug) |
| 98 | + cookie_slug = get_version_from_cookie(request) |
| 99 | + |
| 100 | + header_data = _get_header_version_data(request) |
| 101 | + options = list(header_data.options) |
| 102 | + |
| 103 | + resolved_slug = url_version_slug or cookie_slug |
| 104 | + version = None |
| 105 | + if resolved_slug and resolved_slug != LATEST_RELEASE_URL_PATH_STR: |
| 106 | + version = next((v for v in options if v.slug == resolved_slug), None) |
| 107 | + if version is None: |
| 108 | + version = Version.objects.filter(slug=resolved_slug).first() |
| 109 | + if version is None: |
| 110 | + version = header_data.most_recent |
| 111 | + |
| 112 | + is_non_latest = _is_non_latest_version(url_version_slug, cookie_slug) |
| 113 | + label = version.display_name if (is_non_latest and version) else "Latest" |
| 114 | + |
| 115 | + latest_href = "" |
| 116 | + if is_url_driven and resolver_match and resolver_match.view_name: |
| 117 | + latest_href = _annotate_option_hrefs( |
| 118 | + view_name=resolver_match.view_name, |
| 119 | + url_kwargs=dict(resolver_match.kwargs), |
| 120 | + options=options, |
| 121 | + ) |
| 122 | + |
| 123 | + return { |
| 124 | + "selected_version": version, |
| 125 | + "selected_version_is_url_driven": is_url_driven, |
| 126 | + "selected_version_is_non_latest": is_non_latest, |
| 127 | + "selected_version_label": label, |
| 128 | + "version_dropdown_options": options, |
| 129 | + "latest_href": latest_href, |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | +def _annotate_option_hrefs(*, view_name, url_kwargs, options): |
| 134 | + """Attach `.href` to each option for the current page; return the latest's. |
| 135 | +
|
| 136 | + The swap is a plain `reverse()` with the option's version substituted in — |
| 137 | + on a library detail page at `/library/1.88.0/foo/`, picking 1.70.0 yields |
| 138 | + `/library/1.70.0/foo/` even if that library didn't exist in 1.70.0. The |
| 139 | + target view is responsible for rendering the empty / "not available" state. |
| 140 | +
|
| 141 | + Examples |
| 142 | + -------- |
| 143 | + Current URL /releases/1.88.0/ |
| 144 | + view_name = "release-detail" |
| 145 | + url_kwargs = {"version_slug": "boost-1-88-0"} |
| 146 | + → each option gets .href = "/releases/<that version>/" |
| 147 | + → returns "/releases/latest/" |
| 148 | +
|
| 149 | + Current URL /libraries/1.88.0/grid/containers/ |
| 150 | + view_name = "libraries-list" |
| 151 | + url_kwargs = {"version_slug": "boost-1-88-0", |
| 152 | + "library_view_str": "grid", |
| 153 | + "category_slug": "containers"} |
| 154 | + → each option gets .href = "/libraries/<that version>/grid/containers/" |
| 155 | + → returns "/libraries/latest/grid/containers/" |
| 156 | + """ |
| 157 | + for v in options: |
| 158 | + try: |
| 159 | + v.href = reverse(view_name, kwargs={**url_kwargs, "version_slug": v.slug}) |
| 160 | + except NoReverseMatch: |
| 161 | + v.href = "" |
| 162 | + |
| 163 | + try: |
| 164 | + return reverse( |
| 165 | + view_name, |
| 166 | + kwargs={**url_kwargs, "version_slug": LATEST_RELEASE_URL_PATH_STR}, |
| 167 | + ) |
| 168 | + except NoReverseMatch: |
| 169 | + return "" |
13 | 170 |
|
14 | 171 |
|
15 | 172 | class NavItem(StrEnum): |
|
0 commit comments