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."""
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):
)
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
--- /dev/null
+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
<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'}