Fix review queue pagination and API fallback handling
This commit is contained in:
@@ -367,13 +367,15 @@ def _external_out(item: ExternalObservation) -> ExternalObservationOut:
|
||||
@app.get("/api/v1/admin/external-observations", response_model=list[ExternalObservationOut])
|
||||
def admin_external_observations(
|
||||
db: Db, _: Annotated[str, Depends(_admin)],
|
||||
status: Literal["staged", "mapped", "ready", "published", "rejected"] | None = None,
|
||||
status: Literal["staged", "mapped", "ready", "published", "rejected", "review"] | None = None,
|
||||
source_system: str | None = None, limit: int = Query(50, ge=1, le=200), offset: int = Query(0, ge=0),
|
||||
) -> list[ExternalObservationOut]:
|
||||
query = select(ExternalObservation).options(
|
||||
joinedload(ExternalObservation.fish), joinedload(ExternalObservation.waterbody),
|
||||
)
|
||||
if status:
|
||||
if status == "review":
|
||||
query = query.where(ExternalObservation.status.in_(["staged", "mapped", "ready"]))
|
||||
elif status:
|
||||
query = query.where(ExternalObservation.status == status)
|
||||
if source_system:
|
||||
query = query.where(ExternalObservation.source_system == source_system)
|
||||
|
||||
@@ -57,6 +57,32 @@ def test_invalid_period_is_rejected() -> None:
|
||||
assert client.get("/api/v1/activity?sort=unknown").status_code == 422
|
||||
|
||||
|
||||
def test_review_queue_filters_before_pagination() -> None:
|
||||
with Session(engine) as db:
|
||||
stage_observations(db, [{
|
||||
"source_system": "rf4map", "source_external_id": f"queue-{i}",
|
||||
"source_url": f"https://rf4map.ru/points/queue-{i}",
|
||||
"fish": "Queue fish", "waterbody": "Queue water",
|
||||
} for i in range(3)])
|
||||
rows = list(db.scalars(select(ExternalObservation).where(ExternalObservation.source_system == "rf4map")))
|
||||
for i, row in enumerate(rows):
|
||||
row.status = "published" if i == 0 else "ready"
|
||||
row.last_seen_at = datetime.now(timezone.utc) - timedelta(minutes=i)
|
||||
db.commit()
|
||||
try:
|
||||
headers = {"Authorization": "Bearer change-me-in-production"}
|
||||
url = "/api/v1/admin/external-observations?status=review&source_system=rf4map&limit=1"
|
||||
first = client.get(url, headers=headers).json()
|
||||
second = client.get(url + "&offset=1", headers=headers).json()
|
||||
assert len(first) == len(second) == 1
|
||||
assert first[0]["status"] == second[0]["status"] == "ready"
|
||||
assert first[0]["id"] != second[0]["id"]
|
||||
finally:
|
||||
for row in rows:
|
||||
db.delete(row)
|
||||
db.commit()
|
||||
|
||||
|
||||
def test_timeline_includes_more_than_catch_page_and_sitemap_includes_old_spots() -> None:
|
||||
with Session(engine) as db:
|
||||
fish = db.scalar(select(Fish).where(Fish.slug == "pike"))
|
||||
|
||||
@@ -25,7 +25,7 @@ export class ApiError extends Error {
|
||||
}
|
||||
|
||||
export async function api<T>(path: string): Promise<T> {
|
||||
const response = await fetch(`${base}${path}`);
|
||||
const response = await fetch(`${base}${path}`, { signal: AbortSignal.timeout(8000) });
|
||||
if (!response.ok) throw new ApiError(response.status);
|
||||
return response.json() as Promise<T>;
|
||||
}
|
||||
|
||||
@@ -8,12 +8,22 @@ const apiUrl = import.meta.env.PUBLIC_API_URL || "http://localhost:8000";
|
||||
<form class="admin-login" autocomplete="off"><label>Административный токен<input name="token" type="password" required autocomplete="off" /></label><button type="submit">Открыть очередь</button></form>
|
||||
<p class="privacy">Токен остаётся только в памяти страницы. Исходная ссылка и происхождение сохраняются при публикации.</p>
|
||||
<div class="notice error" data-admin-error role="alert" hidden></div><div class="moderation-list" data-external-list aria-live="polite"></div>
|
||||
<nav data-pages aria-label="Страницы очереди" hidden>
|
||||
<button type="button" data-previous>Предыдущая</button>
|
||||
<span data-page-number aria-live="polite"></span>
|
||||
<button type="button" data-next>Следующая</button>
|
||||
</nav>
|
||||
</section>
|
||||
<script>
|
||||
const root = document.querySelector<HTMLElement>(".moderation-app");
|
||||
const login = document.querySelector<HTMLFormElement>(".admin-login");
|
||||
const list = document.querySelector<HTMLElement>("[data-external-list]");
|
||||
const error = document.querySelector<HTMLElement>("[data-admin-error]");
|
||||
const pages = document.querySelector<HTMLElement>("[data-pages]");
|
||||
const previous = document.querySelector<HTMLButtonElement>("[data-previous]");
|
||||
const next = document.querySelector<HTMLButtonElement>("[data-next]");
|
||||
const pageNumber = document.querySelector<HTMLElement>("[data-page-number]");
|
||||
let offset = 0;
|
||||
let token = ""; let fishes: Record<string, string>[] = []; let waters: Record<string, string>[] = [];
|
||||
const esc = (value: unknown) => String(value ?? "—").replace(/[&<>'"]/g, char => ({"&":"&","<":"<",">":">","'":"'",'"':"""}[char] ?? char));
|
||||
const fail = (message: string) => { if (error) { error.textContent = message; error.hidden = false; } };
|
||||
@@ -22,12 +32,28 @@ const apiUrl = import.meta.env.PUBLIC_API_URL || "http://localhost:8000";
|
||||
async function loadQueue() {
|
||||
if (!root || !list) return; error?.setAttribute("hidden", ""); list.innerHTML = '<div class="state"><h2>Загружаем staging…</h2></div>';
|
||||
[fishes, waters] = await Promise.all([json(`${root.dataset.apiUrl}/api/v1/fishes`), json(`${root.dataset.apiUrl}/api/v1/waterbodies`)]);
|
||||
const rows = await json(`${root.dataset.apiUrl}/api/v1/admin/external-observations?limit=200`, {headers:{Authorization:`Bearer ${token}`}});
|
||||
const pending = rows.filter((row: Record<string, unknown>) => !["published", "rejected"].includes(String(row.status)));
|
||||
pages?.setAttribute("hidden", "");
|
||||
const rows = await json(`${root.dataset.apiUrl}/api/v1/admin/external-observations?status=review&limit=51&offset=${offset}`, {headers:{Authorization:`Bearer ${token}`}});
|
||||
if (!rows.length && offset > 0) { offset = 0; return loadQueue(); }
|
||||
const pending = rows.slice(0, 50);
|
||||
if (pages) pages.hidden = !pending.length;
|
||||
if (previous) previous.disabled = offset === 0;
|
||||
if (next) next.disabled = rows.length <= 50;
|
||||
if (pageNumber) pageNumber.textContent = `Страница ${offset / 50 + 1}`;
|
||||
if (!pending.length) { list.innerHTML = '<div class="state"><h2>Очередь пуста</h2><p>Все внешние записи обработаны.</p></div>'; return; }
|
||||
list.innerHTML = pending.map((row: Record<string, unknown>) => { const complete = row.x != null && row.y != null && row.weight_g != null; return `<article class="moderation-card" data-observation-id="${esc(row.id)}"><div class="moderation-summary"><span class="activity-pill"><i></i>${esc(row.source_system)} · ${esc(row.status)}</span><h2>${esc(row.fish_name)}</h2><p>${esc(row.waterbody_name)}</p><dl><div><dt>Координаты</dt><dd>${row.x == null || row.y == null ? "нет" : `${esc(row.x)}:${esc(row.y)}`}</dd></div><div><dt>Вес</dt><dd>${row.weight_g == null ? "нет" : `${esc(row.weight_g)} г`}</dd></div><div><dt>ID источника</dt><dd>${esc(row.source_external_id)}</dd></div><div><dt>Состояние</dt><dd>${complete ? "полная запись" : "неполная"}</dd></div></dl><a href="${esc(row.source_url)}" target="_blank" rel="noreferrer">Открыть первоисточник</a></div><div class="moderation-actions"><label>Каноническая рыба<select name="fish" required><option value="">Выберите…</option>${options(fishes, row.fish_slug)}</select></label><label>Канонический водоём<select name="waterbody" required><option value="">Выберите…</option>${options(waters, row.waterbody_slug)}</select></label><label>Примечание<textarea name="note" rows="2" maxlength="1000">${esc(row.review_note ?? "")}</textarea></label><p data-alias-message role="status"></p><div><button type="button" data-suggest>Подсказать соответствия</button><button type="button" data-map>Сопоставить</button><button type="button" data-publish ${complete && row.status === "ready" ? "" : "disabled"}>Опубликовать</button><button class="reject" type="button" data-reject>Отклонить</button></div></div></article>`; }).join("");
|
||||
}
|
||||
login?.addEventListener("submit", async event => { event.preventDefault(); token = String(new FormData(login).get("token") || ""); try { await loadQueue(); login.hidden = true; } catch (cause) { fail(cause instanceof Error ? cause.message : "Ошибка загрузки."); } });
|
||||
async function changePage(delta: number) {
|
||||
const oldOffset = offset;
|
||||
offset = Math.max(0, offset + delta);
|
||||
if (previous) previous.disabled = true;
|
||||
if (next) next.disabled = true;
|
||||
try { await loadQueue(); }
|
||||
catch { offset = oldOffset; fail("Не удалось загрузить страницу. Повторите вход в очередь."); if (login) login.hidden = false; }
|
||||
}
|
||||
previous?.addEventListener("click", () => changePage(-50));
|
||||
next?.addEventListener("click", () => changePage(50));
|
||||
login?.addEventListener("submit", async event => { event.preventDefault(); offset = 0; token = String(new FormData(login).get("token") || ""); try { await loadQueue(); login.hidden = true; } catch (cause) { fail(cause instanceof Error ? cause.message : "Ошибка загрузки."); } });
|
||||
list?.addEventListener("click", async event => { const button = (event.target as HTMLElement).closest<HTMLButtonElement>("button"); const card = button?.closest<HTMLElement>("[data-observation-id]"); if (!button || !card || !root) return; button.disabled = true; const base = `${root.dataset.apiUrl}/api/v1/admin/external-observations/${card.dataset.observationId}`; const headers = {Authorization:`Bearer ${token}`,"Content-Type":"application/json"}; try {
|
||||
if (button.hasAttribute("data-suggest")) {
|
||||
const suggestion = await json(`${base}/alias-suggestions`, {headers});
|
||||
|
||||
@@ -9,6 +9,11 @@ try {
|
||||
fish = fishes.find(item => item.slug === slug);
|
||||
if (fish) items = await api<Activity[]>(`/api/v1/activity?hours=72&fish=${encodeURIComponent(fish.slug)}&limit=100`);
|
||||
} catch { unavailable = true; }
|
||||
if (unavailable) {
|
||||
Astro.response.status = 503;
|
||||
Astro.response.headers.set("Retry-After", "60");
|
||||
Astro.response.headers.set("Cache-Control", "no-store");
|
||||
}
|
||||
if (!fish && !unavailable) Astro.response.status = 404;
|
||||
const waters = [...new Map(items.map(item => [item.waterbody_slug, item.waterbody])).entries()];
|
||||
const schema = fish ? { "@context":"https://schema.org", "@type":"CollectionPage", name:`Где ловить ${fish.name_ru} в RF4`, url:`https://rf4spotter.ru/fish/${fish.slug}` } : null;
|
||||
|
||||
@@ -4,6 +4,11 @@ import FishSilhouette from "../../components/FishSilhouette.astro";
|
||||
import { api, type DictionaryItem } from "../../lib/api";
|
||||
let fishes: DictionaryItem[] = [], unavailable = false;
|
||||
try { fishes = await api<DictionaryItem[]>("/api/v1/fishes?limit=500"); } catch { unavailable = true; }
|
||||
if (unavailable) {
|
||||
Astro.response.status = 503;
|
||||
Astro.response.headers.set("Retry-After", "60");
|
||||
Astro.response.headers.set("Cache-Control", "no-store");
|
||||
}
|
||||
---
|
||||
<Layout title="Все виды рыб Russian Fishing 4 — RF4 Spotter" description="Каталог рыб RF4 со свежими точками, уловами, приманками и прозрачными источниками данных.">
|
||||
<section class="catalog-hero content-grid"><span class="overline">Справочник RF4</span><h1>Рыбы</h1><p>Выберите вид, чтобы увидеть свежие подтверждённые точки и полевые сигналы.</p></section>
|
||||
|
||||
@@ -9,6 +9,11 @@ try {
|
||||
water = waters.find(item => item.slug === slug);
|
||||
if (water) items = await api<Activity[]>(`/api/v1/activity?hours=72&waterbody=${encodeURIComponent(water.slug)}&limit=100`);
|
||||
} catch { unavailable = true; }
|
||||
if (unavailable) {
|
||||
Astro.response.status = 503;
|
||||
Astro.response.headers.set("Retry-After", "60");
|
||||
Astro.response.headers.set("Cache-Control", "no-store");
|
||||
}
|
||||
if (!water && !unavailable) Astro.response.status = 404;
|
||||
const fishes = [...new Map(items.map(item => [item.fish_slug, item.fish])).entries()];
|
||||
const schema = water ? { "@context":"https://schema.org", "@type":"CollectionPage", name:`Что ловить на ${water.name_ru} в RF4`, url:`https://rf4spotter.ru/waterbodies/${water.slug}` } : null;
|
||||
|
||||
@@ -9,6 +9,11 @@ try {
|
||||
water = waters.find(item => item.slug === slug); fish = fishes.find(item => item.slug === fishSlug);
|
||||
if (water && fish) items = await api<Activity[]>(`/api/v1/activity?hours=72&waterbody=${encodeURIComponent(water.slug)}&fish=${encodeURIComponent(fish.slug)}&limit=100`);
|
||||
} catch { unavailable = true; }
|
||||
if (unavailable) {
|
||||
Astro.response.status = 503;
|
||||
Astro.response.headers.set("Retry-After", "60");
|
||||
Astro.response.headers.set("Cache-Control", "no-store");
|
||||
}
|
||||
if ((!water || !fish) && !unavailable) Astro.response.status = 404;
|
||||
const valid = Boolean(water && fish);
|
||||
const schema = valid ? { "@type":"CollectionPage", name:`${fish!.name_ru} на ${water!.name_ru} в RF4`, url:`https://rf4spotter.ru/waterbodies/${water!.slug}/${fish!.slug}` } : null;
|
||||
|
||||
@@ -3,6 +3,11 @@ import Layout from "../../layouts/Layout.astro";
|
||||
import { api, type DictionaryItem } from "../../lib/api";
|
||||
let waters: DictionaryItem[] = [], unavailable = false;
|
||||
try { waters = await api<DictionaryItem[]>("/api/v1/waterbodies?limit=500"); } catch { unavailable = true; }
|
||||
if (unavailable) {
|
||||
Astro.response.status = 503;
|
||||
Astro.response.headers.set("Retry-After", "60");
|
||||
Astro.response.headers.set("Cache-Control", "no-store");
|
||||
}
|
||||
---
|
||||
<Layout title="Все водоёмы Russian Fishing 4 — RF4 Spotter" description="Каталог водоёмов RF4 со свежими точками, рыбами, приманками и прозрачными источниками данных.">
|
||||
<section class="catalog-hero content-grid"><span class="overline">Карта водоёмов RF4</span><h1>Водоёмы</h1><p>Откройте водоём, чтобы увидеть активные виды рыб и последние подтверждённые точки.</p></section>
|
||||
|
||||
Reference in New Issue
Block a user