Posting!
authorEevee <git@veekun.com>
Wed, 19 May 2010 07:14:47 +0000 (00:14 -0700)
committerEevee <git@veekun.com>
Wed, 19 May 2010 07:14:47 +0000 (00:14 -0700)
splinext/forum/__init__.py
splinext/forum/controllers/forum.py
splinext/forum/model/__init__.py
splinext/forum/templates/forum/write.mako [new file with mode: 0644]

index 98ad4af..6914409 100644 (file)
@@ -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."""
 
 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', 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):
 
 class ForumPlugin(PluginBase):
     def controllers(self):
index 93eaed4..d75bfe6 100644 (file)
@@ -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
 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
 
 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__)
 
 
 log = logging.getLogger(__name__)
 
+
+class WritePostForm(wtforms.Form):
+    content = fields.TextAreaField('Content')
+
 class ForumController(BaseController):
 
     def forums(self):
 class ForumController(BaseController):
 
     def forums(self):
@@ -37,3 +43,46 @@ class ForumController(BaseController):
             abort(404)
 
         return render('/forum/posts.mako')
             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)
index 089a879..444f5b8 100644 (file)
@@ -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
 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)
     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)
     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 (file)
index 0000000..79138f3
--- /dev/null
@@ -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</%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()}