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:
WolfandClaude Opus 5.5 committed 2026-10-04 08:15:26 +02:00
1 parent ac4cdb8a48
commit fb24c8e4c9
8 files changed
+277 -29

No files matched your search

+16 -2
View File
@@ -1194,9 +1194,17 @@ class Role(Model):
#: The open copies of this role, oldest first, when it is the kept one;
#: empty otherwise. Not a column: filled by `Store.role`.
copies: list[RoleCopy] = []
#: Every place of the role, in the host's order. Not a column: filled by
#: `Store.role` from `role_places`.
#: Every place of the role, the nearest to the viewing agent first
#: (`places.Nearness`), else in the host's order. Not a column: filled
#: by `Store.role` from `role_places`.
places: list[RolePlace] = []
#: The text of the first of `places`, what the row and the reader lead
#: with; '' for a role with no place.
lead_place: str = ""
#: The link of the folded copy whose place leads, which the posting
#: opens at; '' when the role's own place leads, and the role's own
#: link stands.
lead_url: str = ""
#: Where a remote hire may live, as a label: "anywhere", "Europe",
#: "United States, Canada". '' when the role is not remote or its area is
#: not stated. Not a column: filled by `Store` from `role_remote`.
@@ -1321,6 +1329,12 @@ class RoleRow(Model):
excerpt: str = ""
#: How many open roles are copies of this one (`Role.copies`), 0 when none.
copies: int = 0
#: The distinct place texts, the nearest to the agent first; `place` is
#: the first of them and "+N" for the others. `lead_place` and
#: `lead_url` as on `Role`.
place_texts: list[str] = []
lead_place: str = ""
lead_url: str = ""
#: As `Role.remote_where`: the area a remote hire may live in, '' when
#: the role is not remote or does not say.
remote_where: str = ""
+105 -6
View File
@@ -52,13 +52,13 @@ from collections.abc import Callable, Iterable, Sequence
from dataclasses import dataclass
from functools import lru_cache
from app.models import (_CITIES, _COUNTRY_NAMES, _EUROPE, _REGIONS, _SUBDIVISIONS, _place_fold,
country_of, remote_of)
from app.models import (_CITIES, _COUNTRY_NAMES, _EUROPE, _REGIONS, _SUBDIVISIONS, Gate,
_place_fold, country_of, remote_of)
__all__ = ["ANYWHERE", "ANYWHERE_WORDS", "KINDS", "REGIONS", "SQL_FUNCTION", "Gazetteer", "Place",
"Region", "Seed", "Suggestion", "Token", "fold", "known", "label", "match_sql",
"matches", "parse", "placeless", "register", "remote_part", "seed", "suggest",
"towns"]
__all__ = ["ANYWHERE", "ANYWHERE_WORDS", "KINDS", "REGIONS", "SQL_FUNCTION", "Gazetteer",
"Nearness", "Place", "Region", "Seed", "Suggestion", "Token", "fold", "known", "label",
"match_sql", "matches", "nearest_first", "parse", "placeless", "register",
"remote_part", "seed", "suggest", "towns"]
def fold(text: str) -> str:
@@ -1255,3 +1255,102 @@ class Gazetteer:
else:
found = {p for form in _expand(tok.value) for p in self.aliases.get(form, ())}
return frozenset(found) or None
# ------------------------------------------------------------------ nearness
class Nearness:
"""How near each place of a role is to one agent, read off its gates: a
role posted in several cities is shown, and linked, by the nearest.
Three tiers. A place comes first when it meets the agent's `any` group:
it lies under one of the group's `country` or `place` values, or it is a
remote part whose area lies under one of them, is open to the agent
through a `remote_from` value as the remote chips match, or meets the
agent's scope (its `require` gates on country) under a plain `remote`
any gate, as `remote:any` does. A place in the scope comes second, the rest third, a remote part with no
stated area ("Remote") among them. The gazetteer holds no coordinates,
so within a tier the home stands in for distance: the first value of
the first `any` gate on country or place (Denmark for "DK, SE"). A
place in one of the group's own towns inside the home comes first
(Copenhagen and Odense), then one elsewhere in the home (Billund,
"Denmark"), then the rest, each in the host's order. The gates
themselves never read this: they read every place."""
def __init__(self, gaz: Gazetteer, gates: Iterable[Gate]) -> None:
self.gaz = gaz
near: set[int] = set()
towns: set[int] = set()
home: frozenset[int] | None = None
self._words: list[str] = []
self._remote_any = False
self._remote_from: set[int] = set()
scopes: list[frozenset[int]] = []
for gate in gates:
values = [str(v).strip() for v in gate.values if str(v).strip()]
if gate.field == "country" and gate.mode == "require":
scopes.append(frozenset(p for v in values for p in gaz.lookup(f"country:{v}") or ()))
if gate.mode != "any":
continue
if gate.field == "remote":
self._remote_any |= any(fold(v) == "remote" for v in values)
elif gate.field == "remote_from":
for value in values:
tok = parse(f"remote:{value}")
if tok.value == "any":
self._remote_any = True
elif named := gaz.lookup(str(tok)):
self._remote_from |= named if tok.value == "anywhere" else gaz.overlap(named)
elif gate.field in ("country", "place"):
for value in values:
token = f"country:{value}" if gate.field == "country" else value
named = gaz.lookup(token)
if named is None:
self._words.append(token)
continue
near |= named
if gate.field == "place":
towns |= named
if home is None:
home = named
self._near = gaz.under(near)
self._home = gaz.under(home or ())
self._home_towns = gaz.under(p for p in towns if p in self._home)
self._scope = [gaz.under(s) for s in scopes]
self._scope_areas = [gaz.overlap(s) for s in scopes]
def _in_scope(self, ids: Sequence[int]) -> bool:
return bool(self._scope) and any(all(p in s for s in self._scope) for p in ids)
def _area_in_scope(self, area: Sequence[int]) -> bool:
return all(any(p in s for p in area) for s in self._scope_areas)
def key(self, text: str, ids: Iterable[int | None]) -> tuple[int, int]:
"""`(tier, 0 in a home town, 1 in the home, else 2)` of one place
text and the places it resolved to, lower nearer."""
found = [p for p in ids if p is not None and p in self.gaz.places]
area = self.gaz.remote_places(text)
named = any(p in self._near for p in (*found, *area)) or (
not found and any(matches(fold(text), "", w) for w in self._words))
remote = bool(area) and (any(p in self._remote_from for p in area)
or (self._remote_any and self._area_in_scope(area)))
if named or remote:
tier = 1
elif self._in_scope(found) or (area and self._scope and self._area_in_scope(area)):
tier = 2
else:
tier = 3
both = (*found, *area)
return tier, (0 if any(p in self._home_towns for p in both)
else 1 if any(p in self._home for p in both) else 2)
def nearest_first(near: Nearness | None,
entries: Sequence[tuple[str, Sequence[int | None]]]) -> list[int]:
"""The indices of a role's places `(text, place ids)` nearest first
(`Nearness.key`), the host's order within a key and throughout without
an agent."""
order = list(range(len(entries)))
if near is None:
return order
return sorted(order, key=lambda i: near.key(*entries[i]))
+15 -7
View File
@@ -979,13 +979,20 @@
return n > 1 ? `<span class="dupes" title="Posted ${n} times">×${n}</span>` : "";
}
/* Where a role's posting is: the ad's own page once it is known
/* Where a role's own posting is: the ad's own page once it is known
(`posting_url`, found for a Jobindex role when its text is fetched), else
the link the board listed. Either way only an http(s) link. */
function linkOf(x) {
function ownLink(x) {
return safeUrl(x?.posting_url) || safeUrl(x?.original_url);
}
/* Where the row and the reader open a role: the copy of the place it leads
with, the one nearest the agent (`lead_url`, `places.Nearness`), else
its own posting. */
function linkOf(x) {
return safeUrl(x?.lead_url) || ownLink(x);
}
/* The other postings of this role, each a link to where it is posted,
labelled by host and place, as newsdesk's "Also covered by". */
function alsoPosted(r) {
@@ -1004,11 +1011,12 @@
</div>`;
}
/* The role's places in the host's order, one entry for each place text a
link opens: the copies' own links, and the role's link for its own. */
/* The role's places as the server orders them, the nearest to the agent
first, one entry for each place text a link opens: the copies' own
links, and the role's link for its own. */
function placeEntries(r) {
const seen = new Set();
const own = linkOf(r);
const own = ownLink(r);
const out = [];
for (const p of Array.isArray(r.places) ? r.places : []) {
const url = p.url ? safeUrl(p.url) : own;
@@ -1020,8 +1028,8 @@
return out;
}
/* The place line of the reader: the first place and "+N" for the others,
as the feed's row shows it. */
/* The place line of the reader: the leading place and "+N" for the
others, as the feed's row shows it. */
function placeLine(r) {
const texts = [...new Set(placeEntries(r).map((e) => e.text))];
return texts.length > 1 ? `${texts[0]} +${texts.length - 1}` : r.place || texts[0] || "";
+45 -12
View File
@@ -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