region.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from fastapi import APIRouter, Depends
  4. from sqlalchemy.ext.asyncio import AsyncSession
  5. from crud.sysdata.region import crud_region
  6. from schemas.sysdata.region import RegionItemList
  7. from utils.depends import get_async_db
  8. router = APIRouter()
  9. # @router.get("/admin/regions",
  10. # response_model=RegionList,
  11. # summary="地区列表(管理用)")
  12. # async def get_regions(page: Optional[int] = 1,
  13. # size: Optional[int] = 10,
  14. # code: Optional[str] = "",
  15. # name: Optional[str] = "",
  16. # level: Optional[int] = 0,
  17. # db: AsyncSession = Depends(get_async_db)):
  18. #
  19. # # filter conditions and offsets
  20. # filter = {}
  21. # if code:
  22. # filter["code"] = code
  23. # if name:
  24. # filter["name"] = name
  25. # if level:
  26. # filter["level"] = level
  27. # offset = (page - 1) * size
  28. #
  29. # total, items = await crud_manager.find(db, Region, filter, offset, size)
  30. # return {"total": total, "data": items}
  31. # @region_router.get("/admin/regions/{id}",
  32. # response_model=RegionDetail,
  33. # summary="地区详情")
  34. # async def get_region(id: int = Path(..., description="地区ID"),
  35. # db: AsyncSession = Depends(get_async_db)):
  36. # item = await crud_manager.find_one(Region, {"id": id}, db)
  37. # parent = await crud_manager.find_one(Region, {"pcode": item.pcode}, db)
  38. # item.parent = {"id": parent.id, "code": parent.code, "name": parent.name}
  39. # return {"data": item}
  40. @router.get("/regions",
  41. response_model=RegionItemList,
  42. summary="地区列表(级联选择用)",
  43. description="地区列表,默认选择省列表。")
  44. async def get_regions(page: int = None,
  45. size: int = None,
  46. code: str = "",
  47. level: int = 1,
  48. db: AsyncSession = Depends(get_async_db)):
  49. filters = {"level": level}
  50. if code:
  51. filters["pcode"] = code
  52. if level != 1:
  53. filters["level"] = level
  54. # 翻页
  55. if ((page is not None) and (page > 0)) \
  56. and ((size is not None) and (size > 0)):
  57. offset = (page - 1) * size
  58. else:
  59. offset = size = None
  60. total, items = await crud_region.find_all(db,
  61. filters=filters,
  62. offset=offset,
  63. limit=size)
  64. return {"total": total, "data": items}