Files
DjangoAdmin_Tornado_EleVue/src/modules/city/handlers.py
T
gitadmin 004f32dce1
CI / lint (push) Canceled after 0s
CI / test (push) Canceled after 0s
签入版本
2026-09-16 11:28:28 +08:00

138 lines
5.0 KiB
Python

# +======================================================================
# | 模块: 城市请求处理器层
# | 说明: 承接城市相关的 HTTP 请求,校验权限/演示模式并记录操作日志后,
# | 转交城市服务层处理(分页、详情、增删改、批量删除等)
# +======================================================================
"""城市请求处理器。"""
# ----------------------------------------------------------------------
# 导入依赖
# 涵盖:权限/演示模式装饰器、操作日志装饰器、统一响应对象、
# 请求处理基类、城市服务单例
# ----------------------------------------------------------------------
from common.middleware.access_decorators import check_demo, permission_required
from common.middleware.operation_log import operation_log
from core import response as R
from core.base_handler import BaseHandler
from core.database import db
from modules.city.models import City
from modules.city.service import city_service
# ======================================================================
# 查询城市数据
# ======================================================================
class CityPageHandler(BaseHandler):
"""查询城市数据。"""
@permission_required("sys:city:list")
async def get(self):
"""GET 请求:返回城市分页列表。"""
return await city_service.get_page(self)
# ======================================================================
# 查询城市详情
# ======================================================================
class CityDetailHandler(BaseHandler):
"""查询城市详情。"""
@permission_required("sys:city:detail")
async def get(self, id):
"""GET 请求:按 ID 返回城市详情。
:param id: 城市 ID(路径参数)
"""
return R.ok(self, data=city_service.get_detail(id))
# ======================================================================
# 添加城市
# ======================================================================
class CityAddHandler(BaseHandler):
"""添加城市。"""
@permission_required("sys:city:add")
@operation_log("城市管理", "添加")
@check_demo
async def post(self):
"""POST 请求:新增城市。"""
return await city_service.add(self)
# ======================================================================
# 更新城市
# ======================================================================
class CityUpdateHandler(BaseHandler):
"""更新城市。"""
@permission_required("sys:city:update")
@operation_log("城市管理", "更新")
@check_demo
async def put(self):
"""PUT 请求:更新城市信息。"""
return await city_service.update(self)
# ======================================================================
# 删除城市
# ======================================================================
class CityDeleteHandler(BaseHandler):
"""删除城市。"""
@permission_required("sys:city:delete")
@operation_log("城市管理", "删除")
@check_demo
async def delete(self, id):
"""DELETE 请求:按 ID 删除城市(软删除)。
:param id: 城市 ID(路径参数)
"""
return await city_service.delete(self, id)
# ======================================================================
# 批量删除城市
# ======================================================================
class CityBatchDeleteHandler(BaseHandler):
"""批量删除城市。"""
@permission_required("sys:city:delete")
@operation_log("城市管理", "批量删除")
@check_demo
async def post(self):
"""POST 请求:批量删除城市(软删除)。"""
return await city_service.batch_delete(self)
# ======================================================================
# 根据城市编码查询子级城市
# ======================================================================
class CityGetListHandler(BaseHandler):
"""根据城市编码查询子级城市。"""
async def get(self, city_code):
"""GET 请求:根据城市编码返回子级城市列表。
:param city_code: 城市编码(路径参数)
"""
try:
from sqlalchemy import or_
city_code = (city_code or '').strip()
# 顶级(省级)查询:兼容旧数据 '0' 哨兵值与新数据 NULL
if city_code in ('0', ''):
items = db.query(City).filter(
or_(City.parent_code == '0', City.parent_code.is_(None)),
City.is_delete == 0,
).all()
else:
# 查询子级城市
items = db.query(City).filter(
City.parent_code == city_code, City.is_delete == 0
).all()
# 返回完整结构
result = [item.to_dict() for item in items]
return R.ok(self, data=result)
finally:
db.close()