Merge branch 'facts-choices-refactoring' into 'master'
Refactoring facts app to wrap in choices, improve admin Closes #15 and #14 See merge request !13
This commit is contained in:
commit
0b4ffd271d
@ -1,5 +1,5 @@
|
||||
doc-warnings: true
|
||||
strictness: medium
|
||||
strictness: high
|
||||
ignore-paths:
|
||||
- migrations
|
||||
ignore-patterns:
|
||||
@ -15,3 +15,6 @@ pylint:
|
||||
pep8:
|
||||
options:
|
||||
max-line-length: 120
|
||||
pep257:
|
||||
disable:
|
||||
- D203
|
||||
|
@ -1,7 +0,0 @@
|
||||
"""Manage choices models."""
|
||||
|
||||
from django.contrib import admin
|
||||
|
||||
from choices.models import ChoiceSet
|
||||
|
||||
admin.site.register(ChoiceSet)
|
@ -1,21 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='ChoiceSet',
|
||||
fields=[
|
||||
('id', models.AutoField(serialize=False, primary_key=True, verbose_name='ID', auto_created=True)),
|
||||
('name', models.CharField(max_length=20)),
|
||||
('choices', models.TextField()),
|
||||
],
|
||||
),
|
||||
]
|
@ -1,20 +0,0 @@
|
||||
"""Define choice set models"""
|
||||
|
||||
from django.db import models
|
||||
|
||||
|
||||
class ChoiceSet(models.Model):
|
||||
|
||||
"""Define collections of possible choices."""
|
||||
|
||||
name = models.CharField(max_length=20)
|
||||
choices = models.TextField()
|
||||
|
||||
def __str__(self):
|
||||
"""String representation."""
|
||||
|
||||
return "{0:s} - {1:s}".format(self.name, self.choices)
|
||||
|
||||
def choices_list(self):
|
||||
"""Return choices as a list."""
|
||||
return self.choices.split(',')
|
@ -1,9 +0,0 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block title %}choice set: {{ choiceset.name }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h3>{{ choiceset.name }}</h3>
|
||||
<p><strong>Choices:</strong> {{ choiceset.choices_list|join:", " }}</p>
|
||||
<p><strong>Random Choice:</strong> {{ choiceset.choices_list|random }}</p>
|
||||
{% endblock %}
|
@ -1,11 +0,0 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block title %}choices{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<ul>
|
||||
{% for choiceset in choicesets %}
|
||||
<li><a href="{% url 'choices_choiceset_detail' choiceset.name %}">{{ choiceset.name }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endblock %}
|
@ -1,10 +0,0 @@
|
||||
"""URL patterns for choices."""
|
||||
|
||||
from django.conf.urls import url
|
||||
|
||||
from choices.views import index, choiceset_detail
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^$', index, name='choices_index'),
|
||||
url(r'^(?P<set_name>.+)/$', choiceset_detail, name='choices_choiceset_detail'),
|
||||
]
|
@ -1,25 +0,0 @@
|
||||
"""Display choice sets."""
|
||||
|
||||
import logging
|
||||
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
|
||||
from choices.models import ChoiceSet
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def index(request):
|
||||
"""Display nothing, for the moment."""
|
||||
|
||||
choicesets = ChoiceSet.objects.all()
|
||||
|
||||
return render(request, 'choices/index.html', {'choicesets': choicesets})
|
||||
|
||||
|
||||
def choiceset_detail(request, set_name):
|
||||
"""Display info, and a random choice, for the requested choice set."""
|
||||
|
||||
choiceset = get_object_or_404(ChoiceSet, name=set_name)
|
||||
|
||||
return render(request, 'choices/choiceset_detail.html', {'choiceset': choiceset})
|
@ -43,7 +43,6 @@ INSTALLED_APPS = (
|
||||
'bootstrap3',
|
||||
'registration',
|
||||
'rest_framework',
|
||||
'choices',
|
||||
'countdown',
|
||||
'dispatch',
|
||||
'facts',
|
||||
|
@ -12,8 +12,8 @@ admin.autodiscover()
|
||||
urlpatterns = [
|
||||
url(r'^$', TemplateView.as_view(template_name='index.html'), name='home'),
|
||||
|
||||
url(r'^choices/', include('choices.urls')),
|
||||
url(r'^dispatch/', include('dispatch.urls')),
|
||||
url(r'^itemsets/', include('facts.urls')),
|
||||
url(r'^karma/', include('karma.urls')),
|
||||
url(r'^markov/', include('markov.urls')),
|
||||
url(r'^races/', include('races.urls')),
|
||||
|
@ -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,7 +1,20 @@
|
||||
"""Admin interface for the facts app."""
|
||||
from django.contrib import admin
|
||||
|
||||
from facts.models import Fact, FactCategory
|
||||
|
||||
|
||||
class FactInline(admin.TabularInline):
|
||||
"""Inline admin form for facts, for inclusion in fact categories."""
|
||||
|
||||
model = Fact
|
||||
|
||||
|
||||
class FactCategoryAdmin(admin.ModelAdmin):
|
||||
"""Admin pages for the FactCategory model."""
|
||||
|
||||
inlines = [FactInline]
|
||||
|
||||
|
||||
admin.site.register(Fact)
|
||||
admin.site.register(FactCategory)
|
||||
admin.site.register(FactCategory, FactCategoryAdmin)
|
||||
|
@ -1,5 +1,4 @@
|
||||
"""IRC plugin for retrieval of facts."""
|
||||
|
||||
import logging
|
||||
|
||||
from irc.client import NickMask
|
||||
@ -7,17 +6,14 @@ 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+(.*)$|$)',
|
||||
@ -27,7 +23,6 @@ class Facts(Plugin):
|
||||
|
||||
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)
|
||||
|
||||
@ -35,7 +30,6 @@ class Facts(Plugin):
|
||||
|
||||
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) != '':
|
||||
@ -53,7 +47,6 @@ class Facts(Plugin):
|
||||
|
||||
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)
|
||||
|
||||
|
20
facts/migrations/0006_factcategory_show_all_entries.py
Normal file
20
facts/migrations/0006_factcategory_show_all_entries.py
Normal file
@ -0,0 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2017-02-11 15:00
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('facts', '0005_auto_20150814_1653'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='factcategory',
|
||||
name='show_all_entries',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
]
|
@ -1,43 +1,31 @@
|
||||
"""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_all_entries = models.BooleanField(default=False)
|
||||
show_source = models.BooleanField(default=False)
|
||||
|
||||
class Meta:
|
||||
"""Meta options."""
|
||||
|
||||
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)
|
||||
def random_fact(self, regex=None):
|
||||
"""Get a random fact in this category."""
|
||||
facts = self.fact_set.all()
|
||||
if regex:
|
||||
facts = facts.filter(fact__iregex=regex)
|
||||
|
||||
@ -47,8 +35,20 @@ class FactManager(models.Manager):
|
||||
return None
|
||||
|
||||
|
||||
class Fact(models.Model):
|
||||
class FactManager(models.Manager):
|
||||
"""Queries against Fact."""
|
||||
|
||||
def random_fact(self, category, regex=None):
|
||||
"""Get a random fact from the database."""
|
||||
try:
|
||||
factcategory = FactCategory.objects.get(name=category)
|
||||
except FactCategory.DoesNotExist:
|
||||
return None
|
||||
|
||||
return factcategory.random_fact(regex)
|
||||
|
||||
|
||||
class Fact(models.Model):
|
||||
"""Define facts."""
|
||||
|
||||
fact = models.TextField()
|
||||
@ -60,5 +60,4 @@ class Fact(models.Model):
|
||||
|
||||
def __str__(self):
|
||||
"""String representation."""
|
||||
|
||||
return "{0:s} - {1:s}".format(self.category.name, self.fact)
|
||||
|
4
facts/templates/facts/base.html
Normal file
4
facts/templates/facts/base.html
Normal file
@ -0,0 +1,4 @@
|
||||
{% extends 'base.html' %}
|
||||
{% block extra_media %}{% load static %}
|
||||
<link href="{% get_static_prefix %}css/facts.css" rel="stylesheet" type='text/css' />
|
||||
{% endblock %}
|
11
facts/templates/facts/factcategory_detail.html
Normal file
11
facts/templates/facts/factcategory_detail.html
Normal file
@ -0,0 +1,11 @@
|
||||
{% extends 'facts/base.html' %}
|
||||
|
||||
{% block title %}item set: {{ factcategory.name }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h3>{{ factcategory.name }}</h3>
|
||||
<p>{{ factcategory.random_fact.fact|linebreaks }}</p>
|
||||
{% if factcategory.show_all_entries %}
|
||||
<div class="fact-category-items">Items: {{ facts|join:", " }}</div>
|
||||
{% endif %}
|
||||
{% endblock %}
|
11
facts/templates/facts/index.html
Normal file
11
facts/templates/facts/index.html
Normal file
@ -0,0 +1,11 @@
|
||||
{% extends 'facts/base.html' %}
|
||||
|
||||
{% block title %}item sets{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<ul>
|
||||
{% for factcategory in factcategories %}
|
||||
<li><a href="{% url 'facts_factcategory_detail' factcategory.name %}">{{ factcategory.name }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endblock %}
|
9
facts/urls.py
Normal file
9
facts/urls.py
Normal file
@ -0,0 +1,9 @@
|
||||
"""URL patterns for the facts web views."""
|
||||
from django.conf.urls import url
|
||||
|
||||
from facts.views import index, factcategory_detail
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^$', index, name='facts_index'),
|
||||
url(r'^(?P<factcategory_name>.+)/$', factcategory_detail, name='facts_factcategory_detail'),
|
||||
]
|
26
facts/views.py
Normal file
26
facts/views.py
Normal file
@ -0,0 +1,26 @@
|
||||
"""Display fact categories."""
|
||||
import logging
|
||||
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
|
||||
from facts.models import FactCategory
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def index(request):
|
||||
"""Display a simple list of the fact categories, for the moment."""
|
||||
factcategories = FactCategory.objects.all()
|
||||
|
||||
return render(request, 'facts/index.html', {'factcategories': factcategories})
|
||||
|
||||
|
||||
def factcategory_detail(request, factcategory_name):
|
||||
"""Display info, and a random choice, for the fact category."""
|
||||
factcategory = get_object_or_404(FactCategory, name=factcategory_name)
|
||||
|
||||
facts = []
|
||||
if factcategory.show_all_entries:
|
||||
facts = [x.fact for x in factcategory.fact_set.all()]
|
||||
|
||||
return render(request, 'facts/factcategory_detail.html', {'factcategory': factcategory, 'facts': facts})
|
4
static/css/facts.css
Normal file
4
static/css/facts.css
Normal file
@ -0,0 +1,4 @@
|
||||
.fact-category-items {
|
||||
margin-top: 20px;
|
||||
color: #999999;
|
||||
}
|
@ -72,7 +72,7 @@
|
||||
</div>
|
||||
{% block navbar_menu %}
|
||||
<ul class="nav navbar-nav">
|
||||
<li><a href="{% url 'choices_index' %}">Choices</a></li>
|
||||
<li><a href="{% url 'facts_index' %}">Item Sets</a></li>
|
||||
<li><a href="{% url 'karma_index' %}">Karma</a></li>
|
||||
<li><a href="{% url 'markov_index' %}">Markov</a></li>
|
||||
<li><a href="{% url 'races_index' %}">Races</a></li>
|
||||
|
Loading…
Reference in New Issue
Block a user