From c2245d14b8d263b993ec79403e06589a8dc197b9 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Fri, 20 Nov 2026 18:42:19 -0700 Subject: [PATCH] Do not count a trail of 1,000 miles or more toward the 3 trails a place's list page needs, though the page still shows it, and build no 500 to 1,000 mile page for a park or a wilderness area --- web/olt/placefacets.py | 20 +++++++++++---- web/tests/test_place_facets.py | 45 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/web/olt/placefacets.py b/web/olt/placefacets.py index bfe892f..d800219 100644 --- a/web/olt/placefacets.py +++ b/web/olt/placefacets.py @@ -21,21 +21,30 @@ PLACE_MIN = 3 # a park, range or wilderness area's list page needs this many # A list that is itself a place or a holder has no noun, so none is crossed. PLACE_LISTS_OUT = frozenset() +# A trail this long crosses a place and is not "in" it: it is shown on a place's list page but does +# not count toward the PLACE_MIN needed to build one. +VERY_LONG_KM = browse.LONG_KM +# Length bands of 500 miles or more are not crossed with a park or a wilderness area: no trail that +# long lies inside one. A range may keep them. +OUT_BY_KIND = {"park": frozenset({"500-to-1000-miles"}), "wilderness": frozenset({"500-to-1000-miles"})} -def _plan(units, records, min_size, path_of, lists, max_share): + +def _plan(units, records, min_size, path_of, lists, max_share, count_long=True): """[{slug, info, lst, recs, path, kind}]: for each unit (slug, info) and each list, a page when the place has min_size trails in the list, no more than max_share of the place's trails are in it, - and its trails are not nearly the same set as another page of the same place.""" + and its trails are not nearly the same set as another page of the same place. With count_long False + a trail of VERY_LONG_KM or more is shown but not counted toward min_size.""" shown = {lst.key for lst, _ in browse.shown(records)} out = [] for slug, info in units: here = info["records"] kept = [] for lst in lists: - if lst.key not in shown: + if lst.key not in shown or lst.key in OUT_BY_KIND.get(info["kind"], ()): continue recs = browse.members(lst, here) - if len(recs) < min_size or len(recs) > max_share * len(here) or len(recs) >= len(here): + counted = len(recs) if count_long else sum(1 for r in recs if r["length_km"] < VERY_LONG_KM) + if counted < min_size or len(recs) > max_share * len(here) or len(recs) >= len(here): continue ids = {r["id"] for r in recs} if any(_jaccard(ids, other) >= SAME_SET for other in kept): @@ -59,7 +68,8 @@ def plan_places(infos, records, max_share=browse.MAX_SHARE): units = [(i["slug"], i) for i in sorted(infos.values() if isinstance(infos, dict) else infos, key=lambda i: (places_mod.KIND_ORDER[i["kind"]], i["name"].lower()))] lists = [lst for lst in browse.facet_lists() if lst.key not in PLACE_LISTS_OUT] - return _plan(units, records, PLACE_MIN, lambda slug, info, lst: f"{info['path']}{lst.key}/", lists, max_share) + return _plan(units, records, PLACE_MIN, lambda slug, info, lst: f"{info['path']}{lst.key}/", lists, max_share, + count_long=False) def key_of(p): diff --git a/web/tests/test_place_facets.py b/web/tests/test_place_facets.py index 1d32941..8ecf291 100644 --- a/web/tests/test_place_facets.py +++ b/web/tests/test_place_facets.py @@ -90,6 +90,51 @@ class PlanTest(unittest.TestCase): self.assertIn("Loops", before) +class VeryLongTrailsTest(unittest.TestCase): + """A trail of 1,000 miles or more is shown on a place's list page but does not count to its 3.""" + LONG_KM = 1700 + + def world_of(self, normal, long, kind="range", slug="wind-river-range", entries=(RANGE,), km=150): + recs = [in_place(100 + i, kind, slug, loop=True, km=km) for i in range(normal)] + recs += [in_place(150 + i, kind, slug, loop=True, km=self.LONG_KM) for i in range(long)] + recs += [in_place(200 + i, kind, slug, loop=False, km=150) for i in range(8)] + recs += [fake(300 + i, km=500, regions=["NY"], hike={"shape": "loop"}) for i in range(4)] + recs += [fake(400 + i, km=900, regions=["NY"]) for i in range(30)] + return recs + + def loops(self, recs, **kw): + _, plan = plan_for(recs, **kw) + return next((p for p in plan if p["lst"].key == "loops"), None) + + def test_two_ordinary_trails_and_a_very_long_one_make_no_page(self): + self.assertIsNone(self.loops(self.world_of(2, 1))) + self.assertIsNone(self.loops(self.world_of(0, 3))) + + def test_three_ordinary_trails_make_a_page_that_also_shows_the_very_long_one(self): + p = self.loops(self.world_of(3, 1)) + self.assertIsNotNone(p) + self.assertEqual(len(p["recs"]), 4) + + def test_the_state_plan_still_counts_every_trail(self): + plan = placefacets.plan(self.world_of(2, 1)) + self.assertTrue(all(len(p["recs"]) >= browse.MIN_SIZE for p in plan)) + + def test_a_park_or_wilderness_gets_no_page_for_the_500_to_1000_mile_band(self): + for kind in ("park", "wilderness"): + entry = entry_of(kind, "somewhere", "Somewhere", ["WY"]) + recs = self.world_of(4, 0, kind, "somewhere", (entry,), km=1000) + _, plan = plan_for(recs, (entry,)) + self.assertNotIn("500-to-1000-miles", self.keys(plan), kind) + + def test_a_range_keeps_the_500_to_1000_mile_band(self): + recs = self.world_of(4, 0, km=1000) + _, plan = plan_for(recs) + self.assertIn("500-to-1000-miles", self.keys(plan)) + + def keys(self, plan): + return {p["lst"].key for p in plan} + + def page_of(infos, plan, key): p = next(p for p in plan if p["lst"].key == key) return placefacets.page(p, [r for i in infos.values() for r in i["records"]], S, plan)