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
This commit is contained in:
Brian S. Stephan 2015-05-21 22:32:57 -05:00
parent 7d13119176
commit eb006db04d
8 changed files with 165 additions and 0 deletions

View File

@ -36,6 +36,7 @@ INSTALLED_APPS = (
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'facts',
'ircbot',
'karma',
'markov',

View File

7
dr_botzo/facts/admin.py Normal file
View File

@ -0,0 +1,7 @@
from django.contrib import admin
from facts.models import Fact, FactCategory
admin.site.register(Fact)
admin.site.register(FactCategory)

View File

@ -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

View File

@ -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'),
),
]

View File

@ -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'},
),
]

View File

63
dr_botzo/facts/models.py Normal file
View File

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