Update to new frontpage API, and keep frontpage optional.
authorEevee <git@veekun.com>
Sun, 25 Jul 2010 00:55:43 +0000 (17:55 -0700)
committerEevee <git@veekun.com>
Sun, 25 Jul 2010 04:44:53 +0000 (21:44 -0700)
splinext/forum/__init__.py
splinext/forum/frontpage_sources.py [new file with mode: 0644]
splinext/forum/templates/forum/front_page.mako

index 0c8edd3..07db391 100644 (file)
@@ -4,10 +4,8 @@ from pylons import c, session
 
 from spline.lib.plugin import PluginBase
 from spline.lib.plugin import PluginBase, PluginLink, Priority
-from spline.model import meta
 
 import splinext.forum.controllers.forum
-from splinext.forum import model as forum_model
 
 def add_routes_hook(map, *args, **kwargs):
     """Hook to inject some of our behavior into the routes configuration."""
@@ -20,26 +18,6 @@ def add_routes_hook(map, *args, **kwargs):
     map.connect('/forums/{forum_id}/write', controller='forum', action='write_thread')
     map.connect('/forums/{forum_id}/threads/{thread_id}/write', controller='forum', action='write')
 
-class FrontPageNewsPost(object):
-    pass
-
-def frontpage_hook(limit):
-    """Hook to return recent news for the front page."""
-    threads = meta.Session.query(forum_model.Thread) \
-        .join(forum_model.Thread.first_post) \
-        .order_by(forum_model.Post.posted_time.desc()) \
-        [:limit]
-
-    updates = []
-    for thread in threads:
-        update = FrontPageNewsPost()
-        update.time = thread.first_post.posted_time
-        update.template = '/forum/front_page.mako'
-        update.post = thread.first_post
-        updates.append(update)
-
-    return updates
-
 
 class ForumPlugin(PluginBase):
     def controllers(self):
@@ -48,7 +26,16 @@ class ForumPlugin(PluginBase):
         )
 
     def hooks(self):
-        return [
+        hooks = [
             ('routes_mapping',    Priority.NORMAL,      add_routes_hook),
-            ('frontpage_updates', Priority.NORMAL,      frontpage_hook),
         ]
+
+        # frontpage plugin may or may not be installed
+        try:
+            from splinext.forum.frontpage_sources import ForumSource
+            hooks.append(
+                ('frontpage_updates_forum', Priority.NORMAL, ForumSource))
+        except ImportError:
+            pass
+
+        return hooks
diff --git a/splinext/forum/frontpage_sources.py b/splinext/forum/frontpage_sources.py
new file mode 100644 (file)
index 0000000..f101871
--- /dev/null
@@ -0,0 +1,66 @@
+from collections import namedtuple
+
+from pylons import 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.
+
+    ``link``, ``title``, and ``icon`` are all optional; the link and title
+    default to the forum's thread list and name, and the icon defaults to a
+    newspaper.
+
+    Extra properties:
+
+    ``forum_id``
+        id of the forum to check for new threads.
+    """
+
+    template = '/forum/front_page.mako'
+
+    def __init__(self, forum_id, **kwargs):
+        forum = meta.Session.query(forum_model.Forum).get(forum_id)
+
+        # Link is tricky.  Needs url(), which doesn't exist when this class is
+        # loaded.  Lazy-load it in poll() below, instead
+        kwargs.setdefault('link', None)
+        kwargs.setdefault('title', forum.name)
+        kwargs.setdefault('icon', 'newspapers')
+        super(ForumSource, self).__init__(**kwargs)
+
+        self.forum_id = forum_id
+
+    def _poll(self, limit, max_age):
+        if not self.link:
+            self.link = url(
+                controller='forum', action='threads', forum_id=self.forum_id)
+
+        thread_q = meta.Session.query(forum_model.Thread) \
+            .filter_by(forum_id=self.forum_id) \
+            .join(forum_model.Thread.first_post)
+
+        if max_age:
+            thread_q = thread_q.filter(forum_model.Post.posted_time >= max_age)
+
+        threads = thread_q \
+            .order_by(forum_model.Post.posted_time.desc()) \
+            [:limit]
+
+        updates = []
+        for thread in threads:
+            update = FrontPageThread(
+                source = self,
+                time = thread.first_post.posted_time,
+                post = thread.first_post,
+            )
+            updates.append(update)
+
+        return updates
index b967763..ba51503 100644 (file)
@@ -3,20 +3,22 @@
 
 <div class="frontpage-update">
     <div class="header">
-        <div class="category"><img src="${h.static_uri('spline', 'icons/newspapers.png')}" alt=""> ${config['spline.site_title']} news:</div>
+        <div class="category">
+            <a href="${update.source.link}"><img src="${h.static_uri('spline', "icons/{0}.png".format(update.source.icon))}" alt=""> ${config['spline.site_title']} news</a>:
+        </div>
+        <div class="date">${update.post.posted_time}</div>
         <div class="title">
             <a href="${url(controller='forum', action='posts', forum_id=update.post.thread.forum.id, thread_id=update.post.thread.id)}">
                 ${update.post.thread.subject}
             </a>
         </div>
-        <div class="date">${update.post.posted_time}</div>
     </div>
     <div class="avatar">
         <a href="${url(controller='users', action='profile', id=update.post.author.id, name=update.post.author.name)}">
             ${userlib.avatar(update.post.author, size=48)}
         </a>
     </div>
-    <div class="content">${update.post.content}</div>
+    <div class="content has-comments">${update.post.content}</div>
     <div class="comments">
         <a href="${url(controller='forum', action='posts', forum_id=update.post.thread.forum.id, thread_id=update.post.thread.id)}">
             ${update.post.thread.post_count - 1} comment${'' if update.post.thread.post_count == 2 else 's'}