mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-21 12:52:26 +00:00
本次提交包含多项优化: 1. 移除大量冗余的文件头注释与过时的from __future__导入 2. 将CRUD的list方法统一重命名为get_list保持接口一致 3. 修复前后端状态字段类型不匹配问题,将string类型status改为number 4. 修正前端文案错别字,将"代办事项"修正为标准写法 5. 更新sqlalchemy版本并调整依赖配置 6. 新增缓存工具类替代fastapi-cache2,重构缓存调用逻辑 7. 新增开源授权函生成相关工具与数据库字段支持 8. 为多个业务模块添加防重复提交loading状态 9. 修复邮件模型的外键关联缺失问题 10. 优化pdf生成工具的导入时机与文档注释
92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
from datetime import datetime
|
|
|
|
from app.api.v1.module_system.position.crud import PositionCRUD
|
|
from app.api.v1.module_system.role.crud import RoleCRUD
|
|
from app.core.base_crud import CRUDBase
|
|
from app.core.base_schema import AuthSchema
|
|
|
|
from .model import UserModel
|
|
from .schema import (
|
|
UserCreateSchema,
|
|
UserUpdateSchema,
|
|
)
|
|
|
|
|
|
class UserCRUD(CRUDBase[UserModel, UserCreateSchema, UserUpdateSchema]):
|
|
"""用户模块数据层"""
|
|
|
|
def __init__(self, auth: AuthSchema) -> None:
|
|
super().__init__(model=UserModel, auth=auth)
|
|
|
|
async def update_last_login(self, id: int) -> None:
|
|
"""
|
|
更新用户最后登录时间
|
|
|
|
参数:
|
|
- id (int): 用户ID
|
|
"""
|
|
await self.set([id], last_login=datetime.now())
|
|
|
|
async def set_user_roles(self, user_ids: list[int], role_ids: list[int]) -> None:
|
|
"""
|
|
批量设置用户角色
|
|
|
|
参数:
|
|
- user_ids (list[int]): 用户ID列表
|
|
- role_ids (list[int]): 角色ID列表
|
|
|
|
返回:
|
|
- None
|
|
"""
|
|
user_objs = await self.get_list(search={"id": ("in", user_ids)})
|
|
if role_ids:
|
|
role_objs = await RoleCRUD(self.auth).get_list(search={"id": ("in", role_ids)})
|
|
else:
|
|
role_objs = []
|
|
|
|
for obj in user_objs:
|
|
relationship = obj.roles
|
|
relationship.clear()
|
|
relationship.extend(role_objs)
|
|
await self.auth.db.flush()
|
|
|
|
async def set_user_positions(self, user_ids: list[int], position_ids: list[int]) -> None:
|
|
"""
|
|
批量设置用户岗位
|
|
|
|
参数:
|
|
- user_ids (list[int]): 用户ID列表
|
|
- position_ids (list[int]): 岗位ID列表
|
|
|
|
返回:
|
|
- None
|
|
"""
|
|
user_objs = await self.get_list(search={"id": ("in", user_ids)})
|
|
if position_ids:
|
|
position_objs = await PositionCRUD(self.auth).get_list(search={"id": ("in", position_ids)})
|
|
else:
|
|
position_objs = []
|
|
|
|
for obj in user_objs:
|
|
relationship = obj.positions
|
|
relationship.clear()
|
|
relationship.extend(position_objs)
|
|
await self.auth.db.flush()
|
|
|
|
async def change_password(self, id: int, password_hash: str) -> UserModel:
|
|
"""
|
|
修改用户密码
|
|
|
|
参数:
|
|
- id (int): 用户ID
|
|
- password_hash (str): 密码哈希值
|
|
|
|
返回:
|
|
- UserModel: 更新后的用户信息
|
|
"""
|
|
return await self.update(id=id, data=UserUpdateSchema(password=password_hash))
|
|
|
|
async def forget_password(self, id: int, password_hash: str) -> UserModel:
|
|
"""重置密码(与 change_password 逻辑相同)"""
|
|
return await self.change_password(id=id, password_hash=password_hash)
|