Skip to content

Commit 397c954

Browse files
committed
bugfix: auth removed from /routing/search, /routing/new
1 parent 505320d commit 397c954

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

example.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ POSTGRES_TEST_DB=cars_test_db
1212
DATABASE_HOST_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432"
1313
DATABASE_URL="${DATABASE_HOST_URL}/${POSTGRES_DB}?sslmode=disable"
1414

15+
GEOAPIFY_API_KEY=your_geoapify_key
16+
1517
# Super token for trusted services. Send it as Authorization: Bearer ${API_TOKEN}.
1618
#API_TOKEN=change-me
1719

src/routers/routing.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import requests
1010
from fastapi import APIRouter, Depends, HTTPException, Query, status
11-
from sqlalchemy import func, or_
11+
from sqlalchemy import func, or_, text
1212
from sqlalchemy.exc import SQLAlchemyError
1313
from sqlalchemy.orm import Session
1414

@@ -44,6 +44,41 @@
4444
# Сначала зоны грубо сортируются по прямому расстоянию, потом лучшие идут в API.
4545
MAX_MATRIX_TARGETS = 200
4646

47+
PUBLIC_ROUTING_USER_EMAIL = "public-routing@parktrack.local"
48+
49+
50+
def _get_public_routing_user_id(db: Session) -> int:
51+
"""
52+
/routing/new теперь публичный, но routes.user_id в БД NOT NULL.
53+
Поэтому сохраняем публичные маршруты на технического пользователя.
54+
"""
55+
result = db.execute(
56+
text(
57+
"""
58+
INSERT INTO users (
59+
full_name,
60+
email,
61+
hashed_password,
62+
global_role,
63+
is_active
64+
)
65+
VALUES (
66+
'Public Routing User',
67+
:email,
68+
'disabled-public-routing-user',
69+
CAST('user' AS global_roles),
70+
TRUE
71+
)
72+
ON CONFLICT (email)
73+
DO UPDATE SET email = EXCLUDED.email
74+
RETURNING user_id
75+
"""
76+
),
77+
{"email": PUBLIC_ROUTING_USER_EMAIL},
78+
)
79+
80+
return int(result.scalar_one())
81+
4782

4883
# ---------------------------------------------------------------------------
4984
# Внутренние типы
@@ -647,7 +682,6 @@ def _selected_candidate_or_422(
647682
@router.post("/search", response_model=SearchRoutingResponse)
648683
def search_routing(
649684
body: SearchRoutingRequest,
650-
current_user: Annotated[User, require("routing.create")],
651685
db: Annotated[Session, Depends(get_db)],
652686
):
653687
try:
@@ -687,7 +721,6 @@ def search_routing(
687721
@router.post("/new", status_code=status.HTTP_201_CREATED, response_model=RouteResponse)
688722
def create_route(
689723
body: CreateRouteRequest,
690-
current_user: Annotated[User, require("routing.create")],
691724
db: Annotated[Session, Depends(get_db)],
692725
):
693726
if body.selected_zone_id is not None:
@@ -730,9 +763,10 @@ def create_route(
730763

731764
best = result.candidates[0]
732765
now = datetime.now(timezone.utc)
766+
public_user_id = _get_public_routing_user_id(db)
733767

734768
route = Route(
735-
user_id=current_user.user_id,
769+
user_id=public_user_id,
736770
mode=RouteMode(body.mode),
737771
provider=GEOAPIFY_PROVIDER_NAME,
738772
origin_latitude=body.origin.latitude,

0 commit comments

Comments
 (0)