Bündel 2: Zentrale Helper für DRY-Cleanup, SW by-v1114
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.
This commit is contained in:
parent
c517c9281d
commit
297bd22f96
22 changed files with 225 additions and 202 deletions
|
|
@ -1,7 +1,7 @@
|
|||
"""BAN YARO — Gassi-Routen"""
|
||||
|
||||
import datetime as _dt
|
||||
import json, math, os, uuid
|
||||
import json, os, uuid
|
||||
import httpx
|
||||
import polyline as _polyline
|
||||
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File
|
||||
|
|
@ -13,6 +13,7 @@ from routes.achievements import update_streak, check_and_award
|
|||
from timeutils import safe_client_time
|
||||
from media_utils import convert_media
|
||||
from routes.push import send_push_to_user
|
||||
from math_utils import haversine_km, haversine_m
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
|
@ -27,16 +28,6 @@ def _check_speed(distanz_km, dauer_min) -> bool:
|
|||
return (distanz_km / (dauer_min / 60)) <= _MAX_AVG_KMH
|
||||
|
||||
|
||||
def _haversine(lat1: float, lon1: float, lat2: float, lon2: float) -> float:
|
||||
R = 6_371_000
|
||||
p1 = math.radians(lat1)
|
||||
p2 = math.radians(lat2)
|
||||
dp = math.radians(lat2 - lat1)
|
||||
dl = math.radians(lon2 - lon1)
|
||||
a = math.sin(dp / 2) ** 2 + math.cos(p1) * math.cos(p2) * math.sin(dl / 2) ** 2
|
||||
return 2 * R * math.asin(math.sqrt(a))
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Schemas
|
||||
# ------------------------------------------------------------------
|
||||
|
|
@ -137,7 +128,7 @@ async def list_routes(
|
|||
if lat is not None and lon is not None:
|
||||
result = [
|
||||
r for r in result
|
||||
if r['start_lat'] and _haversine(lat, lon, r['start_lat'], r['start_lon']) <= radius
|
||||
if r['start_lat'] and haversine_m(lat, lon, r['start_lat'], r['start_lon']) <= radius
|
||||
]
|
||||
|
||||
user_id = user['id'] if user else None
|
||||
|
|
@ -429,10 +420,7 @@ async def trim_route(route_id: int, data: RouteTrim, user=Depends(get_current_us
|
|||
new_km = 0.0
|
||||
for i in range(1, len(new_track)):
|
||||
p1, p2 = new_track[i-1], new_track[i]
|
||||
dlat = math.radians(p2['lat'] - p1['lat'])
|
||||
dlon = math.radians(p2['lon'] - p1['lon'])
|
||||
a = math.sin(dlat/2)**2 + math.cos(math.radians(p1['lat'])) * math.cos(math.radians(p2['lat'])) * math.sin(dlon/2)**2
|
||||
new_km += 6371 * 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
|
||||
new_km += haversine_km(p1['lat'], p1['lon'], p2['lat'], p2['lon'])
|
||||
new_km = round(new_km, 2)
|
||||
|
||||
# Dauer proportional schätzen (Original-Pace)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue