Upgrade to Pylons 1.0. #283
[zzz-spline-forum.git] / splinext / forum / controllers / forum.py
index d75bfe6..6cd4bfb 100644 (file)
@@ -1,7 +1,7 @@
 import logging
 
 from pylons import config, request, response, session, tmpl_context as c, url
-from pylons.controllers.util import abort, redirect_to
+from pylons.controllers.util import abort, redirect
 from routes import request_config
 from sqlalchemy.orm.exc import NoResultFound
 import wtforms
@@ -18,6 +18,9 @@ log = logging.getLogger(__name__)
 class WritePostForm(wtforms.Form):
     content = fields.TextAreaField('Content')
 
+class WriteThreadForm(WritePostForm):
+    subject = fields.TextField('Subject')
+
 class ForumController(BaseController):
 
     def forums(self):
@@ -26,11 +29,12 @@ class ForumController(BaseController):
         return render('/forum/forums.mako')
 
     def threads(self, forum_id):
-        try:
-            c.forum = meta.Session.query(forum_model.Forum).get(forum_id)
-        except NoResultFound:
+        c.forum = meta.Session.query(forum_model.Forum).get(forum_id)
+        if not c.forum:
             abort(404)
 
+        c.write_thread_form = WriteThreadForm()
+
         c.threads = c.forum.threads
 
         return render('/forum/threads.mako')
@@ -42,12 +46,61 @@ class ForumController(BaseController):
         except NoResultFound:
             abort(404)
 
+        c.write_post_form = WritePostForm()
+
         return render('/forum/posts.mako')
 
 
+    def write_thread(self, forum_id):
+        """Provides a form for posting 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)
+
+        if request.method != 'POST' or not c.write_thread_form.validate():
+            # Failure or initial request; show the form
+            return render('/forum/write_thread.mako')
+
+
+        # Otherwise, add the post.
+        c.forum = meta.Session.query(forum_model.Forum) \
+            .with_lockmode('update') \
+            .get(c.forum.id)
+
+        thread = forum_model.Thread(
+            forum_id = c.forum.id,
+            subject = c.write_thread_form.subject.data,
+            post_count = 1,
+        )
+        post = forum_model.Post(
+            position = 1,
+            author_user_id = c.user.id,
+            content = c.write_thread_form.content.data,
+        )
+
+        thread.posts.append(post)
+        c.forum.threads.append(thread)
+
+        meta.Session.commit()
+
+        # Redirect to the new thread
+        h.flash("Contribution to the collective knowledge of the species successfully recorded.")
+        redirect(
+            url(controller='forum', action='posts',
+                forum_id=forum_id, thread_id=thread.id),
+            code=303,
+        )
+
     def write(self, forum_id, thread_id):
         """Provides a form for posting to a thread."""
-        if not c.user.can('create_forum_post'):
+        if not c.user.can('forum:create-post'):
             abort(403)
 
         try:
@@ -56,10 +109,9 @@ class ForumController(BaseController):
         except NoResultFound:
             abort(404)
 
-        c.form = WritePostForm(request.params)
-        c.write_mode = 'post'
+        c.write_post_form = WritePostForm(request.params)
 
-        if request.method != 'POST' or not c.form.validate():
+        if request.method != 'POST' or not c.write_post_form.validate():
             # Failure or initial request; show the form
             return render('/forum/write.mako')
 
@@ -72,7 +124,7 @@ class ForumController(BaseController):
         post = forum_model.Post(
             position = c.thread.post_count + 1,
             author_user_id = c.user.id,
-            content = c.form.content.data,
+            content = c.write_post_form.content.data,
         )
 
         c.thread.posts.append(post)
@@ -83,6 +135,8 @@ class ForumController(BaseController):
         # Redirect to the thread
         # XXX probably to the post instead; anchor?  depends on paging scheme
         h.flash('Your uniqueness has been added to our own.')
-        redirect_to(controller='forum', action='posts',
-            forum_id=forum_id, thread_id=thread_id,
-            _code=303)
+        redirect(
+            url(controller='forum', action='posts',
+                forum_id=forum_id, thread_id=thread_id),
+            code=303,
+        )