fix(history): normalize legacy sorting keys
This commit is contained in:
1 parent
0aa1c202a2
commit
7f16c12255
3 files changed
+31
-8
No files matched your search
+18
-8
@@ -32,7 +32,6 @@ class CalculationStore:
|
||||
"""
|
||||
self.database_url = database_url or ""
|
||||
self.sqlite_path = Path(sqlite_path) if sqlite_path else None
|
||||
fetch_limit = 5000
|
||||
if self.database_url.startswith(("postgres://", "postgresql://")):
|
||||
import psycopg
|
||||
self._psycopg = psycopg
|
||||
@@ -155,15 +154,26 @@ class CalculationStore:
|
||||
})
|
||||
if sort_by not in {"escort_id", "escort_name", "kitten_id", "kitten_name"}:
|
||||
sort_by = "escort_id"
|
||||
def sort_value(item: dict[str, Any]) -> Any:
|
||||
def sort_value(item: dict[str, Any]) -> tuple[bool, int, int | str]:
|
||||
data = item["input"]
|
||||
if sort_by == "escort_id":
|
||||
return data.get("escort_id", data.get("character_id"))
|
||||
if sort_by == "escort_name":
|
||||
return data.get("escort_name", data.get("character_name"))
|
||||
return data.get(sort_by)
|
||||
records.sort(key=lambda item: (sort_value(item) is None, sort_value(item) or ""),
|
||||
reverse=descending)
|
||||
value = data.get("escort_id", data.get("character_id"))
|
||||
elif sort_by == "escort_name":
|
||||
value = data.get("escort_name", data.get("character_name"))
|
||||
else:
|
||||
value = data.get(sort_by)
|
||||
if value is None:
|
||||
return True, 0, ""
|
||||
if sort_by.endswith("_id"):
|
||||
try:
|
||||
return False, 0, int(value)
|
||||
except (TypeError, ValueError):
|
||||
# Existing records can predate API type normalization. Keep
|
||||
# malformed legacy values visible rather than failing the
|
||||
# entire privileged history view.
|
||||
return False, 1, str(value)
|
||||
return False, 0, str(value).casefold()
|
||||
records.sort(key=sort_value, reverse=descending)
|
||||
return records[:limit]
|
||||
|
||||
def delete_calculation(self, calculation_id: int) -> bool:
|
||||
|
||||
Reference in new issue
Block a user