From a4632260e86fe9f178aec9420e323130f0cfa693 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Fri, 20 Nov 2026 18:42:50 -0700 Subject: [PATCH] Build one page where a park, a range and a wilderness area hold exactly the same trails for a list, preferring the park, then the range, then the wilderness area and then the place with fewer trails, and pin the 75 percent cap on place pages with a test --- web/olt/placefacets.py | 24 +++++++++++++-- web/tests/test_place_facets.py | 55 ++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/web/olt/placefacets.py b/web/olt/placefacets.py index d800219..39b7a33 100644 --- a/web/olt/placefacets.py +++ b/web/olt/placefacets.py @@ -3,7 +3,10 @@ For every place and every list that has a `noun` (olt/browse.py), the page exists when the place has at least MIN_SIZE trails in the list, no more than MAX_SHARE of the place's trails are in it (else it would repeat the place page), and its trails are not nearly the same set as another such page of -the same place. A new trail or a corrected record moves pages in and out with no change here. +the same place. For a park, range or wilderness area, a trail of 1,000 miles or more is shown but does +not count toward the 3 needed, and pages of different places for one list that hold exactly the same +trails are one page (the park's, else the range's, else the wilderness area's). A new trail or a +corrected record moves pages in and out with no change here. """ from . import bounds, browse, common, facets, home, places as places_mod, regions, summary from .markup import Markup, esc @@ -68,8 +71,23 @@ 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, - count_long=False) + return _one_page_per_set(_plan(units, records, PLACE_MIN, lambda slug, info, lst: f"{info['path']}{lst.key}/", + lists, max_share, count_long=False)) + + +def _one_page_per_set(pages): + """Of the place pages of one list that hold exactly the same trails, keep one: the park's, else + the range's, else the wilderness area's; of two of a kind, the place with fewer trails (the more + specific), then the name. The others are dropped, and nothing links to them.""" + rank = {"park": 0, "range": 1, "wilderness": 2} + best = {} + for p in pages: + k = (p["lst"].key, frozenset(r["id"] for r in p["recs"])) + mine = (rank[p["kind"]], len(p["info"]["records"]), p["info"]["name"].lower()) + if k not in best or mine < best[k][0]: + best[k] = (mine, p) + keep = {id(v[1]) for v in best.values()} + return [p for p in pages if id(p) in keep] def key_of(p): diff --git a/web/tests/test_place_facets.py b/web/tests/test_place_facets.py index 8ecf291..66e86e3 100644 --- a/web/tests/test_place_facets.py +++ b/web/tests/test_place_facets.py @@ -135,6 +135,61 @@ class VeryLongTrailsTest(unittest.TestCase): return {p["lst"].key for p in plan} +def shared_world(places, loops=3): + """places: [(entry, how many other trails it has)]. All of them share the same `loops` loops; + each has its own other trails; and enough loops and long trails elsewhere for the list to be shown.""" + every = [(e["kind"], e["slug"]) for e, _ in places] + recs = [rec(100 + i, f"Shared loop {i}", every, km=150) for i in range(loops)] + for r in recs: + r["hike"] = dict(r["hike"], shape="loop") + for n, (e, others) in enumerate(places): + recs += [rec(1000 * (n + 1) + i, f"Own {n} {i}", [(e["kind"], e["slug"])], km=150) for i in range(others)] + 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 + + +class ShareAndSameSetTest(unittest.TestCase): + def paths(self, places, loops=3): + entries = [e for e, _ in places] + _, plan = plan_for(shared_world(places, loops), entries) + return sorted(p["path"] for p in plan if p["lst"].key == "loops") + + def test_a_page_holding_more_than_three_quarters_of_its_place_is_not_built(self): + self.assertEqual(self.paths([(RANGE, 1)], loops=3), ["/ranges/wind-river-range/loops/"]) # 3 of 4 + self.assertEqual(self.paths([(RANGE, 1)], loops=4), []) # 4 of 5 + + def test_one_set_in_a_park_a_range_and_a_wilderness_is_one_page_under_the_park(self): + park = entry_of("park", "grand-test", "Grand Test National Park", ["WY"]) + rng = entry_of("range", "test-range", "Test Range", ["WY"]) + wild = entry_of("wilderness", "test-wild", "Test Wilderness", ["WY"]) + self.assertEqual(self.paths([(wild, 3), (rng, 4), (park, 5)]), ["/parks/grand-test/loops/"]) + self.assertEqual(self.paths([(wild, 3), (rng, 4)]), ["/ranges/test-range/loops/"]) + + def test_of_two_of_one_kind_the_more_specific_place_wins_then_the_name(self): + a = entry_of("range", "a-range", "A Range", ["WY"]) + b = entry_of("range", "b-range", "B Range", ["WY"]) + self.assertEqual(self.paths([(a, 6), (b, 3)]), ["/ranges/b-range/loops/"]) + self.assertEqual(self.paths([(b, 4), (a, 4)]), ["/ranges/a-range/loops/"]) + + def test_different_sets_keep_a_page_each(self): + a = entry_of("range", "a-range", "A Range", ["WY"]) + b = entry_of("range", "b-range", "B Range", ["WY"]) + recs = shared_world([(a, 4), (b, 4)]) + for r in recs: + if r["name"] == "Own 1 0": + r["hike"] = dict(r["hike"], shape="loop") # b has a loop of its own + _, plan = plan_for(recs, [a, b]) + self.assertEqual(sorted(p["path"] for p in plan if p["lst"].key == "loops"), + ["/ranges/a-range/loops/", "/ranges/b-range/loops/"]) + + def test_the_place_left_without_a_page_links_to_nothing_for_the_list(self): + park = entry_of("park", "grand-test", "Grand Test National Park", ["WY"]) + rng = entry_of("range", "test-range", "Test Range", ["WY"]) + _, plan = plan_for(shared_world([(rng, 4), (park, 5)]), [rng, park]) + self.assertEqual({k for p in plan if p["kind"] == "range" for k in [p["lst"].key]} & {"loops"}, set()) + + 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)