amisadmin / fastapi-amis-admin

FastAPI-Amis-Admin is a high-performance, efficient and easily extensible FastAPI admin framework. Inspired by django-admin, and has as many powerful functions as django-admin.
http://docs.amis.work
Apache License 2.0
1.1k stars 161 forks source link

site.register_admin无法调用子包的异步函数 #184

Closed zeroChen00 closed 3 months ago

zeroChen00 commented 3 months ago

版本信息:amis@6.3.0,fastapi-amis-admin@0.7.2 1、代码写在一个文件里,运行正常。相关代码如下:

main.py

@site.register_admin
class SrcDataPage(ModelAdmin):
    pass

image

2、将注册页面拆分成独立的文件,并在主函数引用则异常。登录后页面空白:页面未注册成功

page.py

class SrcDataPage(ModelAdmin):
    pass

main.py

from app.admin.page import ScanDataPage,SrcDataPage

site.register_admin(SrcDataPage)

image

3、参考demo项目 https://github.com/amisadmin/fastapi-user-auth-demo/blob/master/backend/main.py site.register_admin(NavPageAdmin) 写法也没错啊,不理解。求指点!

ps: 项目基本完毕,结构拆分就差注册页面这一步了,含泪求助~

zeroChen00 commented 3 months ago

补充调试代码如下:

page.py

print(11)
class SrcDataPage(ModelAdmin):
    print(12)
    page_schema = PageSchema(sort=2, label='Web资产')
    model = SrcData
    list_display = [SrcData.domain,SrcData.IP,SrcData.unit,SrcData.system,SrcData.company,SrcData.linkman,SrcData.phone,SrcData.remark]
    search_fields = [SrcData.domain,SrcData.IP,SrcData.unit,SrcData.system,SrcData.company,SrcData.linkman,SrcData.remark]

    async def get_list_table(self, request: Request) -> TableCRUD:
        print(13)
        pass

print(14)
class ScanDataPage(ModelAdmin):
    print(15)
    page_schema = PageSchema(sort=1, label='扫描结果')
    model = ScanData
    list_display = [ScanData.domain,ScanData.IP,ScanData.unit,ScanData.system,ScanData.port,ScanData.server,ScanData.website,ScanData.status,ScanData.title,ScanData.finger,ScanData.remark,ScanData.version_id]
    search_fields = [ScanData.domain,ScanData.IP,ScanData.unit,ScanData.system,ScanData.server,ScanData.website,ScanData.title,ScanData.finger,ScanData.remark]

    async def get_list_table(self, request: Request) -> TableCRUD:
        print(16)
        pass

main.py

print(1)
app.include_router(router)

print(2)
site.register_admin(SrcDataPage)
print(3)
site.register_admin(ScanDataPage)

print(4)
@app.on_event("startup")
async def UserRegister():
    print(5)
    await site.db.async_run_sync(SQLModel.metadata.create_all, is_session=False)
    # 创建默认管理员,用户名: admin,密码: admin, 请及时修改密码!!!
    await auth.create_role_user("root")
    await auth.create_role_user("admin")
    # 运行site的startup方法,加载casbin策略等
    await site.router.startup()

    if not auth.enforcer.enforce("u:admin", site.unique_id, "page", "page"):
        await auth.enforcer.add_policy("u:admin", site.unique_id, "page", "page", "allow")

    print(6)
    scheduler.start()

# 挂载后台管理系统
print(7)
app.mount("/static", StaticFiles(directory="static"), name="static")
print(8)
site.mount_app(app)

print(9)
if __name__ == '__main__':
    print(10)
    uvicorn.run(app, host="127.0.0.1" ,port=8000)

代码执行后,输出print顺序如下:

11
12
14
15
1
2
3
4
WebAMS.py:196: DeprecationWarning:
        on_event is deprecated, use lifespan event handlers instead.

        Read more about it in the
        [FastAPI docs for Lifespan Events]([Lifespan Events ](https://fastapi.tiangolo.com/advanced/events/)).

  @app.on_event("startup")
7
8
9
10
INFO:     Started server process [6748]
INFO:     Waiting for application startup.
5
6
INFO:     Application startup complete.
INFO:     Uvicorn running on [http://127.0.0.1:8000 ](http://127.0.0.1:8000/) (Press CTRL+C to quit)

涉及到的两个页面未注册,界面空白。从print的顺序可以猜测,导入包时异步函数未编译,导致在注册函数时,无法进入异步函数。

zeroChen00 commented 3 months ago

写法没问题,只是拆包后要重新给用户赋页面权限。代码没有问题。