refactor: P3b 表单层迁入 src/modules/ → schema.py(23 表单)
- 20 个模块 schema.py(auth/system/association/cms 等) - 合并 2 组:dict+dict_item、config+config_item - 重写 import:base_form/fields→src.common.forms、模型引用→src.modules - application/forms/ 全部替换为 shim 【教训】codemod 首次运行崩溃后重跑,读到了已替换的 shim 导致 9 个 schema.py 自引用损坏;已从 git(P3a 提交)恢复原版重跑修复 【验收】32 表单类;应用启动;pytest 5 passed Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
6de4f3bef2
commit
2586c0aace
+4
-235
@@ -1,239 +1,8 @@
|
||||
"""
|
||||
参数表单验证模块
|
||||
兼容 shim(绞杀者迁移期间保留)
|
||||
=============================================================
|
||||
|
||||
提供参数相关的表单验证功能,包括:
|
||||
- 参数添加/更新表单验证
|
||||
- 参数状态设置表单验证
|
||||
表单已迁移至 src.modules.param.schema。
|
||||
本文件仅作兼容转发,最终在 P7(切尾)阶段删除。
|
||||
"""
|
||||
|
||||
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-禁用'
|
||||
)
|
||||
from src.modules.param.schema import * # noqa: F401,F403
|
||||
|
||||
Reference in New Issue
Block a user