Added apps

This commit is contained in:
nyan404 committed 2026-08-21 13:18:34 +03:00
1 parent 540c393258
commit cfeee5a814
11 files changed
+548 -12

No files matched your search

+44 -2
View File
@@ -11,6 +11,7 @@ from urllib.parse import parse_qs, unquote, urlparse
from .belki.api import ApiError, calculate_and_store, inspect_bulk_payload, resolve_identity_payload
from .belki.history import CalculationStore
from .belki.applications import ApplicationError, ApplicationStore, validate_application
from .belki.identity import IdentityDirectory
from .belki.image_host import ImageHostClient
@@ -19,6 +20,7 @@ ROOT = Path(__file__).resolve().parents[1]
WEB_ROOT = ROOT / "web"
_HISTORY_STORE: CalculationStore | None = None
_IDENTITY_DIR: IdentityDirectory | None = None
_APPLICATION_STORE: ApplicationStore | None = None
_WEB_SESSIONS: set[str] = set()
@@ -36,6 +38,13 @@ def identity_dir() -> IdentityDirectory:
return _IDENTITY_DIR
def application_store() -> ApplicationStore:
global _APPLICATION_STORE
if _APPLICATION_STORE is None:
_APPLICATION_STORE = ApplicationStore.from_environment()
return _APPLICATION_STORE
_WEB_PROFILE_KEY = "web_user"
@@ -115,6 +124,9 @@ class Handler(BaseHTTPRequestHandler):
if path == "/admin":
self._file(WEB_ROOT / "admin.html")
return
if path in ("/applications", "/applications/"):
self._file(WEB_ROOT / "applications.html")
return
if path == "/api/admin/calculations":
if not self._admin_authorized():
return
@@ -127,6 +139,13 @@ class Handler(BaseHTTPRequestHandler):
limit = 100
self._json(200, {"items": history_store().list_calculations(sort_by=sort_by, descending=descending, limit=limit)})
return
if path == "/api/admin/applications":
if not self._admin_authorized():
return
query = parse_qs(urlparse(self.path).query)
status = query.get("status", [None])[0] or None
self._json(200, {"items": application_store().list(status=status)})
return
if path == "/api/web/session":
expected = os.getenv("BELKI_WEB_PASSWORD", "")
cookies = self.headers.get("Cookie", "")
@@ -150,7 +169,7 @@ class Handler(BaseHTTPRequestHandler):
def do_POST(self) -> None: # noqa: N802
if self.path not in ("/api/web/auth", "/api/web/logout", "/api/web/upload", "/api/calculate", "/api/inspect-bulk", "/api/resolve-identity",
"/api/save-profile", "/api/get-profile"):
"/api/save-profile", "/api/get-profile", "/api/applications"):
self._json(404, {"error": "Не найдено"})
return
try:
@@ -195,9 +214,13 @@ class Handler(BaseHTTPRequestHandler):
self._json(200, save_profile(payload))
elif self.path == "/api/get-profile":
self._json(200, get_profile())
elif self.path == "/api/applications":
application = validate_application(payload)
application_id = application_store().create(application=application, actor_id=None, reply_peer_id=None, source="web")
self._json(201, {"id": application_id, "status": "pending", "message": "Заявка отправлена на рассмотрение."})
else:
self._json(200, calculate_and_store(payload, actor_id=None, source="web"))
except ApiError as exc:
except (ApiError, ApplicationError) as exc:
self._json(400, {"error": str(exc)})
except (ValueError, json.JSONDecodeError) as exc:
self._json(400, {"error": str(exc)})
@@ -224,6 +247,25 @@ class Handler(BaseHTTPRequestHandler):
return
self._json(200, {"deleted": calculation_id})
def do_PATCH(self) -> None: # noqa: N802
parsed = urlparse(self.path)
prefix = "/api/admin/applications/"
if not parsed.path.startswith(prefix) or not parsed.path.endswith("/accept"):
self._json(404, {"error": "Не найдено"})
return
if not self._admin_authorized():
return
raw_id = parsed.path[len(prefix):-len("/accept")].strip("/")
try:
application = application_store().accept(int(raw_id))
except (ValueError, ApplicationError) as exc:
self._json(400, {"error": str(exc)})
return
if application is None:
self._json(404, {"error": "Заявка не найдена или уже принята"})
return
self._json(200, {"item": application})
def log_message(self, format: str, *args: object) -> None:
if os.getenv("BELKI_ACCESS_LOG", "0") == "1":
super().log_message(format, *args)