From 87a9812421e1908cee76656c52ca51042e8b9c25 Mon Sep 17 00:00:00 2001 From: Eevee Date: Sat, 24 Jul 2010 17:55:43 -0700 Subject: [PATCH] Update to new frontpage API, and keep frontpage optional. --- splinext/forum/__init__.py | 35 +++++--------- splinext/forum/frontpage_sources.py | 66 ++++++++++++++++++++++++++ splinext/forum/templates/forum/front_page.mako | 8 ++-- 3 files changed, 82 insertions(+), 27 deletions(-) create mode 100644 splinext/forum/frontpage_sources.py diff --git a/splinext/forum/__init__.py b/splinext/forum/__init__.py index 0c8edd3..07db391 100644 --- a/splinext/forum/__init__.py +++ b/splinext/forum/__init__.py @@ -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 index 0000000..f101871 --- /dev/null +++ b/splinext/forum/frontpage_sources.py @@ -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 diff --git a/splinext/forum/templates/forum/front_page.mako b/splinext/forum/templates/forum/front_page.mako index b967763..ba51503 100644 --- a/splinext/forum/templates/forum/front_page.mako +++ b/splinext/forum/templates/forum/front_page.mako @@ -3,20 +3,22 @@
-
${config['spline.site_title']} news:
+ +
${update.post.posted_time}
-
${update.post.posted_time}
-
${update.post.content}
+
${update.post.content}