Sprint 10: OSM-POI-Cache, Karten-Clustering, Routen-Redesign
Karte (map.js):
- OSM Overpass API: Restaurants, Tierärzte, Parkplätze, Bänke, Wasserstellen
- Leaflet.markercluster für alle OSM-Layer
- Standort-Dot mit GPS-Genauigkeitskreis, Wake-Lock bei Aufzeichnung
- Community-Pins setzen/löschen, Meldungen, Crosshair-Placement
- Layer-Sichtbarkeit in localStorage (by_map_visible_v1)
Routen (routes.js + routen.py):
- Komoot-Stil: SVG-Track-Preview, Foto-Upload, Nearby-POIs im Detail-Modal
- Neue Felder: is_public, hunde_tauglichkeit, foto_urls
- Rate-Endpoint (POST /api/routes/{id}/rate)
- Foto-Upload (POST /api/routes/{id}/photo)
- Fix: json_extract $[-1] → $[#-1] (SQLite-kompatibler Pfad für letztes Element)
Backend (osm.py, database.py, scheduler.py):
- /api/osm/pois: OSM-Overpass-Cache mit Tile-Logik (14 Tage TTL)
- /api/osm/user-poi: Community-Marker CRUD
- /api/osm/report: Marker als ungültig melden
- Neue Tabellen: osm_pois, osm_tiles, user_map_pois, osm_reports
- Giftköder-Archiv-Job (täglich 03:00, soft-delete nach Ablauf)
- Giftköder-Archiv-Job als APScheduler-CronJob
UI: Orte-Menüpunkt entfernt (in Karte integriert), APP_VER auf 62
This commit is contained in:
parent
bf26e5faf4
commit
ebe4ce20cf
16 changed files with 3020 additions and 737 deletions
|
|
@ -1,11 +1,11 @@
|
|||
"""BAN YARO — Gassi-Routen"""
|
||||
|
||||
import json, math
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
import json, math, os, uuid
|
||||
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional, List
|
||||
from database import db
|
||||
from auth import get_current_user
|
||||
from auth import get_current_user, get_current_user_optional
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
|
@ -37,6 +37,8 @@ class RouteCreate(BaseModel):
|
|||
untergrund: Optional[str] = None # wald | asphalt | wiese | mix
|
||||
schatten: Optional[bool] = None
|
||||
leine_empfohlen: Optional[bool] = None
|
||||
is_public: Optional[bool] = True
|
||||
hunde_tauglichkeit: Optional[str] = None # eingeschränkt | gut | sehr_gut | premium
|
||||
|
||||
class RouteUpdate(BaseModel):
|
||||
name: Optional[str] = None
|
||||
|
|
@ -45,15 +47,27 @@ class RouteUpdate(BaseModel):
|
|||
untergrund: Optional[str] = None
|
||||
schatten: Optional[bool] = None
|
||||
leine_empfohlen: Optional[bool] = None
|
||||
is_public: Optional[bool] = None
|
||||
hunde_tauglichkeit: Optional[str] = None
|
||||
|
||||
|
||||
def _simplify_track(track: list, max_pts: int = 40) -> list:
|
||||
"""Reduziert GPS-Track auf max_pts Punkte für Vorschau."""
|
||||
if len(track) <= max_pts:
|
||||
return track
|
||||
step = len(track) / max_pts
|
||||
return [track[round(i * step)] for i in range(max_pts)]
|
||||
|
||||
|
||||
def _parse(row) -> dict:
|
||||
d = dict(row)
|
||||
if isinstance(d.get('gps_track'), str):
|
||||
d['gps_track'] = json.loads(d['gps_track'])
|
||||
for k in ('schatten', 'leine_empfohlen'):
|
||||
for k in ('schatten', 'leine_empfohlen', 'is_public'):
|
||||
if d.get(k) is not None:
|
||||
d[k] = bool(d[k])
|
||||
if isinstance(d.get('foto_urls'), str):
|
||||
d['foto_urls'] = json.loads(d['foto_urls'])
|
||||
return d
|
||||
|
||||
|
||||
|
|
@ -65,6 +79,7 @@ async def list_routes(
|
|||
lat: Optional[float] = None,
|
||||
lon: Optional[float] = None,
|
||||
radius: int = 10000,
|
||||
user = Depends(get_current_user_optional),
|
||||
):
|
||||
with db() as conn:
|
||||
rows = conn.execute("""
|
||||
|
|
@ -72,11 +87,12 @@ async def list_routes(
|
|||
r.distanz_km, r.dauer_min, r.schwierigkeit,
|
||||
r.untergrund, r.schatten, r.leine_empfohlen,
|
||||
r.bewertung, r.anz_bewertungen, r.created_at,
|
||||
r.gps_track, r.is_public, r.hunde_tauglichkeit, r.foto_urls,
|
||||
u.name AS user_name,
|
||||
json_extract(r.gps_track, '$[0].lat') AS start_lat,
|
||||
json_extract(r.gps_track, '$[0].lon') AS start_lon,
|
||||
json_extract(r.gps_track, '$[-1].lat') AS end_lat,
|
||||
json_extract(r.gps_track, '$[-1].lon') AS end_lon
|
||||
json_extract(r.gps_track, '$[#-1].lat') AS end_lat,
|
||||
json_extract(r.gps_track, '$[#-1].lon') AS end_lon
|
||||
FROM routes r
|
||||
LEFT JOIN users u ON u.id = r.user_id
|
||||
ORDER BY r.created_at DESC
|
||||
|
|
@ -85,9 +101,14 @@ async def list_routes(
|
|||
result = []
|
||||
for row in rows:
|
||||
d = dict(row)
|
||||
for k in ('schatten', 'leine_empfohlen'):
|
||||
for k in ('schatten', 'leine_empfohlen', 'is_public'):
|
||||
if d.get(k) is not None:
|
||||
d[k] = bool(d[k])
|
||||
if isinstance(d.get('foto_urls'), str):
|
||||
d['foto_urls'] = json.loads(d['foto_urls'])
|
||||
raw_track = json.loads(d.get('gps_track') or '[]')
|
||||
d['preview_track'] = _simplify_track(raw_track, 40)
|
||||
del d['gps_track']
|
||||
result.append(d)
|
||||
|
||||
if lat is not None and lon is not None:
|
||||
|
|
@ -95,6 +116,9 @@ async def list_routes(
|
|||
r for r in result
|
||||
if r['start_lat'] and _haversine(lat, lon, r['start_lat'], r['start_lon']) <= radius
|
||||
]
|
||||
|
||||
user_id = user['id'] if user else None
|
||||
result = [r for r in result if r.get('is_public', True) or r.get('user_id') == user_id]
|
||||
return result
|
||||
|
||||
|
||||
|
|
@ -112,13 +136,15 @@ async def create_route(data: RouteCreate, user=Depends(get_current_user)):
|
|||
cur = conn.execute("""
|
||||
INSERT INTO routes
|
||||
(user_id, name, beschreibung, gps_track, distanz_km, dauer_min,
|
||||
schwierigkeit, untergrund, schatten, leine_empfohlen)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
schwierigkeit, untergrund, schatten, leine_empfohlen, is_public, hunde_tauglichkeit)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""", (
|
||||
user['id'], data.name, data.beschreibung, gps_json,
|
||||
data.distanz_km, data.dauer_min, data.schwierigkeit, data.untergrund,
|
||||
int(data.schatten) if data.schatten is not None else None,
|
||||
int(data.leine_empfohlen) if data.leine_empfohlen is not None else None,
|
||||
int(data.is_public) if data.is_public is not None else 1,
|
||||
data.hunde_tauglichkeit,
|
||||
))
|
||||
row = conn.execute("SELECT * FROM routes WHERE id = ?", (cur.lastrowid,)).fetchone()
|
||||
return _parse(row)
|
||||
|
|
@ -153,7 +179,7 @@ async def update_route(route_id: int, data: RouteUpdate, user=Depends(get_curren
|
|||
|
||||
updates = data.model_dump(exclude_none=True)
|
||||
if updates:
|
||||
for key in ('schatten', 'leine_empfohlen'):
|
||||
for key in ('schatten', 'leine_empfohlen', 'is_public'):
|
||||
if key in updates:
|
||||
updates[key] = int(updates[key])
|
||||
cols = ', '.join(f"{k} = ?" for k in updates)
|
||||
|
|
@ -175,3 +201,61 @@ async def delete_route(route_id: int, user=Depends(get_current_user)):
|
|||
if row['user_id'] != user['id']:
|
||||
raise HTTPException(403, "Nicht berechtigt.")
|
||||
conn.execute("DELETE FROM routes WHERE id = ?", (route_id,))
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# POST /api/routes/{id}/rate — Bewertung abgeben
|
||||
# ------------------------------------------------------------------
|
||||
class RouteRate(BaseModel):
|
||||
wertung: float # 1–5
|
||||
|
||||
@router.post("/{route_id}/rate")
|
||||
async def rate_route(route_id: int, data: RouteRate, user=Depends(get_current_user)):
|
||||
if not 1 <= data.wertung <= 5:
|
||||
raise HTTPException(400, "Wertung muss zwischen 1 und 5 liegen.")
|
||||
with db() as conn:
|
||||
row = conn.execute("SELECT * FROM routes WHERE id = ?", (route_id,)).fetchone()
|
||||
if not row:
|
||||
raise HTTPException(404, "Route nicht gefunden.")
|
||||
r = dict(row)
|
||||
n = (r['anz_bewertungen'] or 0) + 1
|
||||
avg = ((r['bewertung'] or 0) * (n - 1) + data.wertung) / n
|
||||
conn.execute(
|
||||
"UPDATE routes SET bewertung=?, anz_bewertungen=? WHERE id=?",
|
||||
(round(avg, 2), n, route_id)
|
||||
)
|
||||
return {'bewertung': round(avg, 2), 'anz_bewertungen': n}
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# POST /api/routes/{id}/photo — Foto hochladen
|
||||
# ------------------------------------------------------------------
|
||||
MEDIA_DIR = os.getenv("MEDIA_DIR", "/data/media")
|
||||
|
||||
@router.post("/{route_id}/photo", status_code=201)
|
||||
async def add_route_photo(
|
||||
route_id: int,
|
||||
file: UploadFile = File(...),
|
||||
user = Depends(get_current_user),
|
||||
):
|
||||
with db() as conn:
|
||||
row = conn.execute("SELECT * FROM routes WHERE id=?", (route_id,)).fetchone()
|
||||
if not row:
|
||||
raise HTTPException(404, "Route nicht gefunden.")
|
||||
if dict(row)['user_id'] != user['id']:
|
||||
raise HTTPException(403, "Nicht berechtigt.")
|
||||
|
||||
ext = os.path.splitext(file.filename or "")[1] or ".jpg"
|
||||
filename = f"route_{route_id}_{uuid.uuid4().hex[:8]}{ext}"
|
||||
path = os.path.join(MEDIA_DIR, "routes", filename)
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
with open(path, "wb") as f:
|
||||
f.write(await file.read())
|
||||
|
||||
foto_url = f"/media/routes/{filename}"
|
||||
with db() as conn:
|
||||
row = conn.execute("SELECT foto_urls FROM routes WHERE id=?", (route_id,)).fetchone()
|
||||
urls = json.loads(dict(row)['foto_urls'] or '[]')
|
||||
urls.append(foto_url)
|
||||
conn.execute("UPDATE routes SET foto_urls=? WHERE id=?", (json.dumps(urls), route_id))
|
||||
return {'foto_url': foto_url, 'foto_urls': urls}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue