chore: 基线提交(重构前代码快照)
- 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>
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
"""
|
||||
分类表单验证模块
|
||||
=============================================================
|
||||
|
||||
提供分类相关的表单验证功能,包括:
|
||||
- 分类添加/更新表单验证
|
||||
"""
|
||||
|
||||
from wtforms import ValidationError, IntegerField, StringField
|
||||
from wtforms.validators import NumberRange, DataRequired, Length, Optional
|
||||
|
||||
from application.forms.base_form import BaseForm
|
||||
from application.models.category import Category
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 分类表单验证(添加和更新共用)
|
||||
# ============================================================
|
||||
|
||||
class CategoryForm(BaseForm):
|
||||
"""
|
||||
分类表单验证类
|
||||
|
||||
用于添加和更新分类时的数据验证。
|
||||
添加时 id 为 None,更新时 id 必须有值。
|
||||
|
||||
Attributes:
|
||||
id: 分类ID(更新时必填,添加时可选)
|
||||
categoryName: 分类名称(必填,最长100字符,不可重复)
|
||||
categoryCode: 分类编码(可选,最长100字符,不可重复)
|
||||
parentId: 上级ID(可选,不得小于0)
|
||||
sort: 分类排序(可选,0-99999之间的整数)
|
||||
note: 分类备注(可选,最长100字符)
|
||||
type: 分类类型(必填,1-公司,2-子公司,3-部门,4-小组)
|
||||
"""
|
||||
|
||||
# ============================================================
|
||||
# 分类ID
|
||||
# ============================================================
|
||||
id = IntegerField(
|
||||
label='分类ID',
|
||||
validators=[
|
||||
Optional(), # 允许为空(添加时)
|
||||
NumberRange(min=1, message='分类ID必须大于0')
|
||||
],
|
||||
description='分类唯一标识,更新时必填,添加时自动生成'
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# 分类名称
|
||||
# ============================================================
|
||||
categoryName = StringField(
|
||||
label='分类名称',
|
||||
validators=[
|
||||
DataRequired(message='分类名称不能为空'),
|
||||
Length(max=100, message='分类名称长度不得超过100个字符')
|
||||
],
|
||||
description='分类显示名称,如:新闻资讯、产品动态'
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# 分类编码
|
||||
# ============================================================
|
||||
categoryCode = StringField(
|
||||
label='分类编码',
|
||||
validators=[
|
||||
Optional(),
|
||||
Length(max=100, message='分类编码长度不得超过100个字符')
|
||||
],
|
||||
description='分类唯一标识编码,用于程序调用'
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# 上级ID
|
||||
# ============================================================
|
||||
parentId = IntegerField(
|
||||
label='上级ID',
|
||||
validators=[
|
||||
NumberRange(min=0, message='上级ID不得小于0')
|
||||
],
|
||||
description='上级分类ID,0表示顶级分类'
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# 分类排序
|
||||
# ============================================================
|
||||
sort = IntegerField(
|
||||
label='分类排序',
|
||||
validators=[
|
||||
Optional(),
|
||||
NumberRange(min=0, max=99999, message='分类排序值在0~99999之间')
|
||||
],
|
||||
description='排序值,数值越小越靠前'
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# 分类备注
|
||||
# ============================================================
|
||||
note = StringField(
|
||||
label='分类备注',
|
||||
validators=[
|
||||
Optional(),
|
||||
Length(max=100, message='分类备注长度不得超过100个字符')
|
||||
],
|
||||
description='分类补充说明'
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# 自定义验证方法
|
||||
# ============================================================
|
||||
|
||||
def validate_categoryName(self, field):
|
||||
"""
|
||||
自定义验证:分类名称不能重复
|
||||
|
||||
验证逻辑:
|
||||
1. 添加时:检查数据库中是否存在同名记录
|
||||
2. 更新时:排除当前记录自身,检查其他记录是否同名
|
||||
|
||||
Args:
|
||||
field: categoryName 字段对象
|
||||
|
||||
Raises:
|
||||
ValidationError: 分类名称已存在时抛出验证错误
|
||||
"""
|
||||
if not field.data:
|
||||
return
|
||||
|
||||
# 获取当前记录的ID(更新时存在,添加时为None)
|
||||
current_id = self.id.data if self.id.data else None
|
||||
|
||||
# 构建查询:查找同名记录
|
||||
query = Category.query.filter(
|
||||
Category.category_name == field.data,
|
||||
Category.is_delete == 0
|
||||
)
|
||||
|
||||
# 如果是更新操作,排除当前记录自身
|
||||
if current_id:
|
||||
query = query.filter(Category.id != current_id)
|
||||
|
||||
# 如果存在同名记录,抛出验证错误
|
||||
if query.first():
|
||||
raise ValidationError("分类名称已存在")
|
||||
|
||||
def validate_categoryCode(self, field):
|
||||
"""
|
||||
自定义验证:分类编码不能重复
|
||||
|
||||
验证逻辑:
|
||||
1. 添加时:检查数据库中是否存在相同编码的记录
|
||||
2. 更新时:排除当前记录自身,检查其他记录是否编码相同
|
||||
|
||||
Args:
|
||||
field: categoryCode 字段对象
|
||||
|
||||
Raises:
|
||||
ValidationError: 分类编码已存在时抛出验证错误
|
||||
"""
|
||||
if not field.data:
|
||||
return
|
||||
|
||||
# 获取当前记录的ID(更新时存在,添加时为None)
|
||||
current_id = self.id.data if self.id.data else None
|
||||
|
||||
# 构建查询:查找相同编码的记录
|
||||
query = Category.query.filter(
|
||||
Category.category_code == field.data,
|
||||
Category.is_delete == 0
|
||||
)
|
||||
|
||||
# 如果是更新操作,排除当前记录自身
|
||||
if current_id:
|
||||
query = query.filter(Category.id != current_id)
|
||||
|
||||
# 如果存在相同编码的记录,抛出验证错误
|
||||
if query.first():
|
||||
raise ValidationError("分类编码已存在")
|
||||
Reference in New Issue
Block a user