From 3be1ed06645727c8d66d23a07ead0d09a24b55c6 Mon Sep 17 00:00:00 2001 From: Eevee Date: Wed, 19 May 2010 00:14:47 -0700 Subject: [PATCH] Posting! --- splinext/forum/__init__.py | 4 +++ splinext/forum/controllers/forum.py | 49 +++++++++++++++++++++++++++++++ splinext/forum/model/__init__.py | 4 ++- splinext/forum/templates/forum/write.mako | 12 ++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 splinext/forum/templates/forum/write.mako diff --git a/splinext/forum/__init__.py b/splinext/forum/__init__.py index 98ad4af..6914409 100644 --- a/splinext/forum/__init__.py +++ b/splinext/forum/__init__.py @@ -9,10 +9,14 @@ import splinext.forum.controllers.forum def add_routes_hook(map, *args, **kwargs): """Hook to inject some of our behavior into the routes configuration.""" + require_POST = dict(conditions=dict(method=['POST'])) + map.connect('/forums', controller='forum', action='forums') map.connect('/forums/{forum_id}', controller='forum', action='threads') map.connect('/forums/{forum_id}/threads/{thread_id}', controller='forum', action='posts') + map.connect('/forums/{forum_id}/threads/{thread_id}/write', controller='forum', action='write') + class ForumPlugin(PluginBase): def controllers(self): diff --git a/splinext/forum/controllers/forum.py b/splinext/forum/controllers/forum.py index 93eaed4..d75bfe6 100644 --- a/splinext/forum/controllers/forum.py +++ b/splinext/forum/controllers/forum.py @@ -4,6 +4,8 @@ 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.model import meta from spline.lib import helpers as h @@ -12,6 +14,10 @@ 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): @@ -37,3 +43,46 @@ class ForumController(BaseController): abort(404) 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) diff --git a/splinext/forum/model/__init__.py b/splinext/forum/model/__init__.py index 089a879..444f5b8 100644 --- a/splinext/forum/model/__init__.py +++ b/splinext/forum/model/__init__.py @@ -1,3 +1,5 @@ +from datetime import datetime + from sqlalchemy import and_, Column, ForeignKey, Index from sqlalchemy.orm import relation from sqlalchemy.types import DateTime, Integer, Unicode @@ -39,7 +41,7 @@ class Post(TableBase): thread_id = Column(Integer, ForeignKey('threads.id'), nullable=False) position = Column(Integer, nullable=False) author_user_id = Column(Integer, ForeignKey('users.id'), nullable=False) - posted_time = Column(DateTime, nullable=False, index=True) + posted_time = Column(DateTime, nullable=False, index=True, default=datetime.now) content = Column(Unicode(5120), nullable=False) Index('thread_position', Post.thread_id, Post.position, unique=True) diff --git a/splinext/forum/templates/forum/write.mako b/splinext/forum/templates/forum/write.mako new file mode 100644 index 0000000..79138f3 --- /dev/null +++ b/splinext/forum/templates/forum/write.mako @@ -0,0 +1,12 @@ +<%inherit file="/base.mako" /> +<%namespace name="lib" file="/lib.mako" /> + +<%def name="title()">Post to ${c.thread.subject} - ${c.thread.forum.name} - Forums + +${h.form(url.current())} +
+ ${lib.field('content', rows=12, cols=80)} + +
+
+${h.end_form()} -- 2.7.4