forked from iti-framework/iTi-Flask
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
3.8 KiB
Python
133 lines
3.8 KiB
Python
from apiflask import APIBlueprint
|
|
from iti.applications.extensions import db, sys_log
|
|
from iti.applications.common.utils import success, page_schema, page
|
|
from iti.applications.models import (
|
|
IotAlertPush,
|
|
IotAlertPushSchema,
|
|
)
|
|
from .schemas.alert_push import (
|
|
AlertPushQuery,
|
|
AlertPushAddRequest,
|
|
AlertPushUpdateRequest,
|
|
)
|
|
from iti.applications.common import ModelFilter
|
|
from iti.applications.common.exceptions.biz_exp import BizException
|
|
from flask_jwt_extended import jwt_required
|
|
from sqlalchemy import select, delete, exists
|
|
from sqlalchemy.sql.functions import func
|
|
from sqlalchemy.orm import noload
|
|
from iti.applications.common import permission
|
|
|
|
bp = APIBlueprint("iot_alert_push", __name__, url_prefix="/alertPush", tag="消息通知")
|
|
|
|
@bp.get("/list")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:alertPush:list")
|
|
@bp.input(AlertPushQuery.Schema(partial=True), location="query")
|
|
@bp.output(IotAlertPushSchema(many=True))
|
|
def list_alert_push(query_data: AlertPushQuery):
|
|
"""
|
|
获取消息通知列表
|
|
"""
|
|
|
|
r = get_list_or_page(query_data)
|
|
for item in r.items:
|
|
item.alert_level = list(map(int, item.alert_level.split(",")))
|
|
return success(r)
|
|
|
|
@bp.get("/page")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:alertPush:list")
|
|
@bp.input(AlertPushQuery.Schema(partial=True), location="query")
|
|
@bp.output(page_schema(IotAlertPushSchema))
|
|
def page_alert_push(query_data: AlertPushQuery):
|
|
"""
|
|
获取消息通知分页列表
|
|
"""
|
|
|
|
r = get_list_or_page(query_data)
|
|
for item in r.items:
|
|
item.alert_level = list(map(int, item.alert_level.split(",")))
|
|
return success(r)
|
|
|
|
|
|
@bp.post("/add")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:alertPush:add")
|
|
@bp.input(AlertPushAddRequest,location="json")
|
|
def add_alert_push(json_data: dict):
|
|
"""
|
|
添加消息通知
|
|
"""
|
|
|
|
if "alert_level" in json_data:
|
|
json_data["alert_level"] = ",".join(map(str, json_data["alert_level"]))
|
|
alert_push = IotAlertPush(**json_data)
|
|
alert_push.status = 0
|
|
db.session.add(alert_push)
|
|
db.session.commit()
|
|
return success()
|
|
|
|
@bp.put("/<int:id>")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:alertPush:update")
|
|
@bp.input(AlertPushUpdateRequest(partial=True), location="json")
|
|
def update_alert_push(id: int, json_data: dict):
|
|
"""
|
|
更新消息通知
|
|
"""
|
|
|
|
alert_push = db.session.scalar(select(IotAlertPush).where(IotAlertPush.id == id))
|
|
if not alert_push:
|
|
raise BizException("消息通知不存在")
|
|
|
|
for key, value in json_data.items():
|
|
if key == "alert_level":
|
|
value = ",".join(map(str, value))
|
|
if value is not None:
|
|
setattr(alert_push, key, value)
|
|
|
|
db.session.commit()
|
|
return success()
|
|
|
|
|
|
@bp.delete("/<int:id>")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:alertPush:delete")
|
|
def delete_alert_push(id: int):
|
|
"""
|
|
删除消息通知
|
|
"""
|
|
|
|
alert_push = db.session.scalar(select(IotAlertPush).where(IotAlertPush.id == id))
|
|
if not alert_push:
|
|
raise BizException("消息通知不存在")
|
|
|
|
db.session.delete(alert_push)
|
|
db.session.commit()
|
|
return success()
|
|
|
|
|
|
def get_list_or_page(query_data: AlertPushQuery):
|
|
"""
|
|
获取消息通知列表
|
|
"""
|
|
|
|
query = select(IotAlertPush).order_by(IotAlertPush.created_at.desc())
|
|
if query_data.keyword:
|
|
kw = ModelFilter.escape_like(query_data.keyword)
|
|
query = query.filter(
|
|
IotAlertPush.target_name.like(f"%{kw}%")
|
|
)
|
|
if query_data.status is not None:
|
|
query = query.filter(IotAlertPush.status == query_data.status)
|
|
if query_data.page and query_data.size:
|
|
return db.paginate(query, page=query_data.page, per_page=query_data.size)
|
|
else:
|
|
return db.session.scalars(query).all()
|