X-Git-Url: http://git.veekun.com/zzz-spline-forum.git/blobdiff_plain/d2ee32dbc7d8987a3408889daa1d27b3533bfa27..3be1ed06645727c8d66d23a07ead0d09a24b55c6:/splinext/forum/controllers/forum.py?ds=sidebyside diff --git a/splinext/forum/controllers/forum.py b/splinext/forum/controllers/forum.py index bf66618..d75bfe6 100644 --- a/splinext/forum/controllers/forum.py +++ b/splinext/forum/controllers/forum.py @@ -4,33 +4,85 @@ from pylons import config, request, response, session, tmpl_context as c, url from pylons.controllers.util import abort, redirect_to from routes import request_config from sqlalchemy.orm.exc import NoResultFound +import wtforms +from wtforms import fields -from spline import model from spline.model import meta from spline.lib import helpers as h from spline.lib.base import BaseController, render +from splinext.forum import model as forum_model log = logging.getLogger(__name__) + +class WritePostForm(wtforms.Form): + content = fields.TextAreaField('Content') + class ForumController(BaseController): def forums(self): - c.forums = meta.Session.query(model.Forum).order_by(model.Forum.id.asc()) + c.forums = meta.Session.query(forum_model.Forum) \ + .order_by(forum_model.Forum.id.asc()) return render('/forum/forums.mako') def threads(self, forum_id): try: - c.forum = meta.Session.query(model.Forum).get(forum_id) + c.forum = meta.Session.query(forum_model.Forum).get(forum_id) except NoResultFound: abort(404) + c.threads = c.forum.threads + return render('/forum/threads.mako') def posts(self, forum_id, thread_id): try: - c.thread = meta.Session.query(model.Thread) \ + c.thread = meta.Session.query(forum_model.Thread) \ .filter_by(id=thread_id, forum_id=forum_id).one() except NoResultFound: abort(404) - return render('/forum/threads.mako') + return render('/forum/posts.mako') + + + def write(self, forum_id, thread_id): + """Provides a form for posting to a thread.""" + if not c.user.can('create_forum_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.form = WritePostForm(request.params) + c.write_mode = 'post' + + if request.method != 'POST' or not c.form.validate(): + # Failure or initial request; show the form + return render('/forum/write.mako') + + + # Otherwise, add the post. + c.thread = meta.Session.query(forum_model.Thread) \ + .with_lockmode('update') \ + .get(c.thread.id) + + post = forum_model.Post( + position = c.thread.post_count + 1, + author_user_id = c.user.id, + content = c.form.content.data, + ) + + c.thread.posts.append(post) + c.thread.post_count += 1 + + meta.Session.commit() + + # 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)