Add quotes module
Diff
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(-)
@@ -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())
@@ -1,7 +1,8 @@
"""Provide various decorators for dave modules."""
import re
from enum import Enum
import dave.config as config
def match(value):
@@ -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.")
@@ -1,0 +1,35 @@
"""empty message
Revision ID: 398d449f56b8
Revises: 0689a529bce0
Create Date: 2017-07-29 12:20:50.380698
"""
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():
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')
)
def downgrade():
op.drop_table('quotes')