Skip to content

Commit 00b00ea

Browse files
committed
refactor: extract subfunction and improve naming
1 parent a2df14e commit 00b00ea

2 files changed

Lines changed: 22 additions & 18 deletions

File tree

core/context_processors.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ def _get_header_version_data(request):
2020
return data
2121

2222

23+
def _is_non_latest_version(url_version_slug, cookie_slug):
24+
"""Whether the user's active version is a specific, non-latest one.
25+
26+
Drives the dropdown button label: non-latest → show version number,
27+
otherwise → show "Latest". URL wins over cookie.
28+
"""
29+
if url_version_slug:
30+
return url_version_slug != LATEST_RELEASE_URL_PATH_STR
31+
if cookie_slug:
32+
return cookie_slug != LATEST_RELEASE_URL_PATH_STR
33+
return False
34+
35+
2336
def current_version(request):
2437
"""Custom context processor that adds the current release to the context"""
2538
return {"current_version": _get_header_version_data(request).most_recent}
@@ -42,7 +55,7 @@ def selected_version(request):
4255
GET /releases/1.88.0/ (no cookie)
4356
selected_version -> Version(slug="boost-1-88-0")
4457
selected_version_is_url_driven -> True (URL mode: render <a>s)
45-
selected_version_is_explicit -> True (button reads "1.88.0")
58+
selected_version_is_non_latest -> True (button reads "1.88.0")
4659
selected_version_label -> "1.88.0"
4760
version_dropdown_options[i] -> Version with `.href` attached,
4861
e.g. "/releases/1.89.0/"
@@ -51,15 +64,15 @@ def selected_version(request):
5164
GET / (cookie boost_version="boost-1-87-0")
5265
selected_version -> Version(slug="boost-1-87-0")
5366
selected_version_is_url_driven -> False (cookie mode: render <form>s)
54-
selected_version_is_explicit -> True (button reads "1.87.0")
67+
selected_version_is_non_latest -> True (button reads "1.87.0")
5568
selected_version_label -> "1.87.0"
5669
version_dropdown_options[i] -> Version (no `.href` needed)
5770
latest_href -> ""
5871
5972
GET / (no cookie)
6073
selected_version -> Version.objects.most_recent()
6174
selected_version_is_url_driven -> False
62-
selected_version_is_explicit -> False (button reads "Latest")
75+
selected_version_is_non_latest -> False (button reads "Latest")
6376
selected_version_label -> "Latest"
6477
6578
GET /library/1.88.0/foo/ (target version "boost-1-70-0" predates "foo")
@@ -88,17 +101,8 @@ def selected_version(request):
88101
if version is None:
89102
version = header_data.most_recent
90103

91-
is_explicit_non_latest_url = bool(
92-
url_version_slug and url_version_slug != LATEST_RELEASE_URL_PATH_STR
93-
)
94-
is_explicit_cookie = bool(
95-
not url_version_slug
96-
and cookie_slug
97-
and cookie_slug != LATEST_RELEASE_URL_PATH_STR
98-
)
99-
is_explicit = is_explicit_non_latest_url or is_explicit_cookie
100-
101-
label = version.display_name if (is_explicit and version) else "Latest"
104+
is_non_latest = _is_non_latest_version(url_version_slug, cookie_slug)
105+
label = version.display_name if (is_non_latest and version) else "Latest"
102106

103107
latest_href = ""
104108
if is_url_driven and resolver_match and resolver_match.view_name:
@@ -111,7 +115,7 @@ def selected_version(request):
111115
return {
112116
"selected_version": version,
113117
"selected_version_is_url_driven": is_url_driven,
114-
"selected_version_is_explicit": is_explicit,
118+
"selected_version_is_non_latest": is_non_latest,
115119
"selected_version_label": label,
116120
"version_dropdown_options": options,
117121
"latest_href": latest_href,

templates/v3/includes/header/_header_version_dropdown.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Context Variables (from `core.context_processors.selected_version`):
1616
selected_version (Version) — resolved active version
1717
selected_version_is_url_driven (bool) — true on version-aware pages (/releases/, /libraries/, /library/)
18-
selected_version_is_explicit (bool) — drives whether the button label shows a version number or "Latest"
18+
selected_version_is_non_latest (bool) — drives whether the button label shows a version number or "Latest"
1919
selected_version_label (str) — pre-resolved button label
2020
version_dropdown_options (list[Version]) — each has `.href` when URL-driven
2121
latest_href (str) — URL for the "Latest" option when URL-driven
@@ -37,7 +37,7 @@
3737
{% if latest_href %}
3838
<a href="{{ latest_href }}"
3939
class="header__nav-link header__version-option"
40-
{% if not selected_version_is_explicit %}aria-current="true"{% endif %}>
40+
{% if not selected_version_is_non_latest %}aria-current="true"{% endif %}>
4141
Latest
4242
</a>
4343
{% endif %}
@@ -57,7 +57,7 @@
5757
<input type="hidden" name="version" value="latest">
5858
<button type="submit"
5959
class="header__nav-link header__version-option"
60-
{% if not selected_version_is_explicit %}aria-current="true"{% endif %}>
60+
{% if not selected_version_is_non_latest %}aria-current="true"{% endif %}>
6161
Latest
6262
</button>
6363
</form>

0 commit comments

Comments
 (0)