- git 初始化 + .gitignore 补全(根 .env / logs / instance / __pycache__ 等) - 修复 utils/.gitignore 误将整个 utils/ 源码层忽略的问题,补入全部工具源码(含 ip2region.xdb) - 含后端全部代码:application / extensions / utils / config / migrations - 含前端 ui/(后续整体迁走) - 不含根 .env(含密钥,本地保留) Co-Authored-By: Claude <noreply@anthropic.com>
240 lines
8.1 KiB
Python
240 lines
8.1 KiB
Python
"""
|
|
参数表单验证模块
|
|
=============================================================
|
|
|
|
提供参数相关的表单验证功能,包括:
|
|
- 参数添加/更新表单验证
|
|
- 参数状态设置表单验证
|
|
"""
|
|
|
|
from wtforms import ValidationError, StringField, IntegerField
|
|
from wtforms.validators import DataRequired, Length, NumberRange, Optional
|
|
|
|
from application.forms.base_form import BaseForm
|
|
from application.models.param import Param
|
|
|
|
|
|
# ============================================================
|
|
# 参数表单验证(添加和更新共用)
|
|
# ============================================================
|
|
|
|
class ParamForm(BaseForm):
|
|
"""
|
|
参数表单验证类
|
|
|
|
用于添加和更新参数时的数据验证。
|
|
添加时 id 为 None,更新时 id 必须有值。
|
|
|
|
Attributes:
|
|
id: 参数ID(更新时必填,添加时可选)
|
|
name: 参数名称(必填,最长100字符,不可重复)
|
|
code: 参数编码(必填,最长100字符,不可重复)
|
|
value: 参数值(必填,最长255字符)
|
|
type: 参数类型(必填,0-系统 1-业务)
|
|
status: 参数状态(必填,1-正常 2-禁用)
|
|
sort: 参数排序(必填,0-99999之间的整数)
|
|
note: 参数备注(可选,最长255字符)
|
|
"""
|
|
|
|
# ============================================================
|
|
# 参数ID
|
|
# ============================================================
|
|
id = IntegerField(
|
|
label='参数ID',
|
|
validators=[
|
|
Optional(), # 允许为空(添加时)
|
|
NumberRange(min=1, message='参数ID必须大于0')
|
|
],
|
|
description='参数唯一标识,更新时必填,添加时自动生成'
|
|
)
|
|
|
|
# ============================================================
|
|
# 参数名称
|
|
# ============================================================
|
|
name = StringField(
|
|
label='参数名称',
|
|
validators=[
|
|
DataRequired(message='参数名称不能为空'),
|
|
Length(max=100, message='参数名称长度不得超过100个字符')
|
|
],
|
|
description='参数显示名称,如:网站名称、系统版本'
|
|
)
|
|
|
|
# ============================================================
|
|
# 参数编码
|
|
# ============================================================
|
|
code = StringField(
|
|
label='参数编码',
|
|
validators=[
|
|
DataRequired(message='参数编码不能为空'),
|
|
Length(max=100, message='参数编码长度不得超过100个字符')
|
|
],
|
|
description='参数唯一标识编码,用于程序调用,如:site_name、system_version'
|
|
)
|
|
|
|
# ============================================================
|
|
# 参数值
|
|
# ============================================================
|
|
value = StringField(
|
|
label='参数值',
|
|
validators=[
|
|
DataRequired(message='参数值不能为空'),
|
|
Length(max=255, message='参数值长度不得超过255个字符')
|
|
],
|
|
description='参数的具体值'
|
|
)
|
|
|
|
# ============================================================
|
|
# 参数类型
|
|
# ============================================================
|
|
type = IntegerField(
|
|
label='参数类型',
|
|
validators=[
|
|
Optional(),
|
|
NumberRange(min=0, max=1, message='参数类型值在0~1之间')
|
|
],
|
|
description='参数类型:0-系统参数,1-业务参数'
|
|
)
|
|
|
|
# ============================================================
|
|
# 参数状态
|
|
# ============================================================
|
|
status = IntegerField(
|
|
label='参数状态',
|
|
validators=[
|
|
DataRequired(message='参数状态不能为空'),
|
|
NumberRange(min=1, max=2, message='参数状态值在1~2之间')
|
|
],
|
|
description='参数状态:1-正常,2-禁用'
|
|
)
|
|
|
|
# ============================================================
|
|
# 参数排序
|
|
# ============================================================
|
|
sort = IntegerField(
|
|
label='参数排序',
|
|
validators=[
|
|
Optional(),
|
|
NumberRange(min=0, max=99999, message='参数排序值在0~99999之间')
|
|
],
|
|
description='排序值,数值越小越靠前'
|
|
)
|
|
|
|
# ============================================================
|
|
# 参数备注
|
|
# ============================================================
|
|
note = StringField(
|
|
label='参数备注',
|
|
validators=[
|
|
Optional(), # 允许为空
|
|
Length(max=255, message='参数备注长度不得超过255个字符')
|
|
],
|
|
description='参数补充说明'
|
|
)
|
|
|
|
# ============================================================
|
|
# 自定义验证方法
|
|
# ============================================================
|
|
|
|
def validate_name(self, field):
|
|
"""
|
|
自定义验证:参数名称不能重复
|
|
|
|
验证逻辑:
|
|
1. 添加时:检查数据库中是否存在同名记录
|
|
2. 更新时:排除当前记录自身,检查其他记录是否同名
|
|
|
|
Args:
|
|
field: name 字段对象
|
|
|
|
Raises:
|
|
ValidationError: 参数名称已存在时抛出验证错误
|
|
"""
|
|
if not field.data:
|
|
return
|
|
|
|
# 获取当前记录的ID(更新时存在,添加时为None)
|
|
current_id = self.id.data if self.id.data else None
|
|
|
|
# 构建查询:查找同名记录
|
|
query = Param.query.filter(Param.name == field.data, Param.is_delete == 0)
|
|
|
|
# 如果是更新操作,排除当前记录自身
|
|
if current_id:
|
|
query = query.filter(Param.id != current_id)
|
|
|
|
# 如果存在同名记录,抛出验证错误
|
|
if query.first():
|
|
raise ValidationError("参数名称已存在")
|
|
|
|
def validate_code(self, field):
|
|
"""
|
|
自定义验证:参数编码不能重复
|
|
|
|
验证逻辑:
|
|
1. 添加时:检查数据库中是否存在相同编码的记录
|
|
2. 更新时:排除当前记录自身,检查其他记录是否编码相同
|
|
|
|
Args:
|
|
field: code 字段对象
|
|
|
|
Raises:
|
|
ValidationError: 参数编码已存在时抛出验证错误
|
|
"""
|
|
if not field.data:
|
|
return
|
|
|
|
# 获取当前记录的ID(更新时存在,添加时为None)
|
|
current_id = self.id.data if self.id.data else None
|
|
|
|
# 构建查询:查找相同编码的记录
|
|
query = Param.query.filter(Param.code == field.data, Param.is_delete == 0)
|
|
|
|
# 如果是更新操作,排除当前记录自身
|
|
if current_id:
|
|
query = query.filter(Param.id != current_id)
|
|
|
|
# 如果存在相同编码的记录,抛出验证错误
|
|
if query.first():
|
|
raise ValidationError("参数编码已存在")
|
|
|
|
|
|
# ============================================================
|
|
# 状态设置表单
|
|
# ============================================================
|
|
|
|
class ParamStatusForm(BaseForm):
|
|
"""
|
|
参数状态设置表单验证类
|
|
|
|
用于单独修改参数状态时的数据验证。
|
|
|
|
Attributes:
|
|
id: 参数ID(必填,必须大于0)
|
|
status: 参数状态(必填,1-正常 2-禁用)
|
|
"""
|
|
|
|
# ============================================================
|
|
# 参数ID
|
|
# ============================================================
|
|
id = IntegerField(
|
|
label='参数ID',
|
|
validators=[
|
|
DataRequired(message='参数ID不能为空'),
|
|
NumberRange(min=1, message='参数ID必须大于0')
|
|
],
|
|
description='要修改状态的参数ID'
|
|
)
|
|
|
|
# ============================================================
|
|
# 参数状态
|
|
# ============================================================
|
|
status = IntegerField(
|
|
label='参数状态',
|
|
validators=[
|
|
DataRequired(message='参数状态不能为空'),
|
|
NumberRange(min=1, max=2, message='参数状态值在1~2之间')
|
|
],
|
|
description='目标状态:1-正常,2-禁用'
|
|
)
|