collapsing all of dr_botzo one directory
This commit is contained in:
0
facts/__init__.py
Normal file
0
facts/__init__.py
Normal file
7
facts/admin.py
Normal file
7
facts/admin.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from facts.models import Fact, FactCategory
|
||||
|
||||
|
||||
admin.site.register(Fact)
|
||||
admin.site.register(FactCategory)
|
||||
68
facts/ircplugin.py
Normal file
68
facts/ircplugin.py
Normal file
@@ -0,0 +1,68 @@
|
||||
"""IRC plugin for retrieval of facts."""
|
||||
|
||||
import logging
|
||||
|
||||
from irc.client import NickMask
|
||||
|
||||
from ircbot.lib import Plugin, has_permission
|
||||
from facts.models import Fact, FactCategory
|
||||
|
||||
|
||||
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+add\s+(\S+)\s+(.*)$',
|
||||
self.handle_add_fact, -20)
|
||||
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_add_fact)
|
||||
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:
|
||||
if fact.category.show_source:
|
||||
nick = NickMask(fact.nickmask).nick
|
||||
msg = "{0:s} ({1:s})".format(fact.fact, nick)
|
||||
else:
|
||||
msg = fact.fact
|
||||
|
||||
return self.bot.reply(event, msg)
|
||||
|
||||
def handle_add_fact(self, connection, event, match):
|
||||
"""Add a new fact to the database."""
|
||||
|
||||
category_name = match.group(1)
|
||||
fact_text = match.group(2)
|
||||
|
||||
if has_permission(event.source, 'facts.add_fact'):
|
||||
# create the category
|
||||
category, created = FactCategory.objects.get_or_create(name=category_name)
|
||||
fact = Fact.objects.create(fact=fact_text, category=category, nickmask=event.source)
|
||||
if fact:
|
||||
return self.bot.reply(event, "fact added to {0:s}".format(category.name))
|
||||
|
||||
|
||||
plugin = Facts
|
||||
31
facts/migrations/0001_initial.py
Normal file
31
facts/migrations/0001_initial.py
Normal file
@@ -0,0 +1,31 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
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'),
|
||||
),
|
||||
]
|
||||
17
facts/migrations/0002_auto_20150521_2224.py
Normal file
17
facts/migrations/0002_auto_20150521_2224.py
Normal file
@@ -0,0 +1,17 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
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'},
|
||||
),
|
||||
]
|
||||
23
facts/migrations/0003_auto_20150620_1018.py
Normal file
23
facts/migrations/0003_auto_20150620_1018.py
Normal file
@@ -0,0 +1,23 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('facts', '0002_auto_20150521_2224'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='fact',
|
||||
name='nickmask',
|
||||
field=models.CharField(default='', max_length=200),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='factcategory',
|
||||
name='show_source',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
]
|
||||
21
facts/migrations/0004_fact_time.py
Normal file
21
facts/migrations/0004_fact_time.py
Normal file
@@ -0,0 +1,21 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from django.db import models, migrations
|
||||
import datetime
|
||||
from django.utils.timezone import utc
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('facts', '0003_auto_20150620_1018'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='fact',
|
||||
name='time',
|
||||
field=models.DateTimeField(default=datetime.datetime(2015, 6, 20, 15, 22, 20, 481856, tzinfo=utc), auto_now_add=True),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
||||
18
facts/migrations/0005_auto_20150814_1653.py
Normal file
18
facts/migrations/0005_auto_20150814_1653.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('facts', '0004_fact_time'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='fact',
|
||||
name='nickmask',
|
||||
field=models.CharField(default='', max_length=200, blank=True),
|
||||
),
|
||||
]
|
||||
0
facts/migrations/__init__.py
Normal file
0
facts/migrations/__init__.py
Normal file
64
facts/models.py
Normal file
64
facts/models.py
Normal file
@@ -0,0 +1,64 @@
|
||||
"""Store "facts"."""
|
||||
|
||||
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)
|
||||
show_source = models.BooleanField(default=False)
|
||||
|
||||
class Meta:
|
||||
verbose_name_plural = 'fact categories'
|
||||
|
||||
def __str__(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)
|
||||
nickmask = models.CharField(max_length=200, default='', blank=True)
|
||||
time = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
objects = FactManager()
|
||||
|
||||
def __str__(self):
|
||||
"""String representation."""
|
||||
|
||||
return "{0:s} - {1:s}".format(self.category.name, self.fact)
|
||||
Reference in New Issue
Block a user