Show recent forum threads on the front page. Sucks, but. master veekun-promotions/2010121201 veekun-promotions/2010122201 veekun-promotions/2010122701 veekun-promotions/2011011701 veekun-promotions/2011021501 veekun-promotions/2011021502 veekun-promotions/2011022101 veekun-promotions/2011030301 veekun-promotions/2011030302 veekun-promotions/2011041101 veekun-promotions/2011041102 veekun-promotions/2011041201 veekun-promotions/2011041501 veekun-promotions/2011041601
authorEevee <git@veekun.com>
Sat, 11 Dec 2010 08:25:10 +0000 (00:25 -0800)
committerEevee <git@veekun.com>
Sat, 11 Dec 2010 08:25:10 +0000 (00:25 -0800)
splinext/forum/__init__.py
splinext/forum/frontpage_sources.py
splinext/forum/templates/forum/front_page_activity.mako [new file with mode: 0644]

index 8d2115a..5794a4c 100644 (file)
@@ -41,9 +41,11 @@ class ForumPlugin(PluginBase):
 
         # frontpage plugin may or may not be installed
         try:
-            from splinext.forum.frontpage_sources import ForumSource
+            from splinext.forum.frontpage_sources import ForumSource, forum_activity
             hooks.append(
                 ('frontpage_updates_forum', Priority.NORMAL, ForumSource))
+            hooks.append(
+                ('frontpage_extras', Priority.NORMAL, forum_activity))
         except ImportError:
             pass
 
index 69d0f3b..61982c5 100644 (file)
@@ -1,16 +1,15 @@
 from collections import namedtuple
+import datetime
 
 from sqlalchemy.orm import contains_eager, joinedload
-from pylons import url
+from sqlalchemy.sql import func
+from pylons import tmpl_context as c, url
 
 from spline.model import meta
 
 from splinext.forum import model as forum_model
 from splinext.frontpage.sources import Source
 
-def frontpage_hook(limit, max_age, forum_id):
-    """Hook to return recent news for the front page."""
-
 FrontPageThread = namedtuple('FrontPageThread', ['source', 'time', 'post'])
 class ForumSource(Source):
     """Represents a forum whose threads are put on the front page.
@@ -70,3 +69,33 @@ class ForumSource(Source):
             updates.append(update)
 
         return updates
+
+FrontPageActivity = namedtuple('FrontPageActivity', ['template', 'threads'])
+def forum_activity(*args, **kwargs):
+    """Show recently-active threads on the front page.
+
+    Note that this isn't the most recent X threads; it's threads that are more
+    recent than X, sorted by their activity since X.
+    """
+    # XXX this should be configurable probably
+    cutoff = datetime.datetime.now() - datetime.timedelta(days=7)
+
+    # TODO some sort of dropoff here idk
+    active_threads_subq = meta.Session.query(
+        forum_model.Post.thread_id.label('thread_id'),
+        func.count('*').label('ranking'),
+    ) \
+        .filter(forum_model.Post.posted_time >= cutoff) \
+        .group_by(forum_model.Post.thread_id) \
+        .subquery()
+
+    threads_q = meta.Session.query(forum_model.Thread) \
+        .join((active_threads_subq,
+            active_threads_subq.c.thread_id == forum_model.Thread.id)) \
+        .order_by(active_threads_subq.c.ranking.desc()) \
+        .limit(10)
+
+    return FrontPageActivity(
+        template='/forum/front_page_activity.mako',
+        threads=threads_q.all(),
+    )
diff --git a/splinext/forum/templates/forum/front_page_activity.mako b/splinext/forum/templates/forum/front_page_activity.mako
new file mode 100644 (file)
index 0000000..6dcded5
--- /dev/null
@@ -0,0 +1,19 @@
+<%page args="obj" />
+<%namespace name="userlib" file="/users/lib.mako" />
+
+<h1>Forum activity</h1>
+
+% if obj.threads:
+<table class="striped-rows" style="width: 100%;">
+<tbody>
+    % for thread in obj.threads:
+    <tr>
+        <td class="name"><a href="${url(controller='forum', action='posts', forum_id=thread.forum.id, thread_id=thread.id)}">${thread.subject}</a></td>
+    </tr>
+    % endfor
+</tbody>
+</table>
+% else:
+<p>The forums are dead quiet.  No one is posting.  A lone tumbleweed rolls by.</p>
+<p>Maybe you should <a href="${url(controller='forum', action='forums')}">do something about this</a>.</p>
+% endif