- 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>
157 lines
5.3 KiB
Python
157 lines
5.3 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.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("模板编码已存在")
|