mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-21 12:52:26 +00:00
chore: 批量修复优化代码与配置
1. 统一修复多处状态选项值类型从字符串转为数字 2. 优化用户更新逻辑简化代码 3. 新增ORM转Schema工具方法优化数据转换 4. 调整部门与菜单模型加载配置 5. 修复订单详情展示逻辑与样式 6. 移动主题配置到单独文件并更新gitignore
This commit is contained in:
@@ -234,12 +234,37 @@ class UserCreateSchema(CurrentUserUpdateSchema):
|
||||
return value
|
||||
|
||||
|
||||
class UserUpdateSchema(UserCreateSchema):
|
||||
class UserUpdateSchema(CurrentUserUpdateSchema):
|
||||
"""更新"""
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
last_login: DateTimeStr | None = Field(default=None, description="最后登录时间")
|
||||
username: str | None = Field(default=None, max_length=32, description="用户名")
|
||||
status: int | None = Field(default=None, ge=0, le=1, description="状态(0:正常 1:禁用)")
|
||||
description: str | None = Field(default=None, max_length=255, description="备注")
|
||||
dept_id: int | None = Field(default=None, description="部门ID")
|
||||
role_ids: list[int] | None = Field(default=[], description="角色ID列表")
|
||||
position_ids: list[int] | None = Field(default=[], description="岗位ID列表")
|
||||
|
||||
@field_validator("status")
|
||||
@classmethod
|
||||
def validate_status(cls, value: int | None):
|
||||
"""校验状态:仅支持 0(正常)、1(禁用)"""
|
||||
if value is not None and value not in {0, 1}:
|
||||
raise ValueError("状态仅支持 0(正常) 或 1(禁用)")
|
||||
return value
|
||||
|
||||
@field_validator("username")
|
||||
@classmethod
|
||||
def validate_username(cls, value: str | None):
|
||||
"""校验账号:字母开头,2-32 位"""
|
||||
if not value:
|
||||
return value
|
||||
v = value.strip()
|
||||
import re
|
||||
if not re.match(r"^[A-Za-z][A-Za-z0-9_.-]{1,31}$", v):
|
||||
raise ValueError("账号需以字母开头,2-32 位,仅允许字母、数字、_ . -")
|
||||
return v
|
||||
|
||||
|
||||
class UserOutSchema(UserUpdateSchema, BaseSchema, UserBySchema, TenantBySchema):
|
||||
|
||||
@@ -199,12 +199,8 @@ class UserService:
|
||||
if dept.status == 1:
|
||||
raise CustomException(msg="部门已被禁用")
|
||||
|
||||
# 更新用户 - 排除不应被修改的字段, 更新不更新密码
|
||||
user_dict = data.model_dump(
|
||||
exclude_unset=True,
|
||||
exclude={"role_ids", "position_ids", "last_login", "password"},
|
||||
)
|
||||
new_user = await UserCRUD(auth).update(id=id, data=user_dict)
|
||||
# 更新用户
|
||||
new_user = await UserCRUD(auth).update(id=id, data=data)
|
||||
|
||||
# 更新角色和岗位
|
||||
if data.role_ids and len(data.role_ids) > 0:
|
||||
|
||||
Reference in New Issue
Block a user