NEUE BACKEND-MODULE:
math_utils.py
- haversine_km(lat1, lon1, lat2, lon2) — Distanz in km
- haversine_m(...) — Convenience-Wrapper in Metern
- bbox_deg_from_km(lat, radius_km) — Bounding-Box-Approximation
für SQL-Vorfilter (statt Haversine im Python-Loop)
config.py
- DB_PATH, MEDIA_DIR, BREEDER_DOCS_DIR, SCANINPUT_DIR
- API_TIMEOUT_SHORT (5s) / DEFAULT (10s) / LONG (30s)
- HTTP_USER_AGENT, HTTP_HEADERS
errors.py
- not_found(msg), forbidden(msg), bad_request(msg), unauthorized(msg)
- conflict(msg), too_many_requests(msg, retry_after), service_unavailable(msg)
- require_or_404(row, msg) — Convenience-Helper
UI.JS ERWEITERUNGEN:
UI.time erweitert:
- formatDate(d) → "15.03.2026"
- formatDateTime(d) → "15.03.2026, 14:30"
- weekday(d) → "Di"
- parseISO(str) → {year, month, day}
UI.text (neu):
- truncate(str, maxLen, ellipsis='…')
- slug(str) — URL-Slug aus String (mit DE-Umlauten)
UI.money (neu):
- format(value) → "12,34 €" (de-DE, EUR)
- formatWithSuffix(value, '/Jahr')
HAVERSINE-MIGRATION (13 Backend-Routen):
alerts.py, services.py, places.py, events.py, diary.py, playdate.py,
lost.py, poison.py, adoption.py, gassi_zeiten.py, sitting.py, routen.py,
walks.py
- Alle lokalen def _haversine/haversine_km entfernt
- Aufrufe ersetzt durch haversine_km/haversine_m je nach Einheit
- from math_utils import haversine_km|haversine_m in jeder Datei
Tests 19/19 grün.
Hinweis: Migrationen für MEDIA_DIR (19 Stellen), API-Timeouts (12),
Date-Formatter im Frontend (24) und UI.text.truncate (5) sind als
Folge-Sprints möglich. Helper sind verfügbar.
183 lines
6.5 KiB
Python
183 lines
6.5 KiB
Python
"""BAN YARO — Giftköder-Alarm Routes"""
|
|
|
|
import os, uuid
|
|
from datetime import datetime, timedelta
|
|
from fastapi import APIRouter, Depends, HTTPException, Request, UploadFile, File
|
|
from pydantic import BaseModel
|
|
from typing import Optional
|
|
from database import db
|
|
from auth import get_current_user
|
|
from routes.push import send_push_nearby
|
|
from media_utils import convert_media
|
|
from ratelimit import check as rl_check
|
|
from math_utils import haversine_m
|
|
|
|
router = APIRouter()
|
|
MEDIA_DIR = os.getenv("MEDIA_DIR", "/data/media")
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
# Schemas
|
|
# ------------------------------------------------------------------
|
|
class PoisonCreate(BaseModel):
|
|
lat: float
|
|
lon: float
|
|
beschreibung: Optional[str] = None
|
|
typ: str = "unbekannt"
|
|
|
|
|
|
class PoisonResolve(BaseModel):
|
|
grund: str = "beseitigt" # beseitigt | fehlerhaft | anderes
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
# GET /api/poison — aktive Meldungen im Umkreis (kein Login nötig)
|
|
# ------------------------------------------------------------------
|
|
@router.get("")
|
|
async def list_poison(lat: float, lon: float, radius: int = 5000):
|
|
now = datetime.utcnow().isoformat()
|
|
with db() as conn:
|
|
rows = conn.execute(
|
|
"""SELECT p.*, u.name AS melder_name
|
|
FROM poison p
|
|
LEFT JOIN users u ON u.id = p.user_id
|
|
WHERE p.geloest = 0
|
|
AND p.expires_at > ?
|
|
ORDER BY p.created_at DESC""",
|
|
(now,)
|
|
).fetchall()
|
|
|
|
results = []
|
|
for r in rows:
|
|
entry = dict(r)
|
|
dist = haversine_m(lat, lon, entry["lat"], entry["lon"])
|
|
if dist <= radius:
|
|
entry["distanz_m"] = round(dist)
|
|
results.append(entry)
|
|
|
|
results.sort(key=lambda x: x["distanz_m"])
|
|
return results
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
# POST /api/poison — neue Meldung (Login erforderlich)
|
|
# ------------------------------------------------------------------
|
|
@router.post("", status_code=201)
|
|
async def report_poison(data: PoisonCreate, request: Request,
|
|
user=Depends(get_current_user)):
|
|
rl_check(request, max_requests=3, window_seconds=3600, key="poison")
|
|
expires_at = (datetime.utcnow() + timedelta(days=7)).isoformat()
|
|
with db() as conn:
|
|
conn.execute(
|
|
"""INSERT INTO poison (user_id, lat, lon, beschreibung, typ, expires_at)
|
|
VALUES (?, ?, ?, ?, ?, ?)""",
|
|
(user["id"], data.lat, data.lon, data.beschreibung, data.typ, expires_at)
|
|
)
|
|
row = conn.execute(
|
|
"SELECT * FROM poison WHERE user_id=? ORDER BY id DESC LIMIT 1",
|
|
(user["id"],)
|
|
).fetchone()
|
|
entry = dict(row)
|
|
|
|
# Push nur an User im Umkreis von 30 km
|
|
send_push_nearby(data.lat, data.lon, 30_000, {
|
|
"type": "poison_alert",
|
|
"title": "⚠️ Giftköder gemeldet!",
|
|
"body": f"{data.typ or 'Verdächtiger Fund'} in deiner Nähe — bitte vorsichtig sein.",
|
|
"data": {"page": "poison", "id": entry["id"]},
|
|
})
|
|
|
|
return entry
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
# POST /api/poison/{id}/confirm — Community-Bestätigung
|
|
# Verlängert die Laufzeit um 7 weitere Tage ab jetzt
|
|
# ------------------------------------------------------------------
|
|
@router.post("/{poison_id}/confirm")
|
|
async def confirm_poison(poison_id: int, user=Depends(get_current_user)):
|
|
with db() as conn:
|
|
entry = conn.execute(
|
|
"SELECT * FROM poison WHERE id=?", (poison_id,)
|
|
).fetchone()
|
|
if not entry:
|
|
raise HTTPException(404, "Meldung nicht gefunden.")
|
|
if dict(entry)["user_id"] == user["id"]:
|
|
raise HTTPException(400, "Eigene Meldung kann nicht bestätigt werden.")
|
|
|
|
new_expires = (datetime.utcnow() + timedelta(days=7)).isoformat()
|
|
conn.execute(
|
|
"""UPDATE poison
|
|
SET bestaetigt=1, bestaetigt_von=?, expires_at=?
|
|
WHERE id=?""",
|
|
(user["id"], new_expires, poison_id)
|
|
)
|
|
row = conn.execute("SELECT * FROM poison WHERE id=?", (poison_id,)).fetchone()
|
|
return dict(row)
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
# POST /api/poison/{id}/resolve — als erledigt markieren
|
|
# Nur der Melder selbst oder ein Admin
|
|
# ------------------------------------------------------------------
|
|
@router.post("/{poison_id}/resolve")
|
|
async def resolve_poison(
|
|
poison_id: int,
|
|
data: PoisonResolve = PoisonResolve(),
|
|
user=Depends(get_current_user)
|
|
):
|
|
with db() as conn:
|
|
entry = conn.execute(
|
|
"SELECT * FROM poison WHERE id=?", (poison_id,)
|
|
).fetchone()
|
|
if not entry:
|
|
raise HTTPException(404, "Meldung nicht gefunden.")
|
|
e = dict(entry)
|
|
if e["user_id"] != user["id"] and user.get("rolle") != "admin":
|
|
raise HTTPException(403, "Keine Berechtigung.")
|
|
# Soft-Delete: Eintrag bleibt für spätere KI-Musteranalyse erhalten
|
|
conn.execute(
|
|
"""UPDATE poison
|
|
SET geloest=1,
|
|
geloest_von=?,
|
|
geloest_at=datetime('now'),
|
|
geloest_grund=?
|
|
WHERE id=?""",
|
|
(user["id"], data.grund, poison_id)
|
|
)
|
|
return {"ok": True}
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
# POST /api/poison/{id}/photo — Foto nachträglich anhängen
|
|
# Nur vom Melder selbst
|
|
# ------------------------------------------------------------------
|
|
@router.post("/{poison_id}/photo")
|
|
async def upload_photo(
|
|
poison_id: int,
|
|
file: UploadFile = File(...),
|
|
user=Depends(get_current_user),
|
|
):
|
|
with db() as conn:
|
|
entry = conn.execute(
|
|
"SELECT id FROM poison WHERE id=? AND user_id=?",
|
|
(poison_id, user["id"])
|
|
).fetchone()
|
|
if not entry:
|
|
raise HTTPException(404, "Meldung nicht gefunden oder keine Berechtigung.")
|
|
|
|
data, ext = convert_media(await file.read(), file.filename or "")
|
|
if not ext:
|
|
ext = ".jpg"
|
|
filename = f"poison_{poison_id}_{uuid.uuid4().hex[:8]}{ext}"
|
|
path = os.path.join(MEDIA_DIR, "poison", filename)
|
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
|
|
with open(path, "wb") as f:
|
|
f.write(data)
|
|
|
|
foto_url = f"/media/poison/{filename}"
|
|
with db() as conn:
|
|
conn.execute("UPDATE poison SET foto_url=? WHERE id=?", (foto_url, poison_id))
|
|
|
|
return {"foto_url": foto_url}
|