From: Eevee Date: Sun, 26 Sep 2010 04:49:57 +0000 (-0700) Subject: Eagerloading for threads on the front page. X-Git-Tag: veekun-promotions/2010101501~1 X-Git-Url: http://git.veekun.com/zzz-spline-forum.git/commitdiff_plain/17e9ab6fa515104bca6b419b547335dc105f13ec?ds=sidebyside Eagerloading for threads on the front page. --- diff --git a/splinext/forum/frontpage_sources.py b/splinext/forum/frontpage_sources.py index f101871..69d0f3b 100644 --- a/splinext/forum/frontpage_sources.py +++ b/splinext/forum/frontpage_sources.py @@ -1,5 +1,6 @@ from collections import namedtuple +from sqlalchemy.orm import contains_eager, joinedload from pylons import url from spline.model import meta @@ -45,7 +46,12 @@ class ForumSource(Source): 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) diff --git a/splinext/forum/model/__init__.py b/splinext/forum/model/__init__.py index 6bfdd1e..c913561 100644 --- a/splinext/forum/model/__init__.py +++ b/splinext/forum/model/__init__.py @@ -83,8 +83,8 @@ Index('thread_position', Post.thread_id, Post.position, unique=True) 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') diff --git a/splinext/forum/templates/forum/front_page.mako b/splinext/forum/templates/forum/front_page.mako index d0b9d3d..5e03181 100644 --- a/splinext/forum/templates/forum/front_page.mako +++ b/splinext/forum/templates/forum/front_page.mako @@ -8,7 +8,7 @@
${update.post.posted_time}
- + ${update.post.thread.subject}
@@ -20,7 +20,7 @@
${update.post.content|n}
- + ${update.post.thread.post_count - 1} comment${'' if update.post.thread.post_count == 2 else 's'}