feat: 更新存储策略相关逻辑,使用 UUID 替代 int 类型,优化响应模型
This commit is contained in:
@@ -148,7 +148,6 @@ async def init_default_group() -> None:
|
|||||||
if not await Group.get(session, Group.name == "管理员"):
|
if not await Group.get(session, Group.name == "管理员"):
|
||||||
admin_group = Group(
|
admin_group = Group(
|
||||||
name="管理员",
|
name="管理员",
|
||||||
policies="1",
|
|
||||||
max_storage=1 * 1024 * 1024 * 1024, # 1GB
|
max_storage=1 * 1024 * 1024 * 1024, # 1GB
|
||||||
share_enabled=True,
|
share_enabled=True,
|
||||||
web_dav_enabled=True,
|
web_dav_enabled=True,
|
||||||
@@ -194,7 +193,6 @@ async def init_default_group() -> None:
|
|||||||
if not await Group.get(session, Group.name == "游客"):
|
if not await Group.get(session, Group.name == "游客"):
|
||||||
guest_group = Group(
|
guest_group = Group(
|
||||||
name="游客",
|
name="游客",
|
||||||
policies="[]",
|
|
||||||
share_enabled=False,
|
share_enabled=False,
|
||||||
web_dav_enabled=False,
|
web_dav_enabled=False,
|
||||||
)
|
)
|
||||||
@@ -210,6 +208,7 @@ async def init_default_user() -> None:
|
|||||||
from .user import User
|
from .user import User
|
||||||
from .group import Group
|
from .group import Group
|
||||||
from .object import Object, ObjectType
|
from .object import Object, ObjectType
|
||||||
|
from .policy import Policy
|
||||||
from .database import get_session
|
from .database import get_session
|
||||||
|
|
||||||
log.info('初始化管理员用户...')
|
log.info('初始化管理员用户...')
|
||||||
@@ -224,6 +223,12 @@ async def init_default_user() -> None:
|
|||||||
if not admin_group:
|
if not admin_group:
|
||||||
raise RuntimeError("管理员用户组不存在,无法创建管理员用户")
|
raise RuntimeError("管理员用户组不存在,无法创建管理员用户")
|
||||||
|
|
||||||
|
# 获取默认存储策略
|
||||||
|
default_policy = await Policy.get(session, Policy.name == "本地存储")
|
||||||
|
if not default_policy:
|
||||||
|
raise RuntimeError("默认存储策略不存在,无法创建管理员用户")
|
||||||
|
default_policy_id = default_policy.id # 在后续 save 前保存 UUID
|
||||||
|
|
||||||
# 生成管理员密码
|
# 生成管理员密码
|
||||||
admin_password = Password.generate(8)
|
admin_password = Password.generate(8)
|
||||||
hashed_admin_password = Password.hash(admin_password)
|
hashed_admin_password = Password.hash(admin_password)
|
||||||
@@ -245,7 +250,7 @@ async def init_default_user() -> None:
|
|||||||
type=ObjectType.FOLDER,
|
type=ObjectType.FOLDER,
|
||||||
owner_id=admin_user_id,
|
owner_id=admin_user_id,
|
||||||
parent_id=None,
|
parent_id=None,
|
||||||
policy_id=1, # 默认本地存储策略
|
policy_id=default_policy_id,
|
||||||
).save(session)
|
).save(session)
|
||||||
|
|
||||||
log.info(f'初始管理员账号: admin')
|
log.info(f'初始管理员账号: admin')
|
||||||
|
|||||||
@@ -47,8 +47,8 @@ class DirectoryCreateRequest(SQLModelBase):
|
|||||||
name: str
|
name: str
|
||||||
"""目录名称"""
|
"""目录名称"""
|
||||||
|
|
||||||
policy_id: int | None = None
|
policy_id: UUID | None = None
|
||||||
"""存储策略ID,不指定则继承父目录"""
|
"""存储策略UUID,不指定则继承父目录"""
|
||||||
|
|
||||||
|
|
||||||
class ObjectMoveRequest(SQLModelBase):
|
class ObjectMoveRequest(SQLModelBase):
|
||||||
@@ -93,8 +93,8 @@ class ObjectResponse(ObjectBase):
|
|||||||
class PolicyResponse(SQLModelBase):
|
class PolicyResponse(SQLModelBase):
|
||||||
"""存储策略响应 DTO"""
|
"""存储策略响应 DTO"""
|
||||||
|
|
||||||
id: str
|
id: UUID
|
||||||
"""策略ID"""
|
"""策略UUID"""
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
"""策略名称"""
|
"""策略名称"""
|
||||||
@@ -189,8 +189,8 @@ class Object(ObjectBase, UUIDTableBase, table=True):
|
|||||||
owner_id: UUID = Field(foreign_key="user.id", index=True)
|
owner_id: UUID = Field(foreign_key="user.id", index=True)
|
||||||
"""所有者用户UUID"""
|
"""所有者用户UUID"""
|
||||||
|
|
||||||
policy_id: int = Field(foreign_key="policy.id", index=True)
|
policy_id: UUID = Field(foreign_key="policy.id", index=True)
|
||||||
"""存储策略ID(文件直接使用,目录作为子文件的默认策略)"""
|
"""存储策略UUID(文件直接使用,目录作为子文件的默认策略)"""
|
||||||
|
|
||||||
# ==================== 关系 ====================
|
# ==================== 关系 ====================
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from typing import Optional, TYPE_CHECKING
|
|
||||||
from sqlmodel import Field, Relationship, text
|
|
||||||
from .base import TableBase
|
|
||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
|
from sqlmodel import Field, Relationship, text
|
||||||
|
|
||||||
|
from .base import UUIDTableBase
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .object import Object
|
from .object import Object
|
||||||
@@ -11,7 +12,7 @@ class PolicyType(StrEnum):
|
|||||||
LOCAL = "local"
|
LOCAL = "local"
|
||||||
S3 = "s3"
|
S3 = "s3"
|
||||||
|
|
||||||
class Policy(TableBase, table=True):
|
class Policy(UUIDTableBase, table=True):
|
||||||
"""存储策略模型"""
|
"""存储策略模型"""
|
||||||
|
|
||||||
name: str = Field(max_length=255, unique=True)
|
name: str = Field(max_length=255, unique=True)
|
||||||
@@ -62,7 +63,7 @@ class Policy(TableBase, table=True):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def create(
|
async def create(
|
||||||
policy: Optional["Policy"] = None,
|
policy: 'Policy | None' = None,
|
||||||
**kwargs
|
**kwargs
|
||||||
):
|
):
|
||||||
pass
|
pass
|
||||||
@@ -3,24 +3,24 @@
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from uuid import uuid4
|
import uuid
|
||||||
|
|
||||||
from sqlmodel import Field
|
from sqlmodel import Field
|
||||||
|
|
||||||
from .base import SQLModelBase
|
from .base import SQLModelBase
|
||||||
|
|
||||||
|
# [TODO] 未来把这拆了,直接按需返回状态码
|
||||||
class ResponseModel(SQLModelBase):
|
class ResponseModel(SQLModelBase):
|
||||||
"""通用响应模型"""
|
"""通用响应模型"""
|
||||||
|
|
||||||
code: int = Field(default=0, ge=0, lt=60000)
|
code: int = Field(default=0, ge=0, lt=60000)
|
||||||
"""系统内部状态码,0表示成功,其他表示失败"""
|
"""系统内部状态码,0表示成功,其他表示失败"""
|
||||||
|
|
||||||
data: dict[str, Any] | list[Any] | str | int | float | None = None
|
data: Any = None
|
||||||
"""响应数据"""
|
"""响应数据"""
|
||||||
|
|
||||||
msg: str | None = None
|
msg: str | None = None
|
||||||
"""响应消息,可以是错误消息或信息提示"""
|
"""响应消息,可以是错误消息或信息提示"""
|
||||||
|
|
||||||
instance_id: str = Field(default_factory=lambda: str(uuid4()))
|
instance_id: uuid.UUID = Field(default_factory=uuid.uuid4)
|
||||||
"""实例ID,用于标识请求的唯一性"""
|
"""实例ID,用于标识请求的唯一性"""
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ async def router_directory_get(
|
|||||||
]
|
]
|
||||||
|
|
||||||
policy_response = PolicyResponse(
|
policy_response = PolicyResponse(
|
||||||
id=str(policy.id),
|
id=policy.id,
|
||||||
name=policy.name,
|
name=policy.name,
|
||||||
type=policy.type.value,
|
type=policy.type.value,
|
||||||
max_size=policy.max_size,
|
max_size=policy.max_size,
|
||||||
|
|||||||
Reference in New Issue
Block a user