forked from admin/miem_workers
feat: add council employee sorting by name and publication counts
This commit is contained in:
1 parent
fac0431a03
commit
32e26ccdd2
9 files changed
+105
-20
No files matched your search
@@ -149,7 +149,8 @@ def _unique_articles(articles: list[dict], start: date, end: date) -> list[dict]
|
||||
return sorted(unique.values(), key=lambda item: (not item["csv_qualifies"], -(item["year"] or 0), item["title"].casefold()))
|
||||
|
||||
|
||||
def council_candidates(db: Session, today: date | None = None, q: str = "", offset: int = 0) -> dict:
|
||||
def council_candidates(db: Session, today: date | None = None, q: str = "", offset: int = 0,
|
||||
sort: str = "csv_count", direction: str = "desc") -> dict:
|
||||
today = today or datetime.now(ZoneInfo("Europe/Moscow")).date()
|
||||
start, end = period(today)
|
||||
employees = db.scalars(select(Employee).options(selectinload(Employee.publications))).all()
|
||||
@@ -175,7 +176,11 @@ def council_candidates(db: Session, today: date | None = None, q: str = "", offs
|
||||
"db_count": sum(a["in_db"] for a in articles), "articles": articles,
|
||||
"profile_url": _safe_url(employee.canonical_url), "ambiguous": ambiguous,
|
||||
"dismissed": employee.status == "dismissed"})
|
||||
candidates.sort(key=lambda item: (-item["csv_count"], -item["count"], item["name"].casefold()))
|
||||
sort = sort if sort in {"full_name", "csv_count", "db_count", "count"} else "csv_count"
|
||||
direction = direction if direction in {"asc", "desc"} else "desc"
|
||||
candidates.sort(key=lambda item: _name(item["name"]), reverse=sort == "full_name" and direction == "desc")
|
||||
if sort != "full_name":
|
||||
candidates.sort(key=lambda item: (item[sort], item["count"]), reverse=direction == "desc")
|
||||
total = len(candidates)
|
||||
query = _name(q)
|
||||
candidates = [item for item in candidates if query in _name(item["name"])]
|
||||
@@ -183,4 +188,4 @@ def council_candidates(db: Session, today: date | None = None, q: str = "", offs
|
||||
limit = 25
|
||||
offset = min(max(0, offset), ((matched - 1) // limit) * limit) if matched else 0
|
||||
return {"candidates": candidates[offset:offset + limit], "start": start, "end": end, "total": total,
|
||||
"matched": matched, "q": q, "offset": offset, "limit": limit}
|
||||
"matched": matched, "q": q, "offset": offset, "limit": limit, "sort": sort, "direction": direction}
|
||||
Reference in new issue
Block a user