"""Two pages built from how the records point at each other and at their own history: /recent/ the newest additions, and the newest changes, from each record's [[history]] /connected/ trails whose own words name another trail, grouped under the trail they name Nothing names a trail. "Newest" is measured from the newest date the records give, never from the day the site is built, so a page does not change until the data does. """ import collections from . import browse, common, facets, home, pagekit, related, seo from .markup import Markup, esc RECENT = "/recent/" CONNECTED = "/connected/" ADDED_MIN = 20 # whole days of additions are shown until at least this many trails are CHANGE_ROWS = 30 GROUP_MIN = 3 # a trail is a group when at least this many others name it # Recent ----------------------------------------------------------------------------- def added_date(rec): """The date of the entry that added the record.""" for e in rec.get("history", []): if isinstance(e, dict) and e.get("fields") == ["record"]: return str(e.get("date", "")) return "" def recent_added(records): """[(date, [trails])], newest day first, whole days until at least ADDED_MIN trails.""" days = collections.defaultdict(list) for r in records: d = added_date(r) if d: days[d].append(r) out, count = [], 0 for d in sorted(days, reverse=True): out.append((d, sorted(days[d], key=lambda r: r["id"]))) count += len(days[d]) if count >= ADDED_MIN: break return out def recent_changes(records): """(rows, total): the newest applied changes to a record's facts, as (date, trail, what), newest first and in name order within a day; and how many such changes there are.""" found = [] for r in records: for e in r.get("history", []): if (isinstance(e, dict) and e.get("outcome") == "applied" and e.get("fields") != ["record"] and e.get("date")): found.append((str(e["date"]), r, e.get("change", ""))) found.sort(key=lambda t: (t[0], ), reverse=True) found.sort(key=lambda t: (-int(t[0].replace("-", "")), t[1]["name"].lower())) return found[:CHANGE_ROWS], len(found) def recent_page(records, site): """(title, description, body, recs) for /recent/, or None when fewer than MIN_SIZE trails were added.""" added = recent_added(records) recs = [r for _, rs in added for r in rs] if len(recs) < browse.MIN_SIZE: return None changes, total = recent_changes(records) newest = seo.newest(records) groups = [(f"Added {d} ({len(rs)})", rs, None) for d, rs in added] rows = [[d, pagekit.trail_link(r), change] for d, r, change in changes] lede = (f"The {len(recs)} newest additions, from each trail’s own history, and the newest of the {total} changes " f"made to a trail’s facts. The newest date in the data is {esc(newest)}.") body = (pagekit.head("Recently added and changed", lede, [("Trails", "/"), ("Lists", "/lists/")]) + f"""
Each trail keeps its own history, shown at the foot of its page. The changelog lists changes to the site and the dataset as a whole.