diff --git a/api/alembic/versions/f3069f28699a_update_plugin_table.py b/api/alembic/versions/f3069f28699a_update_plugin_table.py new file mode 100644 index 00000000..a6bc9a9c --- /dev/null +++ b/api/alembic/versions/f3069f28699a_update_plugin_table.py @@ -0,0 +1,56 @@ +"""update plugin table + +Revision ID: f3069f28699a +Revises: ac21c38c5e56 +Create Date: 2024-07-11 04:20:39.013033 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = 'f3069f28699a' +down_revision: Union[str, None] = 'ac21c38c5e56' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('plugin', sa.Column( + 'name', sa.String(length=255), nullable=False)) + op.add_column('plugin', sa.Column( + 'avatar', sa.String(length=255), nullable=True)) + op.add_column('plugin', sa.Column('description', sa.Text(), nullable=True)) + op.add_column('plugin_api', sa.Column( + 'name', sa.String(length=255), nullable=True)) + op.add_column('plugin_config', sa.Column( + 'plugin_openapi_desc', sa.Text(), nullable=True)) + op.drop_column('plugin_config', 'avatar') + op.drop_column('plugin_config', 'description') + op.drop_column('plugin_config', 'name') + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('plugin_config', sa.Column('name', sa.VARCHAR( + length=255), autoincrement=False, nullable=False)) + op.add_column('plugin_config', sa.Column( + 'description', sa.TEXT(), autoincrement=False, nullable=True)) + op.add_column('plugin_config', sa.Column('avatar', sa.VARCHAR( + length=255), autoincrement=False, nullable=True)) + op.drop_column('plugin_config', 'plugin_openapi_desc') + op.drop_column('plugin_api', 'name') + op.drop_column('plugin', 'description') + op.drop_column('plugin', 'avatar') + op.drop_column('plugin', 'name') + op.alter_column('messages', 'message_type', + existing_type=sa.Enum( + 'MARKDOWN', 'TEXT', name='messagetype'), + type_=sa.VARCHAR(length=50), + existing_nullable=False) + # ### end Alembic commands ### diff --git a/api/routers/plugin/crud.py b/api/routers/plugin/crud.py index fc1ebee5..6985cd50 100644 --- a/api/routers/plugin/crud.py +++ b/api/routers/plugin/crud.py @@ -20,13 +20,21 @@ def get(session: Session, plugin_id: int) -> PluginORM | None: return session.query(PluginORM).filter(PluginORM.id == plugin_id).one_or_none() @staticmethod - def get_all(session: Session, user_id: int) -> Page[PluginModel]: + def get_user_plugin(session: Session, user_id: int) -> Page[PluginModel]: + print('user_id_test', user_id) return paginate( session.query(PluginORM) .filter(PluginORM.created_by == user_id) .order_by(PluginORM.updated_at.desc()) ) + @staticmethod + def get_all_plugin(session: Session) -> Page[PluginModel]: + return paginate( + session.query(PluginORM) + .order_by(PluginORM.updated_at.desc()) + ) + @staticmethod def update(session: Session, operator: int, plugin_model: PluginUpdate) -> int: plugin_id = plugin_model.id @@ -69,6 +77,14 @@ def get_plugin_draft(session: Session, plugin_id: int) -> PluginConfigORM | None PluginConfigORM.is_draft == True ).one_or_none() + @staticmethod + def get_or_create_plugin_draft(session: Session, operator: int, plugin_id: int,) -> PluginConfigORM: + exist = PluginConfigHelper.get_plugin_draft(session, plugin_id) + if exist is None: + return PluginConfigHelper.create(session, operator, PluginConfigCreate(plugin_id=plugin_id, plugin_openapi_desc='', is_draft=True)) + else: + return exist + @staticmethod def create(session: Session, operator: int, config_model: PluginConfigCreate) -> PluginConfigORM: now = datetime.now() diff --git a/api/routers/plugin/router.py b/api/routers/plugin/router.py index 556e9249..64235c47 100644 --- a/api/routers/plugin/router.py +++ b/api/routers/plugin/router.py @@ -22,7 +22,13 @@ def create_plugin(user_id: int, plugin: PluginCreate, session: Session = Depends @router.get("/", response_model=Page[PluginModel]) def get_plugins(user_id: int, session: Session = Depends(get_db)): - data = PluginHelper.get_all(session, user_id) + data = PluginHelper.get_user_plugin(session, user_id) + return data + + +@router.get("/", response_model=Page[PluginModel]) +def get_all_plugins(session: Session = Depends(get_db)): + data = PluginHelper.get_all_plugin(session) return data