mirror of
https://github.com/Balshgit/gpt_chat_bot.git
synced 2025-12-16 21:20:39 +03:00
add question count table (#73)
* update admin url * update log level * add user question count table * rename ChatGpt to ChatGptModels * change user to root in ci tests * add chatgpt_shared volume
This commit is contained in:
@@ -3,16 +3,16 @@ from typing import TYPE_CHECKING
|
||||
|
||||
from sqladmin import Admin, ModelView
|
||||
|
||||
from core.bot.models.chat_gpt import ChatGpt
|
||||
from core.bot.models.chatgpt import ChatGptModels
|
||||
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]
|
||||
class ChatGptAdmin(ModelView, model=ChatGptModels):
|
||||
column_list = [ChatGptModels.id, ChatGptModels.model, ChatGptModels.priority]
|
||||
column_sortable_list = [ChatGptModels.priority]
|
||||
column_default_sort = ("priority", True)
|
||||
form_widget_args = {"model": {"readonly": True}}
|
||||
|
||||
@@ -21,11 +21,12 @@ class ChatGptAdmin(ModelView, model=ChatGpt):
|
||||
|
||||
|
||||
def create_admin(application: "Application") -> Admin:
|
||||
base_url = os.path.join(settings.URL_PREFIX, "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"),
|
||||
base_url=base_url if base_url.startswith("/") else "/" + base_url,
|
||||
authentication_backend=None,
|
||||
)
|
||||
admin.add_view(ChatGptAdmin)
|
||||
|
||||
@@ -8,6 +8,8 @@ Create Date: 2023-10-05 18:28:30.915361
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
from core.bot.models.chatgpt import ChatGptModels
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "0001_create_chatgpt_table"
|
||||
down_revision = None
|
||||
@@ -18,7 +20,7 @@ depends_on = None
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table(
|
||||
"chatgpt",
|
||||
ChatGptModels.__tablename__,
|
||||
sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
|
||||
sa.Column("model", sa.VARCHAR(length=256), nullable=False),
|
||||
sa.Column("priority", sa.SMALLINT(), nullable=False),
|
||||
@@ -30,5 +32,5 @@ def upgrade() -> None:
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table("chatgpt")
|
||||
op.drop_table(ChatGptModels.__tablename__)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""create_auth_tables
|
||||
|
||||
Revision ID: 0003_create_users_table
|
||||
Revises: 0002_create_chatgpt_models
|
||||
Revision ID: 0002_create_users_table
|
||||
Revises: 0001_create_chatgpt_table
|
||||
Create Date: 2023-11-28 00:58:01.984654
|
||||
|
||||
"""
|
||||
@@ -17,8 +17,8 @@ from infra.database.deps import get_sync_session
|
||||
from settings.config import settings
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "0003_create_auth_tables"
|
||||
down_revision = "0002_create_chatgpt_models"
|
||||
revision = "0002_create_auth_tables"
|
||||
down_revision = "0001_create_chatgpt_table"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
"""add_user_question_count_table
|
||||
|
||||
Revision ID: 0003_create_user_question_count_table
|
||||
Revises: 0002_create_auth_tables
|
||||
Create Date: 2023-12-28 13:24:42.667724
|
||||
|
||||
"""
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "0003_create_user_question_count_table"
|
||||
down_revision = "0002_create_auth_tables"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table(
|
||||
"user_question_count",
|
||||
sa.Column("user_id", sa.INTEGER(), nullable=False),
|
||||
sa.Column("question_count", sa.INTEGER(), nullable=False),
|
||||
sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="cascade"),
|
||||
sa.PrimaryKeyConstraint("user_id"),
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table("user_question_count")
|
||||
# ### end Alembic commands ###
|
||||
@@ -1,7 +1,7 @@
|
||||
"""create chatgpt models
|
||||
|
||||
Revision ID: 0002_create_chatgpt_models
|
||||
Revises: 0001_create_chatgpt_table
|
||||
Revision ID: 0004_add_chatgpt_models
|
||||
Revises: 0003_create_user_question_count_table
|
||||
Create Date: 2025-10-05 20:44:05.414977
|
||||
|
||||
"""
|
||||
@@ -9,19 +9,19 @@ from loguru import logger
|
||||
from sqlalchemy import select, text
|
||||
|
||||
from constants import ChatGptModelsEnum
|
||||
from core.bot.models.chat_gpt import ChatGpt
|
||||
from core.bot.models.chatgpt import ChatGptModels
|
||||
from infra.database.deps import get_sync_session
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "0002_create_chatgpt_models"
|
||||
down_revision = "0001_create_chatgpt_table"
|
||||
revision = "0004_add_chatgpt_models"
|
||||
down_revision = "0003_create_user_question_count_table"
|
||||
branch_labels: str | None = None
|
||||
depends_on: str | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
with get_sync_session() as session:
|
||||
query = select(ChatGpt)
|
||||
query = select(ChatGptModels)
|
||||
results = session.execute(query)
|
||||
models = results.scalars().all()
|
||||
|
||||
@@ -29,13 +29,13 @@ def upgrade() -> None:
|
||||
return
|
||||
models = []
|
||||
for data in ChatGptModelsEnum.base_models_priority():
|
||||
models.append(ChatGpt(**data))
|
||||
models.append(ChatGptModels(**data))
|
||||
session.add_all(models)
|
||||
session.commit()
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
chatgpt_table_name = ChatGpt.__tablename__
|
||||
chatgpt_table_name = ChatGptModels.__tablename__
|
||||
with get_sync_session() as session:
|
||||
# Truncate doesn't exists for SQLite
|
||||
session.execute(text(f"""DELETE FROM {chatgpt_table_name}""")) # noqa: S608
|
||||
Reference in New Issue
Block a user