feat: support rejected application status
This commit is contained in:
1 parent
08ec58c7e9
commit
cd33a382c0
5 files changed
+47
-15
No files matched your search
@@ -227,7 +227,7 @@ class ApplicationStore:
|
||||
|
||||
def list(self, *, status: str | None = None, limit: int = 500) -> list[dict[str, Any]]:
|
||||
"""Возвращает заявки, ограничивая выборку и опционально фильтруя статус."""
|
||||
if status not in (None, "pending", "accepted"):
|
||||
if status not in (None, "pending", "accepted", "rejected"):
|
||||
raise ApplicationError("Неизвестный статус заявки")
|
||||
limit = max(1, min(int(limit), 500))
|
||||
if self.database_url.startswith(("postgres://", "postgresql://")):
|
||||
@@ -253,27 +253,34 @@ class ApplicationStore:
|
||||
|
||||
def accept(self, application_id: int) -> dict[str, Any] | None:
|
||||
"""Атомарно переводит заявку в accepted и возвращает её снимок."""
|
||||
return self.review(application_id, status="accepted")
|
||||
|
||||
def review(self, application_id: int, *, status: str) -> dict[str, Any] | None:
|
||||
"""Атомарно присваивает новой заявке итоговый статус рассмотрения."""
|
||||
if application_id <= 0:
|
||||
raise ApplicationError("ID заявки должен быть положительным")
|
||||
if status not in ("accepted", "rejected"):
|
||||
raise ApplicationError("Некорректный статус рассмотрения")
|
||||
if self.database_url.startswith(("postgres://", "postgresql://")):
|
||||
with self._psycopg.connect(self.database_url) as connection:
|
||||
row = connection.execute("""UPDATE belki_applications SET status = 'accepted', reviewed_at = NOW()
|
||||
row = connection.execute("""UPDATE belki_applications SET status = %s, reviewed_at = NOW()
|
||||
WHERE id = %s AND status = 'pending'
|
||||
RETURNING id, actor_id, reply_peer_id, source, application_json, status, created_at, reviewed_at""", (application_id,)).fetchone()
|
||||
RETURNING id, actor_id, reply_peer_id, source, application_json, status, created_at, reviewed_at""", (status, application_id)).fetchone()
|
||||
else:
|
||||
with sqlite3.connect(self.sqlite_path) as connection:
|
||||
cursor = connection.execute("UPDATE applications SET status = 'accepted', reviewed_at = CURRENT_TIMESTAMP WHERE id = ? AND status = 'pending'", (application_id,))
|
||||
cursor = connection.execute("UPDATE applications SET status = ?, reviewed_at = CURRENT_TIMESTAMP WHERE id = ? AND status = 'pending'", (status, application_id))
|
||||
if not cursor.rowcount:
|
||||
return None
|
||||
application = next((item for item in self.list(limit=500) if item["id"] == application_id), None)
|
||||
if application:
|
||||
if application and status == "accepted":
|
||||
self.enqueue_export(application_id, "application_accepted", application)
|
||||
return application
|
||||
if row is None:
|
||||
return None
|
||||
application = {"id": row[0], "actor_id": row[1], "reply_peer_id": row[2], "source": row[3],
|
||||
"application": row[4], "status": row[5], "created_at": row[6].isoformat(), "reviewed_at": row[7].isoformat()}
|
||||
self.enqueue_export(application_id, "application_accepted", application)
|
||||
if status == "accepted":
|
||||
self.enqueue_export(application_id, "application_accepted", application)
|
||||
return application
|
||||
|
||||
def enqueue_export(self, application_id: int, event_type: str, snapshot: dict[str, Any]) -> int:
|
||||
|
||||
+10
-6
@@ -464,24 +464,28 @@ class Handler(BaseHTTPRequestHandler):
|
||||
self._json(200, {"deleted": calculation_id})
|
||||
|
||||
def do_PATCH(self) -> None: # noqa: N802
|
||||
"""Принимает заявку и запускает уведомление через PATCH API."""
|
||||
"""Меняет статус новой заявки через PATCH API."""
|
||||
parsed = urlparse(self.path)
|
||||
prefix = "/api/admin/applications/"
|
||||
if not parsed.path.startswith(prefix) or not parsed.path.endswith("/accept"):
|
||||
if not parsed.path.startswith(prefix):
|
||||
self._json(404, {"error": "Не найдено"})
|
||||
return
|
||||
if not self._admin_authorized():
|
||||
return
|
||||
raw_id = parsed.path[len(prefix):-len("/accept")].strip("/")
|
||||
raw_id, separator, action = parsed.path[len(prefix):].strip("/").partition("/")
|
||||
if not separator or action not in {"accept", "reject"}:
|
||||
self._json(404, {"error": "Не найдено"})
|
||||
return
|
||||
try:
|
||||
application = application_store().accept(int(raw_id))
|
||||
application = application_store().review(int(raw_id), status="accepted" if action == "accept" else "rejected")
|
||||
except (ValueError, ApplicationError) as exc:
|
||||
self._json(400, {"error": str(exc)})
|
||||
return
|
||||
if application is None:
|
||||
self._json(404, {"error": "Заявка не найдена или уже принята"})
|
||||
self._json(404, {"error": "Заявка не найдена или уже рассмотрена"})
|
||||
return
|
||||
sync_application_exports()
|
||||
if application["status"] == "accepted":
|
||||
sync_application_exports()
|
||||
self._json(200, {"item": application})
|
||||
|
||||
def log_message(self, format: str, *args: object) -> None:
|
||||
|
||||
Reference in new issue
Block a user