parent
4740cd7808
commit
cd31bbb489
|
@ -0,0 +1,7 @@
|
||||||
|
"""Facts allows storage of arbitrarily defined sets of strings.
|
||||||
|
|
||||||
|
The "fact" part is tongue-in-cheek, you can put random options for a decision,
|
||||||
|
roulette style game modes, 8-ball type functionality, and total falsehoods
|
||||||
|
in here too. When fact categories are queried, a random one is returned if
|
||||||
|
you don't know what you're looking for.
|
||||||
|
"""
|
|
@ -1,3 +1,4 @@
|
||||||
|
"""Admin interface for the facts app."""
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
|
||||||
from facts.models import Fact, FactCategory
|
from facts.models import Fact, FactCategory
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
"""IRC plugin for retrieval of facts."""
|
"""IRC plugin for retrieval of facts."""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from irc.client import NickMask
|
from irc.client import NickMask
|
||||||
|
@ -7,17 +6,14 @@ from irc.client import NickMask
|
||||||
from ircbot.lib import Plugin, has_permission
|
from ircbot.lib import Plugin, has_permission
|
||||||
from facts.models import Fact, FactCategory
|
from facts.models import Fact, FactCategory
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger('facts.ircplugin')
|
log = logging.getLogger('facts.ircplugin')
|
||||||
|
|
||||||
|
|
||||||
class Facts(Plugin):
|
class Facts(Plugin):
|
||||||
|
|
||||||
"""Present facts to IRC."""
|
"""Present facts to IRC."""
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
"""Set up the handlers."""
|
"""Set up the handlers."""
|
||||||
|
|
||||||
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!facts\s+add\s+(\S+)\s+(.*)$',
|
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!facts\s+add\s+(\S+)\s+(.*)$',
|
||||||
self.handle_add_fact, -20)
|
self.handle_add_fact, -20)
|
||||||
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!facts\s+(\S+)(\s+(.*)$|$)',
|
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!facts\s+(\S+)(\s+(.*)$|$)',
|
||||||
|
@ -27,7 +23,6 @@ class Facts(Plugin):
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""Tear down handlers."""
|
"""Tear down handlers."""
|
||||||
|
|
||||||
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_add_fact)
|
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_add_fact)
|
||||||
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_facts)
|
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_facts)
|
||||||
|
|
||||||
|
@ -35,7 +30,6 @@ class Facts(Plugin):
|
||||||
|
|
||||||
def handle_facts(self, connection, event, match):
|
def handle_facts(self, connection, event, match):
|
||||||
"""Respond to the facts command with desired fact."""
|
"""Respond to the facts command with desired fact."""
|
||||||
|
|
||||||
category = match.group(1)
|
category = match.group(1)
|
||||||
regex = None
|
regex = None
|
||||||
if match.group(2) != '':
|
if match.group(2) != '':
|
||||||
|
@ -53,7 +47,6 @@ class Facts(Plugin):
|
||||||
|
|
||||||
def handle_add_fact(self, connection, event, match):
|
def handle_add_fact(self, connection, event, match):
|
||||||
"""Add a new fact to the database."""
|
"""Add a new fact to the database."""
|
||||||
|
|
||||||
category_name = match.group(1)
|
category_name = match.group(1)
|
||||||
fact_text = match.group(2)
|
fact_text = match.group(2)
|
||||||
|
|
||||||
|
|
|
@ -1,37 +1,33 @@
|
||||||
"""Store "facts"."""
|
"""Store "facts"."""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import random
|
import random
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger('facts.models')
|
log = logging.getLogger('facts.models')
|
||||||
|
|
||||||
|
|
||||||
class FactCategory(models.Model):
|
class FactCategory(models.Model):
|
||||||
|
|
||||||
"""Define categories for facts."""
|
"""Define categories for facts."""
|
||||||
|
|
||||||
name = models.CharField(max_length=200, unique=True)
|
name = models.CharField(max_length=200, unique=True)
|
||||||
show_source = models.BooleanField(default=False)
|
show_source = models.BooleanField(default=False)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
"""Meta options."""
|
||||||
|
|
||||||
verbose_name_plural = 'fact categories'
|
verbose_name_plural = 'fact categories'
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""String representation."""
|
"""String representation."""
|
||||||
|
|
||||||
return "{0:s}".format(self.name)
|
return "{0:s}".format(self.name)
|
||||||
|
|
||||||
|
|
||||||
class FactManager(models.Manager):
|
class FactManager(models.Manager):
|
||||||
|
|
||||||
"""Queries against Fact."""
|
"""Queries against Fact."""
|
||||||
|
|
||||||
def random_fact(self, category, regex=None):
|
def random_fact(self, category, regex=None):
|
||||||
"""Get a random fact from the database."""
|
"""Get a random fact from the database."""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
fact_category = FactCategory.objects.get(name=category)
|
fact_category = FactCategory.objects.get(name=category)
|
||||||
except FactCategory.DoesNotExist:
|
except FactCategory.DoesNotExist:
|
||||||
|
@ -48,7 +44,6 @@ class FactManager(models.Manager):
|
||||||
|
|
||||||
|
|
||||||
class Fact(models.Model):
|
class Fact(models.Model):
|
||||||
|
|
||||||
"""Define facts."""
|
"""Define facts."""
|
||||||
|
|
||||||
fact = models.TextField()
|
fact = models.TextField()
|
||||||
|
@ -60,5 +55,4 @@ class Fact(models.Model):
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""String representation."""
|
"""String representation."""
|
||||||
|
|
||||||
return "{0:s} - {1:s}".format(self.category.name, self.fact)
|
return "{0:s} - {1:s}".format(self.category.name, self.fact)
|
||||||
|
|
Loading…
Reference in New Issue