From 4a736b1000e63a95a54faa42424e3bd83e6062c3 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sat, 29 Jul 2017 13:20:53 +0100 Subject: [PATCH] Add quotes module --- dave/models.py | 13 +++++++++++++ dave/module.py | 1 + dave/modules/quote.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ migrations/versions/398d449f56b8_.py | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 1 deletion(-) diff --git a/dave/models.py b/dave/models.py index 205de4f..4116e14 100644 --- a/dave/models.py +++ a/dave/models.py @@ -1,5 +1,6 @@ from sqlalchemy.ext.declarative import declarative_base -from sqlalchemy import Column, Integer, String, DateTime +from sqlalchemy import Column, Integer, String, DateTime, func +from sqlalchemy.dialects.postgresql import UUID Base = declarative_base() @@ -9,3 +10,13 @@ id = Column(Integer, primary_key=True) nick = Column(String) location = Column(String) + + +class Quote(Base): + __tablename__ = "quotes" + + id = Column(UUID, server_default="uuid_generate_v4()", primary_key=True) + quote = Column(String) + attributed = Column(String) + added_by = Column(String) + created = Column(DateTime, default=func.now())diff --git a/dave/module.py b/dave/module.py index 905e54c..52e8bc2 100644 --- a/dave/module.py +++ a/dave/module.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- """Provide various decorators for dave modules.""" import re from enum import Enum +import dave.config as config def match(value): diff --git a/dave/modules/quote.py b/dave/modules/quote.py new file mode 100644 index 0000000..2f9b712 100644 --- /dev/null +++ a/dave/modules/quote.py @@ -1,0 +1,67 @@ +"""Quote system""" +import dave.module +import dave.config +import uuid +import random +from dave.models import Quote +from twisted.words.protocols.irc import assembleFormattedText, attributes as A + +@dave.module.help("Syntax: aq [quote] (-- attribute). Add a quote to the quote database.") +@dave.module.command(["aq", "addquote"], "(.*?)(?: (?:--|—) ?(.+?))?$") +def add_quote(bot, args, sender, source): + generated_uuid = str(uuid.uuid4()) + quote = Quote(id=generated_uuid, quote=args[0], attributed=args[1], added_by=sender) + dave.config.session.add(quote) + dave.config.session.commit() + + bot.reply(source, sender, assembleFormattedText( + A.normal["Successfully added quote to database: ", A.bold[args[0]], " by ", + (args[1] or sender)])) + + bot.msg(sender, "Added quote to database, you can remove this quote later using dq {}" + .format(generated_uuid)) + +@dave.module.help("Syntax: q. Return a random quote.") +@dave.module.command(["q", "quote"]) +def quote(bot, args, sender, source): + query = dave.config.session.query(Quote) + + if not query.count(): + bot.reply(source, sender, "No quotes found.") + return + + row = query.offset( + random.randrange(query.count()) + ).first() + + bot.reply(source, sender, assembleFormattedText(A.normal[ + A.bold[row.quote], " by ", (row.attributed or row.added_by) + ])) + +@dave.module.help("Syntax: fq [search]. Search for a quote in the quote database.") +@dave.module.command(["fq", "findquote"], "(.*)$") +def find_quote(bot, args, sender, source): + quotes = dave.config.session.query(Quote).filter( + (Quote.quote.op("~")(args[0])) | (Quote.attributed.op("~")(args[0])) + | (Quote.added_by.op("~")(args[0])) + ).all() + + if len(quotes) == 0: + bot.reply(source, sender, "No results for query.") + + if len(quotes) > 3: + bot.reply(source, sender, "Your query yielded too many results ({}), here's a " \ + "random sample:".format(len(quotes))) + quotes = random.sample(quotes, 3) + + for quote in quotes: + bot.reply(source, sender, assembleFormattedText(A.normal[ + A.bold[quote.quote], " by ", (quote.attributed or quote.added_by) + ])) + +@dave.module.help("Syntax: dq [uuid]. Allow the quote owner to delete a quote.") +@dave.module.command(["dq", "deletequote"], "(.*)$") +def delete_quote(bot, args, sender, source): + dave.config.session.query(Quote).filter(Quote.id == args[0]).delete() + dave.config.session.commit() + bot.reply(source, sender, "Successfully deleted quote.")diff --git a/migrations/versions/398d449f56b8_.py b/migrations/versions/398d449f56b8_.py new file mode 100644 index 0000000..3bdf934 100644 --- /dev/null +++ a/migrations/versions/398d449f56b8_.py @@ -1,0 +1,35 @@ +"""empty message + +Revision ID: 398d449f56b8 +Revises: 0689a529bce0 +Create Date: 2017-07-29 12:20:50.380698 + +""" + +# revision identifiers, used by Alembic. +revision = '398d449f56b8' +down_revision = '0689a529bce0' +branch_labels = None +depends_on = None + +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('quotes', + sa.Column('id', postgresql.UUID(), nullable=False), + sa.Column('quote', sa.String(), nullable=True), + sa.Column('attributed', sa.String(), nullable=True), + sa.Column('added_by', sa.String(), nullable=True), + sa.Column('created', sa.DateTime(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('quotes') + # ### end Alembic commands ### -- rgit 0.1.3