Admin fixes
This commit is contained in:
1 parent
6db39f0d51
commit
a7f6fc0fc8
18 files changed
+285
-59
No files matched your search
+32
-7
@@ -34,6 +34,16 @@ class Handler(BaseHTTPRequestHandler):
|
||||
self.end_headers()
|
||||
self.wfile.write(encoded)
|
||||
|
||||
def _admin_authorized(self) -> bool:
|
||||
expected = os.getenv("BELKI_ADMIN_PASSWORD", "")
|
||||
if not expected:
|
||||
self._json(503, {"error": "Админская панель не настроена: задайте BELKI_ADMIN_PASSWORD"})
|
||||
return False
|
||||
if self.headers.get("X-Admin-Password", "") != expected:
|
||||
self._json(401, {"error": "Неверный пароль"})
|
||||
return False
|
||||
return True
|
||||
|
||||
def do_GET(self) -> None: # noqa: N802
|
||||
path = urlparse(self.path).path
|
||||
if path in ("/health", "/api/health"):
|
||||
@@ -46,15 +56,10 @@ class Handler(BaseHTTPRequestHandler):
|
||||
self._file(WEB_ROOT / "admin.html")
|
||||
return
|
||||
if path == "/api/admin/calculations":
|
||||
expected = os.getenv("BELKI_ADMIN_PASSWORD", "")
|
||||
if not expected:
|
||||
self._json(503, {"error": "Админская панель не настроена: задайте BELKI_ADMIN_PASSWORD"})
|
||||
return
|
||||
if self.headers.get("X-Admin-Password", "") != expected:
|
||||
self._json(401, {"error": "Неверный пароль"})
|
||||
if not self._admin_authorized():
|
||||
return
|
||||
query = parse_qs(urlparse(self.path).query)
|
||||
sort_by = query.get("sort", ["kitten_id"])[0]
|
||||
sort_by = query.get("sort", ["escort_id"])[0]
|
||||
descending = query.get("direction", ["desc"])[0] != "asc"
|
||||
self._json(200, {"items": history_store().list_calculations(sort_by=sort_by, descending=descending)})
|
||||
return
|
||||
@@ -93,6 +98,26 @@ class Handler(BaseHTTPRequestHandler):
|
||||
except Exception:
|
||||
self._json(500, {"error": "Внутренняя ошибка калькулятора"})
|
||||
|
||||
def do_DELETE(self) -> None: # noqa: N802
|
||||
parsed = urlparse(self.path)
|
||||
prefix = "/api/admin/calculations/"
|
||||
if not parsed.path.startswith(prefix):
|
||||
self._json(404, {"error": "Не найдено"})
|
||||
return
|
||||
if not self._admin_authorized():
|
||||
return
|
||||
raw_id = parsed.path[len(prefix):]
|
||||
try:
|
||||
calculation_id = int(raw_id)
|
||||
deleted = history_store().delete_calculation(calculation_id)
|
||||
except ValueError as exc:
|
||||
self._json(400, {"error": str(exc)})
|
||||
return
|
||||
if not deleted:
|
||||
self._json(404, {"error": "Запись не найдена"})
|
||||
return
|
||||
self._json(200, {"deleted": calculation_id})
|
||||
|
||||
def log_message(self, format: str, *args: object) -> None:
|
||||
if os.getenv("BELKI_ACCESS_LOG", "0") == "1":
|
||||
super().log_message(format, *args)
|
||||
|
||||
Reference in new issue
Block a user