ACTUALLY order threads in a forum by last-post time.
[zzz-spline-forum.git] / splinext / forum / controllers / forum.py
index 5b5ecf9..a27dd81 100644 (file)
@@ -4,8 +4,9 @@ import math
 
 from pylons import cache, config, request, response, session, tmpl_context as c, url
 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
@@ -148,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()
@@ -200,11 +202,25 @@ class ForumController(BaseController):
             abort(404)
 
         c.write_thread_form = WriteThreadForm(request.params)
+        return render('/forum/write_thread.mako')
 
-        if request.method != 'POST' or not c.write_thread_form.validate():
-            # Failure or initial request; show the form
-            return render('/forum/write_thread.mako')
+    @authenticate_form
+    def write_thread_commit(self, forum_id):
+        """Posts a new thread."""
+        if not c.user.can('forum:create-thread'):
+            abort(403)
+
+        try:
+            c.forum = meta.Session.query(forum_model.Forum) \
+                .filter_by(id=forum_id).one()
+        except NoResultFound:
+            abort(404)
 
+        c.write_thread_form = WriteThreadForm(request.params)
+
+        # Reshow the form on failure
+        if not c.write_thread_form.validate():
+            return render('/forum/write_thread.mako')
 
         # Otherwise, add the post.
         c.forum = meta.Session.query(forum_model.Forum) \
@@ -249,11 +265,25 @@ class ForumController(BaseController):
             abort(404)
 
         c.write_post_form = WritePostForm(request.params)
+        return render('/forum/write.mako')
 
-        if request.method != 'POST' or not c.write_post_form.validate():
-            # Failure or initial request; show the form
-            return render('/forum/write.mako')
+    @authenticate_form
+    def write_commit(self, forum_id, thread_id):
+        """Post to a thread."""
+        if not c.user.can('forum:create-post'):
+            abort(403)
+
+        try:
+            c.thread = meta.Session.query(forum_model.Thread) \
+                .filter_by(id=thread_id, forum_id=forum_id).one()
+        except NoResultFound:
+            abort(404)
 
+        c.write_post_form = WritePostForm(request.params)
+
+        # Reshow the form on failure
+        if not c.write_post_form.validate():
+            return render('/forum/write.mako')
 
         # Otherwise, add the post.
         c.thread = meta.Session.query(forum_model.Thread) \