From f19444ef778c3543e2ae04c92c71f8b1ec5f746c Mon Sep 17 00:00:00 2001 From: FrozenChen Date: Sat, 4 Sep 2021 01:53:51 -0300 Subject: [PATCH] Implement tags --- cogs/extras.py | 37 ++++++++++++++++++- .../versions/3eb5fe5f18d4_add_tags_table.py | 36 ++++++++++++++++++ utils/crud.py | 18 +++++++++ utils/models.py | 8 ++++ 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 migrations/versions/3eb5fe5f18d4_add_tags_table.py diff --git a/cogs/extras.py b/cogs/extras.py index 40c7b1376..0a94e1bba 100644 --- a/cogs/extras.py +++ b/cogs/extras.py @@ -12,7 +12,7 @@ from typing import Union from utils.checks import is_staff from utils import crud -from utils.utils import parse_time +from utils.utils import parse_time, gen_color python_version = sys.version.split()[0] @@ -289,6 +289,41 @@ async def remindme(self, ctx, remind_in: str, *, reminder: str): await crud.add_reminder(reminder_time, ctx.author.id, reminder) await ctx.send("I will send you a reminder then.") + @commands.group(invoke_without_command=True) + async def tag(self, ctx, *, title: str = ""): + if ctx.invoked_subcommand is None: + if title: + if tag := await crud.get_tag(title): + return await ctx.send(tag.content) + else: + await ctx.send("This tag doesn't exists!") + else: + await ctx.send_help(ctx.command) + + @is_staff('Helper') + @tag.command() + async def create(self, ctx, title: str, *, content: str): + if await crud.get_tag(title): + await ctx.send("This tag already exists!") + await crud.create_tag(title=title, content=content, author=ctx.author.id) + await ctx.send("Tag created successfully") + + @tag.command() + async def search(self, ctx, query: str): + if tags := await crud.search_tags(query): + embed = discord.Embed(description='\n'.join(f'{n}. {tag.title}' for n, tag in enumerate(tags, start=1)), color=gen_color(ctx.author.id)) + await ctx.send(embed=embed) + else: + await ctx.send("No tags found.") + + @is_staff('Helper') + @tag.command() + async def delete(self, ctx, *, title: str): + if not (await crud.get_tag(title)): + return await ctx.send("This tag doesn't exists!") + await crud.delete_tag(title=title) + await ctx.send("Tag deleted successfully") + def setup(bot): bot.add_cog(Extras(bot)) diff --git a/migrations/versions/3eb5fe5f18d4_add_tags_table.py b/migrations/versions/3eb5fe5f18d4_add_tags_table.py new file mode 100644 index 000000000..73f4d3e6b --- /dev/null +++ b/migrations/versions/3eb5fe5f18d4_add_tags_table.py @@ -0,0 +1,36 @@ +"""Add tags table + +Revision ID: 3eb5fe5f18d4 +Revises: bbd00345a74a +Create Date: 2021-09-03 22:11:34.286026 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '3eb5fe5f18d4' +down_revision = 'bbd00345a74a' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('tags', + sa.Column('id', sa.BigInteger(), nullable=False), + sa.Column('title', sa.String(), nullable=False), + sa.Column('content', sa.String(), nullable=False), + sa.Column('author', sa.BigInteger(), nullable=False), + sa.ForeignKeyConstraint(['author'], ['members.id'], ), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('title') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('tags') + # ### end Alembic commands ### diff --git a/utils/crud.py b/utils/crud.py index cd895094f..5a83b5c56 100644 --- a/utils/crud.py +++ b/utils/crud.py @@ -346,3 +346,21 @@ async def get_reminders() -> list[models.RemindMeEntry]: async def remove_reminder(reminder_id: int): db_reminder = await models.RemindMeEntry.get(reminder_id) await db_reminder.delete() + + +async def create_tag(title: str, content: str, author: int): + await add_dbmember_if_not_exist(author) + await models.Tag.create(id=generate_id(), title=title, content=content, author=author) + + +async def get_tag(title: str) -> models.Tag: + return await models.Tag.query.where(models.Tag.title == title).gino.first() + + +async def search_tags(query: str) -> list[models.Tag]: + return await models.Tag.query.where(models.Tag.title.ilike(f"%{query}%")).limit(10).gino.all() + + +async def delete_tag(title: str): + db_tag = await get_tag(title) + await db_tag.delete() diff --git a/utils/models.py b/utils/models.py index b05051b86..fb2f8f7fa 100644 --- a/utils/models.py +++ b/utils/models.py @@ -132,3 +132,11 @@ class RemindMeEntry(db.Model): date = db.Column(db.TIMESTAMP, nullable=False) author = db.Column(db.BigInteger, db.ForeignKey("members.id"), nullable=False) reminder = db.Column(db.String, nullable=False) + + +class Tag(db.Model): + __tablename__ = "tags" + id = db.Column(db.BigInteger(), primary_key=True) + title = db.Column(db.String, unique=True, nullable=False) + content = db.Column(db.String, nullable=False) + author = db.Column(db.BigInteger, db.ForeignKey("members.id"), nullable=False)