Skip to content

Commit 03e34c4

Browse files
committed
perf: Improved compatibility with pydantic v2
1 parent 15bb01b commit 03e34c4

4 files changed

Lines changed: 22 additions & 18 deletions

File tree

fastapi_amis_admin/admin/admin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from sqlalchemy_database import AsyncDatabase, Database
2828
from starlette import status
2929
from starlette.middleware.base import BaseHTTPMiddleware
30-
from starlette.responses import HTMLResponse, JSONResponse, Response
30+
from starlette.responses import HTMLResponse, Response
3131
from starlette.templating import Jinja2Templates
3232
from typing_extensions import Annotated, Literal
3333

@@ -432,7 +432,7 @@ async def page_parser(self, request: Request, page: Page) -> Response:
432432
if await request.body():
433433
data = deep_update(data, (await request.json()).get("_update", {}))
434434
result = BaseAmisApiOut(data=data)
435-
result = JSONResponse(result.dict())
435+
result = Response(result.amis_json(), media_type="application/json")
436436
return result
437437

438438
def register_router(self):

fastapi_amis_admin/admin/site.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from pathlib import Path
66

77
import aiofiles
8+
import pydantic
9+
import sqlalchemy
810
from fastapi import FastAPI, File, UploadFile
911
from pydantic import BaseModel
1012
from starlette.requests import Request
@@ -76,6 +78,8 @@ async def get_page(self, request: Request) -> Page:
7678
Property.Item(label="amis-cdn", content=self.site.settings.amis_cdn),
7779
Property.Item(label="amis-pkg", content=self.site.settings.amis_pkg),
7880
Property.Item(label="amis-theme", content=self.site.settings.amis_theme),
81+
Property.Item(label="sqlalchemy", content=sqlalchemy.__version__),
82+
Property.Item(label="pydantic", content=pydantic.__version__),
7983
],
8084
),
8185
]

