🏡 index : ~doyle/dave.git

author Jordan Doyle <jordan@doyle.wf> 2017-07-29 12:20:53.0 +00:00:00
committer Jordan Doyle <jordan@doyle.wf> 2017-07-29 12:20:53.0 +00:00:00
commit
4a736b1000e63a95a54faa42424e3bd83e6062c3 [patch]
tree
e1bb8048ea68304904a77649fb00abc4c9249403
parent
a88faff3b055abfe2d3bd375739f9417c51660b9
download
4a736b1000e63a95a54faa42424e3bd83e6062c3.tar.gz

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(-)

diff --git a/dave/models.py b/dave/models.py
index 205de4f..4116e14 100644
--- a/dave/models.py
+++ b/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 @@ class Location(Base):
    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())
\ No newline at end of file
diff --git a/dave/module.py b/dave/module.py
index 905e54c..52e8bc2 100644
--- a/dave/module.py
+++ b/dave/module.py
@@ -2,6 +2,7 @@
"""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
--- /dev/null
+++ b/dave/modules/quote.py
@@ -0,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.")
\ No newline at end of file
diff --git a/migrations/versions/398d449f56b8_.py b/migrations/versions/398d449f56b8_.py
new file mode 100644
index 0000000..3bdf934
--- /dev/null
+++ b/migrations/versions/398d449f56b8_.py
@@ -0,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 ###