123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from fastapi import APIRouter, Depends
- from sqlalchemy.ext.asyncio import AsyncSession
- from crud.sysdata.region import crud_region
- from schemas.sysdata.region import RegionItemList
- from utils.depends import get_async_db
- router = APIRouter()
- # @router.get("/admin/regions",
- # response_model=RegionList,
- # summary="地区列表(管理用)")
- # async def get_regions(page: Optional[int] = 1,
- # size: Optional[int] = 10,
- # code: Optional[str] = "",
- # name: Optional[str] = "",
- # level: Optional[int] = 0,
- # db: AsyncSession = Depends(get_async_db)):
- #
- # # filter conditions and offsets
- # filter = {}
- # if code:
- # filter["code"] = code
- # if name:
- # filter["name"] = name
- # if level:
- # filter["level"] = level
- # offset = (page - 1) * size
- #
- # total, items = await crud_manager.find(db, Region, filter, offset, size)
- # return {"total": total, "data": items}
- # @region_router.get("/admin/regions/{id}",
- # response_model=RegionDetail,
- # summary="地区详情")
- # async def get_region(id: int = Path(..., description="地区ID"),
- # db: AsyncSession = Depends(get_async_db)):
- # item = await crud_manager.find_one(Region, {"id": id}, db)
- # parent = await crud_manager.find_one(Region, {"pcode": item.pcode}, db)
- # item.parent = {"id": parent.id, "code": parent.code, "name": parent.name}
- # return {"data": item}
- @router.get("/regions",
- response_model=RegionItemList,
- summary="地区列表(级联选择用)",
- description="地区列表,默认选择省列表。")
- async def get_regions(page: int = None,
- size: int = None,
- code: str = "",
- level: int = 1,
- db: AsyncSession = Depends(get_async_db)):
- filters = {"level": level}
- if code:
- filters["pcode"] = code
- if level != 1:
- filters["level"] = level
- # 翻页
- if ((page is not None) and (page > 0)) \
- and ((size is not None) and (size > 0)):
- offset = (page - 1) * size
- else:
- offset = size = None
- total, items = await crud_region.find_all(db,
- filters=filters,
- offset=offset,
- limit=size)
- return {"total": total, "data": items}
|