|
8 | 8 |
|
9 | 9 | import requests |
10 | 10 | from fastapi import APIRouter, Depends, HTTPException, Query, status |
11 | | -from sqlalchemy import func, or_ |
| 11 | +from sqlalchemy import func, or_, text |
12 | 12 | from sqlalchemy.exc import SQLAlchemyError |
13 | 13 | from sqlalchemy.orm import Session |
14 | 14 |
|
|
44 | 44 | # Сначала зоны грубо сортируются по прямому расстоянию, потом лучшие идут в API. |
45 | 45 | MAX_MATRIX_TARGETS = 200 |
46 | 46 |
|
| 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 | + |
47 | 82 |
|
48 | 83 | # --------------------------------------------------------------------------- |
49 | 84 | # Внутренние типы |
@@ -647,7 +682,6 @@ def _selected_candidate_or_422( |
647 | 682 | @router.post("/search", response_model=SearchRoutingResponse) |
648 | 683 | def search_routing( |
649 | 684 | body: SearchRoutingRequest, |
650 | | - current_user: Annotated[User, require("routing.create")], |
651 | 685 | db: Annotated[Session, Depends(get_db)], |
652 | 686 | ): |
653 | 687 | try: |
@@ -687,7 +721,6 @@ def search_routing( |
687 | 721 | @router.post("/new", status_code=status.HTTP_201_CREATED, response_model=RouteResponse) |
688 | 722 | def create_route( |
689 | 723 | body: CreateRouteRequest, |
690 | | - current_user: Annotated[User, require("routing.create")], |
691 | 724 | db: Annotated[Session, Depends(get_db)], |
692 | 725 | ): |
693 | 726 | if body.selected_zone_id is not None: |
@@ -730,9 +763,10 @@ def create_route( |
730 | 763 |
|
731 | 764 | best = result.candidates[0] |
732 | 765 | now = datetime.now(timezone.utc) |
| 766 | + public_user_id = _get_public_routing_user_id(db) |
733 | 767 |
|
734 | 768 | route = Route( |
735 | | - user_id=current_user.user_id, |
| 769 | + user_id=public_user_id, |
736 | 770 | mode=RouteMode(body.mode), |
737 | 771 | provider=GEOAPIFY_PROVIDER_NAME, |
738 | 772 | origin_latitude=body.origin.latitude, |
|
0 commit comments