region.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from fastapi import APIRouter, Depends
  4. from sqlalchemy import asc
  5. from sqlalchemy.ext.asyncio import AsyncSession
  6. from crud.sysdata.region import crud_region
  7. from schemas.sysdata.region import RegionItemList
  8. from utils.depends import get_async_db
  9. router = APIRouter()
  10. @router.get("/regions",
  11. response_model=RegionItemList,
  12. summary="地区列表(级联选择用)",
  13. description="地区列表,默认选择省列表。")
  14. async def get_regions(page: int = None,
  15. size: int = None,
  16. code: str = "",
  17. level: int = 1,
  18. db: AsyncSession = Depends(get_async_db)):
  19. filters = {"level": level}
  20. if code:
  21. filters["pcode"] = code
  22. if level != 1:
  23. filters["level"] = level
  24. # 翻页
  25. if ((page is not None) and (page > 0)) \
  26. and ((size is not None) and (size > 0)):
  27. offset = (page - 1) * size
  28. else:
  29. offset = size = None
  30. total, items = await crud_region.find_all(db,
  31. filters=filters,
  32. offset=offset,
  33. limit=size,
  34. order_by=[asc("id")])
  35. return {"total": total, "data": items}