Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /opendatalab_labelU /labelu /internal /adapter /routers /user.py
| from typing import List | |
| from sqlalchemy.orm import Session | |
| from fastapi import APIRouter, status, Depends, Security | |
| from fastapi.security import HTTPAuthorizationCredentials | |
| from labelu.internal.common import db as db_module | |
| from labelu.internal.domain.models.user import User | |
| from labelu.internal.common.security import security | |
| from labelu.internal.dependencies.user import get_current_user | |
| from labelu.internal.application.response.base import MetaData, OkResp, OkRespWithMeta | |
| from labelu.internal.application.service import user as service | |
| from labelu.internal.application.command.user import LoginCommand | |
| from labelu.internal.application.command.user import SignupCommand | |
| from labelu.internal.application.response.user import LoginResponse | |
| from labelu.internal.application.response.user import LogoutResponse | |
| from labelu.internal.application.response.user import UserResponse | |
| router = APIRouter(prefix="/users", tags=["users"]) | |
| async def list_users( | |
| authorization: HTTPAuthorizationCredentials = Security(security), | |
| current_user: User = Depends(get_current_user), | |
| db: Session = Depends(db_module.get_db), | |
| page: int | None = 0, | |
| size: int | None = 10, | |
| username: str | None = None, | |
| ): | |
| """ | |
| List all users | |
| """ | |
| # business logic | |
| data, total = await service.list_by(db=db, page=page, size=size, username=username) | |
| meta_data = MetaData(total=total, page=page, size=len(data)) | |
| # response | |
| return OkRespWithMeta[List[UserResponse]](meta_data=meta_data, data=data) | |
| async def get_me( | |
| authorization: HTTPAuthorizationCredentials = Security(security), | |
| current_user: User = Depends(get_current_user), | |
| ): | |
| """ | |
| Get current user information | |
| """ | |
| # response | |
| return OkResp[UserResponse](data=UserResponse( | |
| id=current_user.id, | |
| username=current_user.username, | |
| )) | |
| async def signup(cmd: SignupCommand, db: Session = Depends(db_module.get_db)): | |
| """ | |
| User signup a account | |
| """ | |
| # business logic | |
| data = await service.signup(db=db, cmd=cmd) | |
| # response | |
| return OkResp[UserResponse](data=data) | |
| async def login(cmd: LoginCommand, db: Session = Depends(db_module.get_db)): | |
| """ | |
| User login, get an access token for future requests | |
| """ | |
| # business logic | |
| data = await service.login(db=db, cmd=cmd) | |
| # response | |
| return OkResp[LoginResponse](data=data) | |
| async def logout( | |
| authorization: HTTPAuthorizationCredentials = Security(security), | |
| db: Session = Depends(db_module.get_db), | |
| current_user: User = Depends(get_current_user), | |
| ): | |
| """ | |
| User logout. | |
| Note: With JWT token expiration enabled (30min TTL), server-side | |
| token invalidation is not strictly necessary. The client should | |
| discard the token on logout. For stricter security, implement | |
| a token blacklist with Redis. | |
| """ | |
| # response | |
| data = LogoutResponse(msg="succeeded") | |
| return OkResp[LogoutResponse](data=data) | |
Xet Storage Details
- Size:
- 3.47 kB
- Xet hash:
- 5804e2ba52fffc5f68bd9b4059fd10e10d73d15d417ceb05ea43d5d2a4a0ab5b
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.