From 8f58103a45c89dbe4c80211b2715a14492443575 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Thu, 19 Nov 2026 13:14:52 -0700 Subject: [PATCH] Put the closest in length first among the other trails of the same state on a trail page Co-Authored-By: Claude Fable 5.2 --- web/olt/trail.py | 11 +++++++-- web/tests/test_onward_order.py | 45 ++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 web/tests/test_onward_order.py diff --git a/web/olt/trail.py b/web/olt/trail.py index 4077d44..4861cb6 100644 --- a/web/olt/trail.py +++ b/web/olt/trail.py @@ -4,6 +4,7 @@ The main sections say what the record says, in plain words. Every field of the record, with its exact value, is in the table at the end of the page (raw_record); the main flow carries no coded values. """ +import math import os import re import urllib.parse @@ -1773,13 +1774,19 @@ def _onward_list(title, recs, more_href, more_text, heading_id, meta=None): f'

{esc(title)}

{more}') +def nearest_length(rec, other): + """A sort key for the trails of a place on a trail's page: the closest in length first (by ratio, so + 300 and 600 miles are as far apart as 30 and 60), then by name.""" + return (abs(math.log(other["length_km"] / rec["length_km"])), facets.sort_name(other)) + + RELATED_WHY = {"name": "same name", "named": "named here", "names": "mentions this"} def onward(rec, records): """Ways onward at the foot of the page, all worked out from the records (olt/related.py): trails tied to this one by name or by what the descriptions say; other trails in the - same state or province (up to three of them); trails like this one in length, terrain + same state or province (up to three of them), the closest in length first; trails like this one in length, terrain and shape; and for a route, other routes by the same author.""" if not records: return "" @@ -1793,7 +1800,7 @@ def onward(rec, records): for i, key in enumerate(k for k in facets.place_keys(rec) if k != facets.ACROSS_CANADA): if i >= 3: break - here = sorted((r for r in others if key in facets.place_keys(r)), key=lambda r: -r["length_km"]) + here = sorted((r for r in others if key in facets.place_keys(r)), key=lambda r: nearest_length(rec, r)) if not here: continue name = facets.place_name(key) diff --git a/web/tests/test_onward_order.py b/web/tests/test_onward_order.py new file mode 100644 index 0000000..99dc747 --- /dev/null +++ b/web/tests/test_onward_order.py @@ -0,0 +1,45 @@ +"""On a trail page, the other trails of the same state come closest in length first.""" +import math +import os +import re +import sys +import unittest + +WEB = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +ROOT = os.path.dirname(WEB) +sys.path.insert(0, WEB) + +from olt import facets, records, trail # noqa: E402 + + +class OnwardOrderTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.recs = records.load_records(ROOT) + cls.by_id = {r["id"]: r for r in cls.recs} + + def part(self, rec, key): + html = str(trail.onward(rec, self.recs)) + start = html.index(f'id="onward-{key.lower()}"') + return html[start:html.index("", start)] + + def test_the_sierra_high_route_leads_with_trails_near_its_length(self): + rec = self.by_id["sierra-high-route"] + ids = re.findall(r'href="/trails/([^/"]+)/"', self.part(rec, "US-CA")) + self.assertEqual(len(ids), trail.ONWARD_MAX) + gaps = [abs(math.log(self.by_id[i]["length_km"] / rec["length_km"])) for i in ids] + self.assertEqual(gaps, sorted(gaps)) + self.assertNotIn("great-western-loop", ids) # the 6,875 mile loop is no longer first + everyone = sorted((r for r in self.recs if r["id"] != rec["id"] and "US-CA" in facets.place_keys(r)), + key=lambda r: trail.nearest_length(rec, r)) + self.assertEqual(ids, [r["id"] for r in everyone[:trail.ONWARD_MAX]]) + + def test_how_many_are_shown_is_unchanged(self): + for rec in self.recs[:40]: + html = str(trail.onward(rec, self.recs)) + for ul in re.findall(r'', html): + self.assertLessEqual(ul.count("
  • "), trail.ONWARD_MAX) + + +if __name__ == "__main__": + unittest.main()