from collections import namedtuple
+from sqlalchemy.orm import contains_eager, joinedload
from pylons import url
from spline.model import meta
thread_q = meta.Session.query(forum_model.Thread) \
.filter_by(forum_id=self.forum_id) \
- .join(forum_model.Thread.first_post)
+ .join((forum_model.Post, forum_model.Thread.first_post)) \
+ .options(
+ contains_eager(forum_model.Thread.first_post, alias=forum_model.Post),
+ contains_eager(forum_model.Thread.first_post, forum_model.Post.thread, alias=forum_model.Thread),
+ joinedload(forum_model.Thread.first_post, forum_model.Post.author),
+ )
if max_age:
thread_q = thread_q.filter(forum_model.Post.posted_time >= max_age)
Forum.threads = relation(Thread, order_by=Thread.id.desc(), lazy='dynamic', backref='forum')
Thread.posts = relation(Post, order_by=Post.position.asc(), lazy='dynamic', backref='thread')
-Thread.first_post = relation(Post, primaryjoin=and_(Post.thread_id == Thread.id, Post.position == 1), foreign_keys=[Thread.id], innerjoin=True, uselist=False)
+Thread.first_post = relation(Post, primaryjoin=and_(Post.thread_id == Thread.id, Post.position == 1), foreign_keys=[Post.thread_id], innerjoin=True, uselist=False, viewonly=True)
# XXX THIS WILL NEED TO CHANGE when posts can be deleted! Or change what 'position' means
-Thread.last_post = relation(Post, primaryjoin=and_(Post.thread_id == Thread.id, Post.position == Thread.post_count), foreign_keys=[Thread.id, Thread.post_count], innerjoin=True, uselist=False)
+Thread.last_post = relation(Post, primaryjoin=and_(Post.thread_id == Thread.id, Post.position == Thread.post_count), foreign_keys=[Post.thread_id], innerjoin=True, uselist=False, viewonly=True)
Post.author = relation(users_model.User, backref='posts')
</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)}">
+ <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>
<div class="content has-comments">${update.post.content|n}</div>
<div class="comments">
- <a href="${url(controller='forum', action='posts', forum_id=update.post.thread.forum.id, thread_id=update.post.thread.id)}">
+ <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'}
</a>
</div>