Spaces:
Runtime error
Runtime error
Download backend/app/api/users.py from SII-gelingli/echem-agent: direct link, hf CLI and curl.
- Browser
- Download file 1.74 kB
-
https://huggingface.co/spaces/SII-gelingli/echem-agent/resolve/main/backend/app/api/users.py
- Command line
-
hf download hf://spaces/SII-gelingli/echem-agent/backend/app/api/users.py
-
curl -L -o users.py https://huggingface.co/spaces/SII-gelingli/echem-agent/resolve/main/backend/app/api/users.py
1.74 kB
| """ | |
| Users API: profile info and settings. | |
| """ | |
| from fastapi import APIRouter, Depends, HTTPException | |
| from pydantic import BaseModel, Field | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from app.core.database import get_db | |
| from app.core.security import verify_password, hash_password | |
| from app.dependencies import get_current_user | |
| from app.models.user import User | |
| from app.schemas.auth import UserResponse | |
| router = APIRouter(prefix="/users", tags=["users"]) | |
| class UpdateProfileRequest(BaseModel): | |
| email: str | None = None | |
| preferences: dict | None = None | |
| class ChangePasswordRequest(BaseModel): | |
| current_password: str | |
| new_password: str = Field(..., min_length=6) | |
| async def get_profile(user: User = Depends(get_current_user)): | |
| return user | |
| async def update_profile( | |
| req: UpdateProfileRequest, | |
| user: User = Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| if req.email is not None: | |
| user.email = req.email | |
| if req.preferences is not None: | |
| user.preferences = req.preferences | |
| await db.flush() | |
| await db.refresh(user) | |
| await db.commit() | |
| return user | |
| async def change_password( | |
| req: ChangePasswordRequest, | |
| user: User = Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| if not verify_password(req.current_password, user.password_hash): | |
| raise HTTPException(status_code=400, detail="Current password is incorrect") | |
| user.password_hash = hash_password(req.new_password) | |
| await db.flush() | |
| await db.commit() | |
| return {"message": "Password changed successfully"} | |