fastapi_amis_admin/amis/components.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
try:
99
from pydantic import SerializeAsAny
1010
except ImportError:
11-
from typing import Optional as SerializeAsAny
11+
from typing import Union as SerializeAsAny
1212
from .constants import (
1313
BarcodeEnum,
1414
DisplayModeEnum,
@@ -356,8 +356,8 @@ class Toast(Action):
356356
"""Toast light"""
357357

358358
class ToastItem(AmisNode):
359-
title: SerializeAsAny[Union[str, SchemaNode]] = None # Toast Item Title
360-
body: SerializeAsAny[Union[str, SchemaNode]] = None # Toast Item Content
359+
title: SerializeAsAny[SchemaNode] = None # Toast Item Title
360+
body: SerializeAsAny[SchemaNode] = None # Toast Item Content
361361
level: str = None # default 'info', Display icon, optional 'info', 'success', 'error', 'warning'
362362
position: str = None # default 'top-center', display position,
363363
# 'top-right', 'top-center', 'top-left', 'bottom-center', 'bottom-left', 'bottom-right', 'center'
@@ -1184,8 +1184,8 @@ class CollapseItem(AmisNode):
11841184
disabled: bool = None # default False
11851185
collapsed: bool = None # default True
11861186
key: Union[int, str] = None # default -, logo
1187-
header: SerializeAsAny[Union[str, SchemaNode]] = None # default -, title
1188-
body: SerializeAsAny[Union[str, SchemaNode]] = None # default -, content
1187+
header: SerializeAsAny[SchemaNode] = None # default -, title
1188+
body: SerializeAsAny[SchemaNode] = None # default -, content
11891189

11901190
type: str = "collapse-group"
11911191
activeKey: Union[str, int, List[Union[int, str]]] = None # Initialize the key to activate the panel
@@ -1371,7 +1371,7 @@ class Limit(BaseAmisModel):
13711371
autoFill: Dict[str, str] = None # After the upload is successful, the value returned by the upload interface can
13721372
# be filled into a form item by configuring autoFill (not supported under non-form)
13731373
initAutoFill: bool = None # False # 表单反显时是否执行 autoFill
1374-
uploadBtnText: SerializeAsAny[Union[str, SchemaNode]] = None # 上传按钮文案。支持tpl、schema形式配置。
1374+
uploadBtnText: SerializeAsAny[SchemaNode] = None # 上传按钮文案。支持tpl、schema形式配置。
13751375
dropCrop: bool = None # True # 图片上传后是否进入裁剪模式
13761376
initCrop: bool = None # False # 图片选择器初始化后是否立即进入裁剪模式
13771377

@@ -1416,7 +1416,7 @@ class Picker(FormItem):
14161416
extractValue: bool = None # False # extract value
14171417
autoFill: dict = None # autofill
14181418
modalMode: Literal["dialog", "drawer"] = None # "dialog" # Set dialog or drawer to configure the popup mode.
1419-
pickerSchema: SerializeAsAny[Union["CRUD", SchemaNode]] = None # "{mode: 'list', listItem: {title: '${label}'}}"
1419+
pickerSchema: SerializeAsAny[Union[SchemaNode, "CRUD"]] = None # "{mode: 'list', listItem: {title: '${label}'}}"
14201420
# That is to use the rendering of the List type to display the list information. More configuration reference CRUD
14211421
embed: bool = None # False # whether to use embedded mode
14221422

@@ -1543,7 +1543,7 @@ class Timeline(AmisNode):
15431543

15441544
class TimelineItem(AmisNode):
15451545
time: str # Node Time
1546-
title: Union[str, SchemaNode] = None # Node Title
1546+
title: SchemaNode = None # Node Title
15471547
detail: str = None # Node detailed description (collapsed)
15481548
detailCollapsedText: str = None # default 'Expand'
15491549
detailExpandedText: str = None # default 'Collapse'
@@ -1563,9 +1563,9 @@ class Steps(AmisNode):
15631563
"""Steps Bar"""
15641564

15651565
class StepItem(AmisNode):
1566-
title: SerializeAsAny[Union[str, SchemaNode]] = None # Title
1567-
subTitle: SerializeAsAny[Union[str, SchemaNode]] = None # Sub Heading
1568-
description: SerializeAsAny[Union[str, SchemaNode]] = None # Detail Description
1566+
title: SerializeAsAny[SchemaNode] = None # Title
1567+
subTitle: SerializeAsAny[SchemaNode] = None # Sub Heading
1568+
description: SerializeAsAny[SchemaNode] = None # Detail Description
15691569
value: str = None # Step Value
15701570
icon: str = None # Icon name, support fontawesome v4 or use url (priority is higher than color)
15711571
className: str = None # Custom CSS class name
@@ -1970,8 +1970,8 @@ class InputKVS(FormItem):
19701970
type: str = "input-kvs"
19711971
addButtonText: str = None # default 'new field', butto text of the add button
19721972
draggable: bool = None # Default True, Whether to drag and drop to sort is allowed
1973-
keyItem: SerializeAsAny[Union[str, SchemaNode]] = None # key field
1974-
valueItems: SerializeAsAny[List[Union[str, SchemaNode]]] = None # items for the key
1973+
keyItem: SerializeAsAny[SchemaNode] = None # key field
1974+
valueItems: SerializeAsAny[List[SchemaNode]] = None # items for the key
19751975

19761976

19771977
class InputTimeRange(FormItem):

tests/test_amis/test_amis.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
from fastapi_amis_admin.amis.components import Page, PageSchema
1+
from fastapi_amis_admin import amis
22

33

44
def test_Page():
5-
page = Page(title="Title", body="HelloWorld!")
5+
page = amis.Page(title="Title", body="HelloWorld!")
66
page_json = r'{"type":"page","title":"Title","body":"HelloWorld!"}'
77
assert page.amis_json().replace(" ", "") == page_json
88
assert page.amis_dict() == {"type": "page", "title": "Title", "body": "HelloWorld!"}
99
assert page.amis_html().find(page_json)
1010

1111

1212
def test_extra_fields():
13-
tmp = PageSchema(schema=Page(), children=[PageSchema()], extra_field="extra field") # type: ignore
13+
tmp = amis.PageSchema(schema=amis.Page(), children=[amis.PageSchema()], extra_field="extra field") # type: ignore
1414
assert tmp.amis_dict().get("extra_field") == "extra field"

0 commit comments

Comments
 (0)