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
@@ -1,156 +1,8 @@
|
||||
"""
|
||||
文件模板表单验证模块
|
||||
兼容 shim(绞杀者迁移期间保留)
|
||||
=============================================================
|
||||
|
||||
提供文件模板相关的表单验证功能,包括:
|
||||
- 文件模板添加/更新表单验证
|
||||
表单已迁移至 src.modules.file_template.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.file_template import FileTemplate
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 文件模板表单验证(添加和更新共用)
|
||||
# ============================================================
|
||||
|
||||
class FileTemplateForm(BaseForm):
|
||||
"""
|
||||
文件模板表单验证类
|
||||
|
||||
用于添加和更新文件模板时的数据验证。
|
||||
|
||||
Attributes:
|
||||
id: 模板ID(更新时必填,添加时可选)
|
||||
name: 模板名称(可选,最长100字符)
|
||||
code: 模板编码(可选,最长50字符,不可重复)
|
||||
fileName: 文件名称(可选,最长255字符)
|
||||
filePath: 文件路径(可选,最长255字符)
|
||||
note: 模板备注(可选,最长255字符)
|
||||
sort: 模板排序(必填,0-99999之间的整数)
|
||||
"""
|
||||
|
||||
# ============================================================
|
||||
# 模板ID
|
||||
# ============================================================
|
||||
id = IntegerField(
|
||||
label='模板ID',
|
||||
validators=[
|
||||
Optional(), # 允许为空(添加时)
|
||||
NumberRange(min=1, message='模板ID必须大于0')
|
||||
],
|
||||
description='模板唯一标识,更新时必填,添加时自动生成'
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# 模板名称
|
||||
# ============================================================
|
||||
name = StringField(
|
||||
label='模板名称',
|
||||
validators=[
|
||||
Optional(), # 允许为空
|
||||
Length(max=100, message='模板名称长度不得超过100个字符')
|
||||
],
|
||||
description='模板显示名称'
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# 模板编码
|
||||
# ============================================================
|
||||
code = StringField(
|
||||
label='模板编码',
|
||||
validators=[
|
||||
Optional(), # 允许为空
|
||||
Length(max=50, message='模板编码长度不得超过50个字符')
|
||||
],
|
||||
description='模板唯一标识编码,用于程序调用'
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# 文件名称
|
||||
# ============================================================
|
||||
fileName = StringField(
|
||||
label='文件名称',
|
||||
validators=[
|
||||
Optional(), # 允许为空
|
||||
Length(max=255, message='文件名称长度不得超过255个字符')
|
||||
],
|
||||
description='文件名称'
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# 文件路径
|
||||
# ============================================================
|
||||
filePath = StringField(
|
||||
label='文件路径',
|
||||
validators=[
|
||||
Optional(), # 允许为空
|
||||
Length(max=255, message='文件路径长度不得超过255个字符')
|
||||
],
|
||||
description='文件存储路径'
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# 模板备注
|
||||
# ============================================================
|
||||
note = StringField(
|
||||
label='模板备注',
|
||||
validators=[
|
||||
Optional(), # 允许为空
|
||||
Length(max=255, message='模板备注长度不得超过255个字符')
|
||||
],
|
||||
description='模板补充说明'
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# 模板排序
|
||||
# ============================================================
|
||||
sort = IntegerField(
|
||||
label='模板排序',
|
||||
validators=[
|
||||
DataRequired(message='模板排序不能为空'),
|
||||
NumberRange(min=0, max=99999, message='模板排序值在0~99999之间')
|
||||
],
|
||||
description='排序值,数值越小越靠前'
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# 自定义验证方法
|
||||
# ============================================================
|
||||
|
||||
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 = FileTemplate.query.filter(
|
||||
FileTemplate.code == field.data,
|
||||
FileTemplate.is_delete == 0
|
||||
)
|
||||
|
||||
# 如果是更新操作,排除当前记录自身
|
||||
if current_id:
|
||||
query = query.filter(FileTemplate.id != current_id)
|
||||
|
||||
# 如果存在相同编码的记录,抛出验证错误
|
||||
if query.first():
|
||||
raise ValidationError("模板编码已存在")
|
||||
from src.modules.file_template.schema import * # noqa: F401,F403
|
||||
|
||||
Reference in New Issue
Block a user