Refactor models and routes for item management
Reorganized model structure by replacing 'object' and 'items' with a unified 'item' model using UUIDs, and moved base model logic into separate files. Updated routes to use the new item model and improved request/response handling. Enhanced user and setting models, added utility functions, and improved error handling throughout the codebase. Also added initial .idea project files and minor admin API improvements. Co-Authored-By: 砂糖橘 <54745033+Foxerine@users.noreply.github.com>
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
from loguru import logger
|
||||
from sqlmodel import select
|
||||
from .setting import Setting
|
||||
from .user import User
|
||||
from .user import User, UserTypeEnum
|
||||
from pkg import Password
|
||||
|
||||
default_settings: list[Setting] = [
|
||||
Setting(type='string', name='version', value='1.0.0'),
|
||||
Setting(type='int', name='jwt_token_exp', value='30'),
|
||||
Setting(type='string', name='server_chan_key', value=''),
|
||||
Setting(type='string', name='version', value='2.0.0'), # 版本号,用于考虑是否需要数据迁移
|
||||
Setting(type='int', name='jwt_token_exp', value='30'), # JWT Token 访问令牌
|
||||
Setting(type='string', name='server_chan_key', value=''), # Server 酱推送密钥
|
||||
]
|
||||
|
||||
async def migration(session):
|
||||
@@ -24,39 +23,32 @@ async def migration(session):
|
||||
names = [s.name for s in settings]
|
||||
existed_settings = await Setting.get(
|
||||
session,
|
||||
Setting.name.in_(names),
|
||||
fetch_mode="all"
|
||||
Setting.name in names,
|
||||
fetch_mode='all'
|
||||
)
|
||||
existed: set[str] = {s.name for s in (existed_settings or [])}
|
||||
|
||||
to_insert = [s for s in settings if s.name not in existed]
|
||||
if to_insert:
|
||||
await Setting.add(session, to_insert, refresh=False)
|
||||
|
||||
if await User.get(session, User.id == 1):
|
||||
# 已有超级管理员用户,说明不是第一次运行
|
||||
await Setting.add(session, to_insert)
|
||||
|
||||
# 修复数据库id为1的用户不是管理员的问题
|
||||
admin_user = await User.get(session, User.id == 1)
|
||||
if admin_user and not admin_user.is_admin:
|
||||
admin_user.is_admin = True
|
||||
await User.update(session, admin_user, refresh=False)
|
||||
|
||||
# 已有用户,直接返回
|
||||
return
|
||||
if not await User.get(session, User.role == UserTypeEnum.super_admin):
|
||||
# 生成初始密码与密钥
|
||||
admin_password = Password.generate()
|
||||
logger.warning("当前无管理员用户,已自动创建初始管理员用户:")
|
||||
logger.warning("邮箱: admin@yxqi.cn")
|
||||
logger.warning(f"密码: {admin_password}")
|
||||
|
||||
# 生成初始密码与密钥
|
||||
admin_password = Password.generate()
|
||||
logger.warning("当前无管理员用户,已自动创建初始管理员用户:")
|
||||
logger.warning("邮箱: admin@yxqi.cn")
|
||||
logger.warning(f"密码: {admin_password}")
|
||||
User._initializing = True
|
||||
|
||||
admin_user = User(
|
||||
id=1,
|
||||
email='admin@yxqi.cn',
|
||||
username='Admin',
|
||||
password=Password.hash(admin_password),
|
||||
is_admin=True
|
||||
)
|
||||
admin_user = User(
|
||||
email='admin@yxqi.cn',
|
||||
username='Admin',
|
||||
password=Password.hash(admin_password),
|
||||
role=UserTypeEnum.super_admin,
|
||||
_initializing=True
|
||||
)
|
||||
|
||||
await User.add(session, admin_user, refresh=False)
|
||||
await User.add(session, admin_user)
|
||||
|
||||
User._initializing = False
|
||||
|
||||
Reference in New Issue
Block a user