mirror of
https://github.com/Balshgit/gpt_chat_bot.git
synced 2025-12-15 16:10:39 +03:00
add sqladmin (#32)
This commit is contained in:
32
bot_microservice/infra/admin.py
Normal file
32
bot_microservice/infra/admin.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import os
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqladmin import Admin, ModelView
|
||||
|
||||
from core.bot.models.chat_gpt import ChatGpt
|
||||
from settings.config import settings
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from main import Application
|
||||
|
||||
|
||||
class ChatGptAdmin(ModelView, model=ChatGpt):
|
||||
column_list = [ChatGpt.id, ChatGpt.model, ChatGpt.priority]
|
||||
column_sortable_list = [ChatGpt.priority]
|
||||
column_default_sort = ("priority", True)
|
||||
form_widget_args = {"model": {"readonly": True}}
|
||||
|
||||
can_create = False
|
||||
can_delete = False
|
||||
|
||||
|
||||
def create_admin(application: "Application") -> Admin:
|
||||
admin = Admin(
|
||||
title='Chat GPT admin',
|
||||
app=application.fastapi_app,
|
||||
engine=application.db.async_engine,
|
||||
base_url=os.path.join(settings.URL_PREFIX, "admin"),
|
||||
authentication_backend=None,
|
||||
)
|
||||
admin.add_view(ChatGptAdmin)
|
||||
return admin
|
||||
@@ -51,6 +51,10 @@ class Database:
|
||||
session.commit()
|
||||
session.close()
|
||||
|
||||
@property
|
||||
def async_engine(self) -> AsyncEngine:
|
||||
return self._async_engine
|
||||
|
||||
@asynccontextmanager
|
||||
async def session(self) -> AsyncGenerator[AsyncSession, None]:
|
||||
session: AsyncSession = self._async_session_factory()
|
||||
|
||||
@@ -9,6 +9,8 @@ from constants import LogLevelEnum
|
||||
from core.bot.app import BotApplication, BotQueue
|
||||
from core.bot.handlers import bot_event_handlers
|
||||
from core.lifetime import shutdown, startup
|
||||
from infra.admin import create_admin
|
||||
from infra.database.db_adapter import Database
|
||||
from infra.logging_conf import configure_logging
|
||||
from routers import api_router
|
||||
from settings.config import AppSettings, get_settings
|
||||
@@ -28,6 +30,7 @@ class Application:
|
||||
self.app.state.settings = settings
|
||||
self.app.state.queue = BotQueue(bot_app=bot_app)
|
||||
self.app.state.bot_app = bot_app
|
||||
self.db = Database(settings)
|
||||
self.bot_app = bot_app
|
||||
|
||||
self.app.on_event("startup")(startup(self.app, settings))
|
||||
@@ -75,7 +78,10 @@ def create_app(settings: AppSettings | None = None) -> FastAPI:
|
||||
settings = settings or get_settings()
|
||||
bot_app = BotApplication(settings=settings, handlers=bot_event_handlers.handlers)
|
||||
|
||||
return Application(settings=settings, bot_app=bot_app).fastapi_app
|
||||
application = Application(settings=settings, bot_app=bot_app)
|
||||
create_admin(application)
|
||||
|
||||
return application.fastapi_app
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
||||
Reference in New Issue
Block a user