Eagerloading for threads on the front page.
authorEevee <git@veekun.com>
Sun, 26 Sep 2010 04:49:57 +0000 (21:49 -0700)
committerEevee <git@veekun.com>
Sun, 26 Sep 2010 04:49:57 +0000 (21:49 -0700)
splinext/forum/frontpage_sources.py
splinext/forum/model/__init__.py
splinext/forum/templates/forum/front_page.mako

index f101871..69d0f3b 100644 (file)
@@ -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)
index 6bfdd1e..c913561 100644 (file)
@@ -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')
index d0b9d3d..5e03181 100644 (file)
@@ -8,7 +8,7 @@
         </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>
@@ -20,7 +20,7 @@
     </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>