changeset 159:416353def4ca

Added a new posts template tag for the home page.
author Brian Neal <bgneal@gmail.com>
date Sun, 20 Dec 2009 20:42:19 +0000
parents ca7d619ee27b
children 2eb3984ccb15
files gpp/forums/templatetags/forum_tags.py gpp/templates/forums/new_posts_tag.html gpp/templates/home.html
diffstat 3 files changed, 49 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/forums/templatetags/forum_tags.py	Sun Dec 20 05:51:22 2009 +0000
+++ b/gpp/forums/templatetags/forum_tags.py	Sun Dec 20 20:42:19 2009 +0000
@@ -7,15 +7,25 @@
 from django import template
 from django.conf import settings
 
+from forums.models import Topic
+
+
 register = template.Library()
 
-
 TIME_FMT_24 = "%H:%M"
 TIME_FMT_12 = "%I:%M %p"
 
 DATE_FMT = "%b %d %Y"
-DATE_FMT_24 = "%s %s" % (DATE_FMT, TIME_FMT_24)
-DATE_FMT_12 = "%s %s" % (DATE_FMT, TIME_FMT_12)
+SHORT_DATE_FMT = "%b %d"
+
+DATE_FMT_24 = (
+    "%s %s" % (DATE_FMT, TIME_FMT_24),          # long format
+    "%s %s" % (TIME_FMT_24, SHORT_DATE_FMT),    # short format
+)
+DATE_FMT_12 = (
+    "%s %s" % (DATE_FMT, TIME_FMT_12),          # long format
+    "%s %s" % (TIME_FMT_12, SHORT_DATE_FMT),    # short format
+)
 
 SERVER_TZ = timezone(settings.TIME_ZONE)
 
@@ -65,11 +75,13 @@
 
 
 @register.simple_tag
-def forum_date(date, user):
+def forum_date(date, user, long_format=True):
     """
     This tag displays an arbitrary datetime, adjusted by the user's
     time zone preferences.
     """
+    fmt_index = 0 if long_format else 1
+
     date = SERVER_TZ.localize(date)
     if user.is_authenticated():
         profile = user.get_profile()
@@ -79,7 +91,7 @@
     else:
         fmt = DATE_FMT_12
 
-    return date.strftime(fmt)
+    return date.strftime(fmt[fmt_index])
 
 
 @register.inclusion_tag('forums/show_form.html')
@@ -94,3 +106,19 @@
         'is_ajax': is_ajax,
         'MEDIA_URL': media_url,
     }
+
+
+@register.inclusion_tag('forums/new_posts_tag.html', takes_context=True)
+def new_posts(context):
+    """
+    This tag displays the topics that have the newest posts.
+    Only the "public" forums are displayed.
+    """
+    topics = Topic.objects.filter(
+            forum__category__groups__isnull=True).select_related(
+                    'user', 'last_post').order_by('-update_date')[:10]
+
+    return {
+        'topics': topics,
+        'user': context['user'],
+    }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/templates/forums/new_posts_tag.html	Sun Dec 20 20:42:19 2009 +0000
@@ -0,0 +1,14 @@
+{% load forum_tags %}
+<div>
+<h2>Latest Forum Posts</h2>
+<p>Join us in our <a href="{% url forums-index %}">forums</a> for some lively discussions. Here are the list of forum topics with new posts.</p>
+{% if topics %}
+   <ul>
+   {% for topic in topics %}
+   <li><a href="{{ topic.last_post.get_absolute_url }}">{{ topic.name }}</a> by {{ topic.user.username }} on {% forum_date topic.update_date user 0 %}</li>
+   {% endfor %}
+   </ul>
+{% else %}
+   <p>No forum topics at this time.</p>
+{% endif %}
+</div>
--- a/gpp/templates/home.html	Sun Dec 20 05:51:22 2009 +0000
+++ b/gpp/templates/home.html	Sun Dec 20 20:42:19 2009 +0000
@@ -3,6 +3,7 @@
 {% load news_tags %}
 {% load weblinks_tags %}
 {% load downloads_tags %}
+{% load forum_tags %}
 {% load cache %}
 {% block title %}Home{% endblock %}
 {% block custom_css %}
@@ -39,6 +40,7 @@
 {% cache 3600 home_bulletins %}
    {% current_bulletins %}
 {% endcache %}
+{% new_posts %}
 {% cache 3600 home_news %}
    {% current_news %}
 {% endcache %}