Lead a role in several cities with the one nearest the viewing agent, in the row, the reader and the posting link
Places are ordered in tiers off the agent's gates (the any group, then the require scope, then the rest), and within a tier by the home the any group gives, since the gazetteer holds no coordinates. GET /api/roles and /api/roles/{uid} carry place_texts, lead_place and lead_url for the roles served; the gates and the dupes rule are unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ac4cdb8a48
commit
fb24c8e4c9
8 files changed
+277
-29
No files matched your search
+45
-12
@@ -214,6 +214,27 @@ def _own_texts(conn: sqlite3.Connection, uid: str) -> list[str]:
|
||||
(uid,))))
|
||||
|
||||
|
||||
def _nearest_first(rows: Sequence[RolePlace],
|
||||
near: places.Nearness | None) -> list[RolePlace]:
|
||||
"""A role's place rows, those of one place text (a copy's, one position)
|
||||
kept together, nearest to the agent first (`places.nearest_first`)."""
|
||||
groups: dict[tuple[str, int, str], list[RolePlace]] = {}
|
||||
for row in rows:
|
||||
groups.setdefault((row.url, row.position, row.text), []).append(row)
|
||||
entries = list(groups.values())
|
||||
order = places.nearest_first(near, [(g[0].text, [r.place_id for r in g]) for g in entries])
|
||||
return [row for i in order for row in entries[i]]
|
||||
|
||||
|
||||
def _leads(rows: Sequence[RolePlace]) -> tuple[list[str], str, str]:
|
||||
"""`(distinct texts, lead text, lead url)` of places already in order:
|
||||
the first place leads, and its url is a copy's link, '' for the role's
|
||||
own place."""
|
||||
texts = list(dict.fromkeys(t for r in rows if (t := r.text.strip())))
|
||||
lead = next((r for r in rows if r.text.strip()), None)
|
||||
return texts, lead.text.strip() if lead else "", lead.url if lead else ""
|
||||
|
||||
|
||||
def _like(text: str) -> str:
|
||||
"""A `LIKE` pattern for a case-insensitive substring match, with SQLite's
|
||||
own wildcards escaped so a search for "50%" or "a_b" stays literal."""
|
||||
@@ -918,7 +939,9 @@ class Store:
|
||||
role = ScoredRole.from_row(row)
|
||||
role.tags = self._tags_of(uid)
|
||||
role.copies = self._copies_of(uid)
|
||||
role.places = self.role_places(uid)
|
||||
role.places = _nearest_first(self.role_places(uid), self._nearness(agent_id))
|
||||
_, role.lead_place, role.lead_url = _leads(role.places)
|
||||
role.lead_place = role.lead_place or role.place
|
||||
role.remote_where = self._remote_where([uid]).get(uid, "")
|
||||
role.judgement = self.judgement(agent_id, uid)
|
||||
role.cv_score = role.judgement.cv_score if role.judgement else None
|
||||
@@ -967,21 +990,28 @@ class Store:
|
||||
gaz = self.gazetteer()
|
||||
return {uid: gaz.area_label(ids) for uid, ids in found.items()}
|
||||
|
||||
def _place_lines(self, uids: Sequence[str]) -> dict[str, str]:
|
||||
"""`uid -> the line the feed shows` for a page of roles that have
|
||||
more than one place (`dupes.place_line`); a role with one place or
|
||||
none is absent and keeps `roles.place`."""
|
||||
def _nearness(self, agent_id: int | None) -> places.Nearness | None:
|
||||
"""How near a place is to the agent a page is viewed as, from its
|
||||
gates; None without an agent, which keeps the host's order."""
|
||||
found = self.agent(agent_id) if agent_id is not None else None
|
||||
return places.Nearness(self.gazetteer(), found.gates) if found is not None else None
|
||||
|
||||
def _place_leads(self, uids: Sequence[str],
|
||||
near: places.Nearness | None) -> dict[str, list[RolePlace]]:
|
||||
"""`uid -> its places nearest first` (`_nearest_first`) for a page
|
||||
of roles; a role with no place is absent. One query over the page's
|
||||
uids, so only the roles shown are ordered."""
|
||||
if not uids:
|
||||
return {}
|
||||
found: dict[str, list[str]] = {}
|
||||
found: dict[str, list[RolePlace]] = {}
|
||||
for r in self._rows(
|
||||
"""SELECT uid, text FROM role_places
|
||||
"""SELECT uid, position, text, place_id, url FROM role_places
|
||||
WHERE uid IN (SELECT value FROM json_each(?))
|
||||
ORDER BY uid, url != '', url, position, rowid""",
|
||||
(json.dumps(list(uids)),)):
|
||||
found.setdefault(str(r["uid"]), []).append(str(r["text"]))
|
||||
return {uid: dupes.place_line(texts) for uid, texts in found.items()
|
||||
if len(set(texts)) > 1}
|
||||
found.setdefault(str(r["uid"]), []).append(RolePlace(
|
||||
position=r["position"], text=r["text"], place_id=r["place_id"], url=r["url"]))
|
||||
return {uid: _nearest_first(rows, near) for uid, rows in found.items()}
|
||||
|
||||
def rebuild_dupes(self) -> DupeCounts:
|
||||
"""Recompute which open roles are copies of one posting, in one
|
||||
@@ -1270,12 +1300,15 @@ class Store:
|
||||
found.append(RoleRow.from_row(row))
|
||||
by_uid = self._tags_by_uid([f.uid for f in found])
|
||||
copies = self._copy_counts([f.uid for f in found])
|
||||
lines = self._place_lines([f.uid for f in found])
|
||||
leads = self._place_leads([f.uid for f in found], self._nearness(agent))
|
||||
areas = self._remote_where([f.uid for f in found])
|
||||
for f in found:
|
||||
f.tags = by_uid.get(f.uid, [])
|
||||
f.copies = copies.get(f.uid, 0)
|
||||
f.place = lines.get(f.uid, f.place)
|
||||
f.place_texts, f.lead_place, f.lead_url = _leads(leads.get(f.uid, []))
|
||||
if len(f.place_texts) > 1:
|
||||
f.place = dupes.place_line(f.place_texts)
|
||||
f.lead_place = f.lead_place or f.place
|
||||
f.remote_where = areas.get(f.uid, "")
|
||||
return found
|
||||
|
||||
|
||||
Reference in new issue
Block a user