changeset 166:8acf5be27f18

Implement #50, add a template tag to display the latest poll and add to the home page.
author Brian Neal <bgneal@gmail.com>
date Sun, 17 Jan 2010 20:24:01 +0000
parents 952e05cb3d80
children cf9f9d4c4d54
files gpp/polls/templatetags/__init__.py gpp/polls/templatetags/poll_tags.py gpp/polls/urls.py gpp/templates/home.html gpp/templates/polls/latest_poll_tag.html
diffstat 4 files changed, 37 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/polls/templatetags/poll_tags.py	Sun Jan 17 20:24:01 2010 +0000
@@ -0,0 +1,18 @@
+"""
+Template tags for the Polls application. 
+"""
+from django import template
+
+from polls.models import Poll
+
+register = template.Library()
+
+
+@register.inclusion_tag("polls/latest_poll_tag.html")
+def latest_poll():
+    try:
+        poll = Poll.objects.get_current_polls()[0]
+    except IndexError:
+        poll = None
+
+    return {'poll': poll }
--- a/gpp/polls/urls.py	Sun Jan 03 04:15:14 2010 +0000
+++ b/gpp/polls/urls.py	Sun Jan 17 20:24:01 2010 +0000
@@ -2,8 +2,8 @@
 from django.conf.urls.defaults import *
 
 urlpatterns = patterns('polls.views',
-   url(r'^$', 'poll_index', name='polls-main'),
-   (r'^(?P<poll_id>\d+)/$', 'poll_detail'),
-   (r'^(?P<poll_id>\d+)/results/$', 'poll_results'),
-   (r'^(?P<poll_id>\d+)/vote/$', 'poll_vote'),
+    url(r'^$', 'poll_index', name='polls-main'),
+    url(r'^(?P<poll_id>\d+)/$', 'poll_detail', name='polls-detail'),
+    url(r'^(?P<poll_id>\d+)/results/$', 'poll_results', name='polls-results'),
+    url(r'^(?P<poll_id>\d+)/vote/$', 'poll_vote', name='polls-vote'),
 )
--- a/gpp/templates/home.html	Sun Jan 03 04:15:14 2010 +0000
+++ b/gpp/templates/home.html	Sun Jan 17 20:24:01 2010 +0000
@@ -3,6 +3,7 @@
 {% load news_tags %}
 {% load weblinks_tags %}
 {% load downloads_tags %}
+{% load poll_tags %}
 {% load forum_tags %}
 {% load cache %}
 {% block title %}Home{% endblock %}
@@ -45,6 +46,7 @@
    {% current_news %}
 {% endcache %}
 {% cache 3600 home_new_stuff %}
+{% latest_poll %}
 <div class="span-9 append-1">
    {% latest_weblinks %} 
 </div>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/templates/polls/latest_poll_tag.html	Sun Jan 17 20:24:01 2010 +0000
@@ -0,0 +1,13 @@
+{% if poll %}
+<div>
+   <h2>Latest Poll</h2>
+   <h3>{{ poll.question }}</h3>
+   <ul>
+      {% for choice in poll.choice_set.all %}
+         <li>{{ choice.choice }}</li>
+      {% endfor %}
+   </ul>
+   <p>Go <a href="{% url polls-detail poll_id=poll.id %}">see the results and vote</a>
+   or check out <a href="{% url polls-main %}">more polls</a>.</p>
+</div>
+{% endif %}