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):
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
log = logging.getLogger(__name__)
+
+class WritePostForm(wtforms.Form):
+ content = fields.TextAreaField('Content')
+
class ForumController(BaseController):
def forums(self):
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)
+from datetime import datetime
+
from sqlalchemy import and_, Column, ForeignKey, Index
from sqlalchemy.orm import relation
from sqlalchemy.types import DateTime, Integer, Unicode
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)
--- /dev/null
+<%inherit file="/base.mako" />
+<%namespace name="lib" file="/lib.mako" />
+
+<%def name="title()">Post to ${c.thread.subject} - ${c.thread.forum.name} - Forums</%def>
+
+${h.form(url.current())}
+<dl class="standard-form">
+ ${lib.field('content', rows=12, cols=80)}
+
+ <dd><button type="submit">Post!</button></dd>
+</dl>
+${h.end_form()}