Admin fixes
This commit is contained in:
1 parent
6db39f0d51
commit
a7f6fc0fc8
18 files changed
+285
-59
No files matched your search
+26
-5
@@ -86,9 +86,9 @@ class CalculationStore:
|
||||
)
|
||||
return int(cursor.lastrowid)
|
||||
|
||||
def list_calculations(self, *, sort_by: str = "kitten_id", descending: bool = True,
|
||||
def list_calculations(self, *, sort_by: str = "escort_id", descending: bool = True,
|
||||
limit: int = 100) -> list[dict[str, Any]]:
|
||||
"""Return admin rows with safe, application-level sorting by kitten identity."""
|
||||
"""Return admin rows with safe, application-level sorting by participant or kitten."""
|
||||
limit = max(1, min(int(limit), 500))
|
||||
fetch_limit = 5000
|
||||
if self.database_url.startswith(("postgres://", "postgresql://")):
|
||||
@@ -112,8 +112,29 @@ class CalculationStore:
|
||||
"input": input_data, "result": result_data, "message": row[6],
|
||||
"created_at": row[7].isoformat() if hasattr(row[7], "isoformat") else row[7],
|
||||
})
|
||||
if sort_by not in {"kitten_id", "kitten_name"}:
|
||||
sort_by = "kitten_id"
|
||||
records.sort(key=lambda item: (item["input"].get(sort_by) is None, item["input"].get(sort_by, "")),
|
||||
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:
|
||||
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)
|
||||
return records[:limit]
|
||||
|
||||
def delete_calculation(self, calculation_id: int) -> bool:
|
||||
"""Delete one history row and report whether it existed."""
|
||||
if isinstance(calculation_id, bool) or not isinstance(calculation_id, int) or calculation_id <= 0:
|
||||
raise ValueError("ID записи должно быть положительным целым числом")
|
||||
if self.database_url.startswith(("postgres://", "postgresql://")):
|
||||
with self._psycopg.connect(self.database_url) as connection:
|
||||
row = connection.execute(
|
||||
"DELETE FROM belki_calculations WHERE id = %s RETURNING id", (calculation_id,)
|
||||
).fetchone()
|
||||
return row is not None
|
||||
with sqlite3.connect(self.sqlite_path) as connection:
|
||||
cursor = connection.execute("DELETE FROM calculations WHERE id = ?", (calculation_id,))
|
||||
return cursor.rowcount > 0
|
||||
Reference in new issue
Block a user