fastapi-user-auth icon indicating copy to clipboard operation
fastapi-user-auth copied to clipboard

和fastapi-scheduler搭配使用

Open cuijiahuawx opened this issue 1 year ago • 3 comments

已经用fastapi-scheduler创建了定时任务,不想随便让人通过注册账户登入后台来控制定时任务。但还想用fastapi-user-auth和fastapi_amis_admin的功能,有什么办法吗?

cuijiahuawx avatar Sep 09 '22 15:09 cuijiahuawx

class MyAuthAdminSite(AuthAdminSite):
    """自定义管理后台"""

    async def has_page_permission(self, request: Request) -> bool:
        return await self.auth.requires(response = False, roles = ['admin'])(request)

# 管理站点
site = MyAuthAdminSite(settings, engine = engine)

amisadmin avatar Sep 11 '22 15:09 amisadmin

from fastapi import FastAPI
from starlette.requests import Request
from sqlmodel import SQLModel
from fastapi_user_auth.site import AuthAdminSite
from fastapi_amis_admin.admin.settings import Settings
from fastapi_scheduler import SchedulerAdmin


app = FastAPI()


class MyAuthAdminSite(AuthAdminSite):
    """自定义管理后台"""

    async def has_page_permission(self, request: Request) -> bool:
        return await self.auth.requires(response=False, roles=['admin'])(request)


# 管理站点
site = MyAuthAdminSite(settings=Settings(database_url_async='sqlite+aiosqlite:///amisadmin.db'))
auth = site.auth

# 创建定时任务调度器`SchedulerAdmin`实例
scheduler = SchedulerAdmin.bind(site)


# 添加定时任务
@scheduler.scheduled_job('cron', hour=9, minute=0)
def cron_task_test():
    pass


# 挂载后台管理系统
site.mount_app(app)


@app.on_event("startup")
async def startup():
    # 启动定时任务调度器
    scheduler.start()
    # await site.db.async_run_sync(SQLModel.metadata.create_all, is_session=False)
    # await auth.create_role_user('admin')
    # await auth.create_role_user('vip')


if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app, debug=True)

以上代码,虽然可以正常运行。但刷新admin页面会报以下warning,这个该怎么解决那

The garbage collector is trying to clean up connection <AdaptedConnection <Connection(Thread-87, started daemon 23092)>>. This feature is unsupported on async dbapi, since no IO can be performed at this stage to reset the connection. Please close out all connections when they are no longer used, calling ``close()`` or using a context manager to manage their lifetime.
C:\Users\cjh25\scoop\apps\python\current\lib\site-packages\fastapi_user_auth\auth\models.py:193: SAWarning: The garbage collector is trying to clean up connection <AdaptedConnection <C
onnection(Thread-87, started daemon 23092)>>. This feature is unsupported on async dbapi, since no IO can be performed at this stage to reset the connection. Please close out all connections when they are no longer used, calling ``close()`` or using a context manager to manage their lifetime.
  return bool(session.scalar(stmt))

cuijiahuawx avatar Sep 14 '22 12:09 cuijiahuawx

升级sqlalchemy-database

amisadmin avatar Sep 19 '22 02:09 amisadmin