From: Eevee Date: Wed, 8 Dec 2010 08:06:09 +0000 (-0800) Subject: ACTUALLY order threads in a forum by last-post time. X-Git-Tag: veekun-promotions/2010121201~1 X-Git-Url: http://git.veekun.com/zzz-spline-forum.git/commitdiff_plain/f36a20d6fd1cb45ef493211c3699c77b68f81ab8 ACTUALLY order threads in a forum by last-post time. --- diff --git a/splinext/forum/controllers/forum.py b/splinext/forum/controllers/forum.py index f2eb476..a27dd81 100644 --- a/splinext/forum/controllers/forum.py +++ b/splinext/forum/controllers/forum.py @@ -6,7 +6,7 @@ from pylons import cache, config, request, response, session, tmpl_context as c, from pylons.controllers.util import abort, redirect from pylons.decorators.secure import authenticate_form from routes import request_config -from sqlalchemy.orm import joinedload +from sqlalchemy.orm import aliased, contains_eager, joinedload from sqlalchemy.orm.exc import NoResultFound from sqlalchemy.sql import func import wtforms @@ -149,11 +149,12 @@ class ForumController(BaseController): c.write_thread_form = WriteThreadForm() # nb: This will never show post-less threads. Oh well! + last_post = aliased(forum_model.Post) threads_q = c.forum.threads \ - .join(forum_model.Thread.last_post) \ - .order_by(forum_model.Post.posted_time.desc()) \ + .join((last_post, forum_model.Thread.last_post)) \ + .order_by(last_post.posted_time.desc()) \ .options( - joinedload('last_post'), + contains_eager(forum_model.Thread.last_post, alias=last_post), joinedload('last_post.author'), ) c.num_threads = threads_q.count() diff --git a/splinext/forum/model/__init__.py b/splinext/forum/model/__init__.py index b8f9225..64cfda5 100644 --- a/splinext/forum/model/__init__.py +++ b/splinext/forum/model/__init__.py @@ -80,7 +80,7 @@ Index('thread_position', Post.thread_id, Post.position, unique=True) # XXX sort by time, how? -Forum.threads = relation(Thread, order_by=Thread.id.desc(), lazy='dynamic', backref='forum') +Forum.threads = relation(Thread, 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=[Post.thread_id], innerjoin=True, uselist=False, viewonly=True)