- 替换 Field(max_length=X) 为 StrX/TextX 类型别名(21 个 sqlmodels 文件) - 替换 get + 404 检查为 get_exist_one()(17 个路由文件,约 50 处) - 替换 save + session.refresh 为 save(load=...) - 替换 session.add + commit 为 save()(dav/provider.py) - 更新所有依赖至最新版本 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,23 +1,60 @@
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING
|
||||
from uuid import UUID
|
||||
|
||||
from sqlmodel import Field, Relationship, Column, func, DateTime
|
||||
from sqlalchemy import BigInteger
|
||||
from sqlmodel import Field, Relationship
|
||||
|
||||
from sqlmodel_ext import SQLModelBase, TableBaseMixin
|
||||
from sqlmodel_ext import SQLModelBase, TableBaseMixin, Str255
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .user import User
|
||||
|
||||
|
||||
# ==================== DTO 模型 ====================
|
||||
|
||||
class StoragePackResponse(SQLModelBase):
|
||||
"""容量包响应 DTO"""
|
||||
|
||||
id: int
|
||||
"""容量包ID"""
|
||||
|
||||
name: str
|
||||
"""容量包名称"""
|
||||
|
||||
size: int
|
||||
"""容量大小(字节)"""
|
||||
|
||||
active_time: datetime | None = None
|
||||
"""激活时间"""
|
||||
|
||||
expired_time: datetime | None = None
|
||||
"""过期时间"""
|
||||
|
||||
product_id: UUID | None = None
|
||||
"""来源商品UUID"""
|
||||
|
||||
|
||||
# ==================== 数据库模型 ====================
|
||||
|
||||
class StoragePack(SQLModelBase, TableBaseMixin):
|
||||
"""容量包模型"""
|
||||
|
||||
name: str = Field(max_length=255, description="容量包名称")
|
||||
active_time: datetime | None = Field(default=None, description="激活时间")
|
||||
expired_time: datetime | None = Field(default=None, index=True, description="过期时间")
|
||||
size: int = Field(description="容量包大小(字节)")
|
||||
|
||||
name: Str255
|
||||
"""容量包名称"""
|
||||
|
||||
active_time: datetime | None = None
|
||||
"""激活时间"""
|
||||
|
||||
expired_time: datetime | None = Field(default=None, index=True)
|
||||
"""过期时间"""
|
||||
|
||||
size: int = Field(sa_type=BigInteger)
|
||||
"""容量包大小(字节)"""
|
||||
|
||||
product_id: UUID | None = Field(default=None, foreign_key="product.id", ondelete="SET NULL")
|
||||
"""来源商品UUID"""
|
||||
|
||||
# 外键
|
||||
user_id: UUID = Field(
|
||||
foreign_key="user.id",
|
||||
@@ -25,6 +62,17 @@ class StoragePack(SQLModelBase, TableBaseMixin):
|
||||
ondelete="CASCADE"
|
||||
)
|
||||
"""所属用户UUID"""
|
||||
|
||||
|
||||
# 关系
|
||||
user: "User" = Relationship(back_populates="storage_packs")
|
||||
user: "User" = Relationship(back_populates="storage_packs")
|
||||
|
||||
def to_response(self) -> StoragePackResponse:
|
||||
"""转换为响应 DTO"""
|
||||
return StoragePackResponse(
|
||||
id=self.id,
|
||||
name=self.name,
|
||||
size=self.size,
|
||||
active_time=self.active_time,
|
||||
expired_time=self.expired_time,
|
||||
product_id=self.product_id,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user