mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2026-05-09 18:27:56 +08:00
⬆️ 适配 Pydantic V2 (#484)
* ⬆️ 适配 Pydantic V2 * 🐛 修复测试报错 * 🐛 适配忘记的 from_orm * 🐛 忘记的 class-based `config` * 🐛 更新 red 适配器版本
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from abc import ABC
|
||||
|
||||
from pydantic import BaseModel
|
||||
from nonebot.compat import PYDANTIC_V2, ConfigDict
|
||||
from nonebot_plugin_saa.registries import AllSupportedPlatformTarget as UserInfo
|
||||
|
||||
from ....types import Tag, Category
|
||||
@@ -10,8 +11,12 @@ class NBESFBase(BaseModel, ABC):
|
||||
version: int # 表示nbesf格式版本,有效版本从1开始
|
||||
groups: list = []
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
if PYDANTIC_V2:
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
else:
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class SubReceipt(BaseModel):
|
||||
|
||||
@@ -6,6 +6,7 @@ from functools import partial
|
||||
from nonebot.log import logger
|
||||
from pydantic import BaseModel
|
||||
from nonebot_plugin_saa import TargetQQGroup, TargetQQPrivate
|
||||
from nonebot.compat import PYDANTIC_V2, ConfigDict, model_dump, type_validate_json, type_validate_python
|
||||
|
||||
from ..utils import NBESFParseErr
|
||||
from ....types import Tag, Category
|
||||
@@ -16,14 +17,21 @@ from ...db_config import SubscribeDupException, config
|
||||
NBESF_VERSION = 1
|
||||
|
||||
|
||||
class UserHead(BaseModel, orm_mode=True):
|
||||
class UserHead(BaseModel):
|
||||
"""Bison快递包收货信息"""
|
||||
|
||||
type: str
|
||||
uid: int
|
||||
|
||||
if PYDANTIC_V2:
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
else:
|
||||
|
||||
class Target(BaseModel, orm_mode=True):
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class Target(BaseModel):
|
||||
"""Bsion快递包发货信息"""
|
||||
|
||||
target_name: str
|
||||
@@ -31,14 +39,28 @@ class Target(BaseModel, orm_mode=True):
|
||||
platform_name: str
|
||||
default_schedule_weight: int
|
||||
|
||||
if PYDANTIC_V2:
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
else:
|
||||
|
||||
class SubPayload(BaseModel, orm_mode=True):
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class SubPayload(BaseModel):
|
||||
"""Bison快递包里的单件货物"""
|
||||
|
||||
categories: list[Category]
|
||||
tags: list[Tag]
|
||||
target: Target
|
||||
|
||||
if PYDANTIC_V2:
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
else:
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class SubPack(BaseModel):
|
||||
"""Bison给指定用户派送的快递包"""
|
||||
@@ -56,7 +78,7 @@ class SubGroup(
|
||||
结构参见`nbesf_model`下的对应版本
|
||||
"""
|
||||
|
||||
version = NBESF_VERSION
|
||||
version: int = NBESF_VERSION
|
||||
groups: list[SubPack]
|
||||
|
||||
|
||||
@@ -84,7 +106,7 @@ async def subs_receipt_gen(nbesf_data: SubGroup):
|
||||
tags=sub.tags,
|
||||
)
|
||||
try:
|
||||
await config.add_subscribe(receipt.user, **receipt.dict(exclude={"user"}))
|
||||
await config.add_subscribe(receipt.user, **model_dump(receipt, exclude={"user"}))
|
||||
except SubscribeDupException:
|
||||
logger.warning(f"!添加订阅条目 {repr(receipt)} 失败: 相同的订阅已存在")
|
||||
except Exception as e:
|
||||
@@ -96,9 +118,9 @@ async def subs_receipt_gen(nbesf_data: SubGroup):
|
||||
def nbesf_parser(raw_data: Any) -> SubGroup:
|
||||
try:
|
||||
if isinstance(raw_data, str):
|
||||
nbesf_data = SubGroup.parse_raw(raw_data)
|
||||
nbesf_data = type_validate_json(SubGroup, raw_data)
|
||||
else:
|
||||
nbesf_data = SubGroup.parse_obj(raw_data)
|
||||
nbesf_data = type_validate_python(SubGroup, raw_data)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("数据解析失败,该数据格式可能不满足NBESF格式标准!")
|
||||
|
||||
@@ -6,6 +6,7 @@ from functools import partial
|
||||
from nonebot.log import logger
|
||||
from pydantic import BaseModel
|
||||
from nonebot_plugin_saa.registries import AllSupportedPlatformTarget
|
||||
from nonebot.compat import PYDANTIC_V2, ConfigDict, model_dump, type_validate_json, type_validate_python
|
||||
|
||||
from ..utils import NBESFParseErr
|
||||
from ....types import Tag, Category
|
||||
@@ -16,7 +17,7 @@ from ...db_config import SubscribeDupException, config
|
||||
NBESF_VERSION = 2
|
||||
|
||||
|
||||
class Target(BaseModel, orm_mode=True):
|
||||
class Target(BaseModel):
|
||||
"""Bsion快递包发货信息"""
|
||||
|
||||
target_name: str
|
||||
@@ -24,14 +25,28 @@ class Target(BaseModel, orm_mode=True):
|
||||
platform_name: str
|
||||
default_schedule_weight: int
|
||||
|
||||
if PYDANTIC_V2:
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
else:
|
||||
|
||||
class SubPayload(BaseModel, orm_mode=True):
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class SubPayload(BaseModel):
|
||||
"""Bison快递包里的单件货物"""
|
||||
|
||||
categories: list[Category]
|
||||
tags: list[Tag]
|
||||
target: Target
|
||||
|
||||
if PYDANTIC_V2:
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
else:
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class SubPack(BaseModel):
|
||||
"""Bison给指定用户派送的快递包"""
|
||||
@@ -68,7 +83,7 @@ async def subs_receipt_gen(nbesf_data: SubGroup):
|
||||
tags=sub.tags,
|
||||
)
|
||||
try:
|
||||
await config.add_subscribe(receipt.user, **receipt.dict(exclude={"user"}))
|
||||
await config.add_subscribe(receipt.user, **model_dump(receipt, exclude={"user"}))
|
||||
except SubscribeDupException:
|
||||
logger.warning(f"!添加订阅条目 {repr(receipt)} 失败: 相同的订阅已存在")
|
||||
except Exception as e:
|
||||
@@ -80,9 +95,9 @@ async def subs_receipt_gen(nbesf_data: SubGroup):
|
||||
def nbesf_parser(raw_data: Any) -> SubGroup:
|
||||
try:
|
||||
if isinstance(raw_data, str):
|
||||
nbesf_data = SubGroup.parse_raw(raw_data)
|
||||
nbesf_data = type_validate_json(SubGroup, raw_data)
|
||||
else:
|
||||
nbesf_data = SubGroup.parse_obj(raw_data)
|
||||
nbesf_data = type_validate_python(SubGroup, raw_data)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("数据解析失败,该数据格式可能不满足NBESF格式标准!")
|
||||
|
||||
@@ -6,6 +6,7 @@ from sqlalchemy import select
|
||||
from nonebot.log import logger
|
||||
from sqlalchemy.sql.selectable import Select
|
||||
from nonebot_plugin_saa import PlatformTarget
|
||||
from nonebot.compat import type_validate_python
|
||||
from nonebot_plugin_datastore.db import create_session
|
||||
from sqlalchemy.orm.strategy_options import selectinload
|
||||
|
||||
@@ -37,7 +38,7 @@ async def subscribes_export(selector: Callable[[Select], Select]) -> v2.SubGroup
|
||||
user_id_sub_dict: dict[int, list[v2.SubPayload]] = defaultdict(list)
|
||||
|
||||
for sub in sub_data:
|
||||
sub_paylaod = v2.SubPayload.from_orm(sub)
|
||||
sub_paylaod = type_validate_python(v2.SubPayload, sub)
|
||||
user_id_sub_dict[sub.user_id].append(sub_paylaod)
|
||||
|
||||
for user in user_data:
|
||||
|
||||
Reference in New Issue
Block a user