1 from collections
import namedtuple
5 from spline
.model
import meta
7 from splinext
.forum
import model
as forum_model
8 from splinext
.frontpage
.sources
import Source
10 def frontpage_hook(limit
, max_age
, forum_id
):
11 """Hook to return recent news for the front page."""
13 FrontPageThread
= namedtuple('FrontPageThread', ['source', 'time', 'post'])
14 class ForumSource(Source
):
15 """Represents a forum whose threads are put on the front page.
17 ``link``, ``title``, and ``icon`` are all optional; the link and title
18 default to the forum's thread list and name, and the icon defaults to a
24 id of the forum to check for new threads.
27 template
= '/forum/front_page.mako'
29 def __init__(self
, forum_id
, **kwargs
):
30 forum
= meta
.Session
.query(forum_model
.Forum
).get(forum_id
)
32 # Link is tricky. Needs url(), which doesn't exist when this class is
33 # loaded. Lazy-load it in poll() below, instead
34 kwargs
.setdefault('link', None)
35 kwargs
.setdefault('title', forum
.name
)
36 kwargs
.setdefault('icon', 'newspapers')
37 super(ForumSource
, self
).__init__(**kwargs
)
39 self
.forum_id
= forum_id
41 def _poll(self
, limit
, max_age
):
44 controller
='forum', action
='threads', forum_id
=self
.forum_id
)
46 thread_q
= meta
.Session
.query(forum_model
.Thread
) \
47 .filter_by(forum_id
=self
.forum_id
) \
48 .join(forum_model
.Thread
.first_post
)
51 thread_q
= thread_q
.filter(forum_model
.Post
.posted_time
>= max_age
)
54 .order_by(forum_model
.Post
.posted_time
.desc()) \
58 for thread
in threads
:
59 update
= FrontPageThread(
61 time
= thread
.first_post
.posted_time
,
62 post
= thread
.first_post
,
64 updates
.append(update
)