From eb006db04d70427a74d455cb03a9f57dd4f713ae Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Thu, 21 May 2015 22:32:57 -0500 Subject: [PATCH] facts: store facts in the database for retrieval should be able to accomplish a variety of things, from actual helpful facts to quotes to fortune commands --- dr_botzo/dr_botzo/settings.py | 1 + dr_botzo/facts/__init__.py | 0 dr_botzo/facts/admin.py | 7 +++ dr_botzo/facts/ircplugin.py | 44 +++++++++++++ dr_botzo/facts/migrations/0001_initial.py | 32 ++++++++++ .../migrations/0002_auto_20150521_2224.py | 18 ++++++ dr_botzo/facts/migrations/__init__.py | 0 dr_botzo/facts/models.py | 63 +++++++++++++++++++ 8 files changed, 165 insertions(+) create mode 100644 dr_botzo/facts/__init__.py create mode 100644 dr_botzo/facts/admin.py create mode 100644 dr_botzo/facts/ircplugin.py create mode 100644 dr_botzo/facts/migrations/0001_initial.py create mode 100644 dr_botzo/facts/migrations/0002_auto_20150521_2224.py create mode 100644 dr_botzo/facts/migrations/__init__.py create mode 100644 dr_botzo/facts/models.py diff --git a/dr_botzo/dr_botzo/settings.py b/dr_botzo/dr_botzo/settings.py index 933dd99..830c822 100644 --- a/dr_botzo/dr_botzo/settings.py +++ b/dr_botzo/dr_botzo/settings.py @@ -36,6 +36,7 @@ INSTALLED_APPS = ( 'django.contrib.messages', 'django.contrib.staticfiles', 'django_extensions', + 'facts', 'ircbot', 'karma', 'markov', diff --git a/dr_botzo/facts/__init__.py b/dr_botzo/facts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dr_botzo/facts/admin.py b/dr_botzo/facts/admin.py new file mode 100644 index 0000000..7764b7f --- /dev/null +++ b/dr_botzo/facts/admin.py @@ -0,0 +1,7 @@ +from django.contrib import admin + +from facts.models import Fact, FactCategory + + +admin.site.register(Fact) +admin.site.register(FactCategory) diff --git a/dr_botzo/facts/ircplugin.py b/dr_botzo/facts/ircplugin.py new file mode 100644 index 0000000..649bb3f --- /dev/null +++ b/dr_botzo/facts/ircplugin.py @@ -0,0 +1,44 @@ +from __future__ import unicode_literals + +import logging + +from ircbot.lib import Plugin +from facts.models import Fact + + +log = logging.getLogger('facts.ircplugin') + + +class Facts(Plugin): + + """Present facts to IRC.""" + + def start(self): + """Set up the handlers.""" + + self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!facts\s+(\S+)(\s+(.*)$|$)', + self.handle_facts, -20) + + super(Facts, self).start() + + def stop(self): + """Tear down handlers.""" + + self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_facts) + + super(Facts, self).stop() + + def handle_facts(self, connection, event, match): + """Respond to the facts command with desired fact.""" + + category = match.group(1) + regex = None + if match.group(2) != '': + regex = match.group(3) + + fact = Fact.objects.random_fact(category, regex) + if fact: + return self.bot.reply(event, fact.fact) + + +plugin = Facts diff --git a/dr_botzo/facts/migrations/0001_initial.py b/dr_botzo/facts/migrations/0001_initial.py new file mode 100644 index 0000000..043a067 --- /dev/null +++ b/dr_botzo/facts/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Fact', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('fact', models.TextField()), + ], + ), + migrations.CreateModel( + name='FactCategory', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('name', models.CharField(unique=True, max_length=200)), + ], + ), + migrations.AddField( + model_name='fact', + name='category', + field=models.ForeignKey(to='facts.FactCategory'), + ), + ] diff --git a/dr_botzo/facts/migrations/0002_auto_20150521_2224.py b/dr_botzo/facts/migrations/0002_auto_20150521_2224.py new file mode 100644 index 0000000..e91277a --- /dev/null +++ b/dr_botzo/facts/migrations/0002_auto_20150521_2224.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('facts', '0001_initial'), + ] + + operations = [ + migrations.AlterModelOptions( + name='factcategory', + options={'verbose_name_plural': 'fact categories'}, + ), + ] diff --git a/dr_botzo/facts/migrations/__init__.py b/dr_botzo/facts/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dr_botzo/facts/models.py b/dr_botzo/facts/models.py new file mode 100644 index 0000000..b16e171 --- /dev/null +++ b/dr_botzo/facts/models.py @@ -0,0 +1,63 @@ +"""Store "facts".""" + +from __future__ import unicode_literals + +import logging +import random + +from django.db import models + + +log = logging.getLogger('facts.models') + + +class FactCategory(models.Model): + + """Define categories for facts.""" + + name = models.CharField(max_length=200, unique=True) + + class Meta: + verbose_name_plural = 'fact categories' + + def __unicode__(self): + """String representation.""" + + return "{0:s}".format(self.name) + + +class FactManager(models.Manager): + + """Queries against Fact.""" + + def random_fact(self, category, regex=None): + """Get a random fact from the database.""" + + try: + fact_category = FactCategory.objects.get(name=category) + except FactCategory.DoesNotExist: + return None + + facts = Fact.objects.filter(category=fact_category) + if regex: + facts = facts.filter(fact__iregex=regex) + + if len(facts) > 0: + return random.choice(facts) + else: + return None + + +class Fact(models.Model): + + """Define facts.""" + + fact = models.TextField() + category = models.ForeignKey(FactCategory) + + objects = FactManager() + + def __unicode__(self): + """String representation.""" + + return "{0:s} - {1:s}".format(self.category.name, self.fact)