d75bfe6850d92d1fe8ac5da642563cb6404b4e86
3 from pylons
import config
, request
, response
, session
, tmpl_context
as c
, url
4 from pylons
.controllers
.util
import abort
, redirect_to
5 from routes
import request_config
6 from sqlalchemy
.orm
.exc
import NoResultFound
8 from wtforms
import fields
10 from spline
.model
import meta
11 from spline
.lib
import helpers
as h
12 from spline
.lib
.base
import BaseController
, render
13 from splinext
.forum
import model
as forum_model
15 log
= logging
.getLogger(__name__
)
18 class WritePostForm(wtforms
.Form
):
19 content
= fields
.TextAreaField('Content')
21 class ForumController(BaseController
):
24 c
.forums
= meta
.Session
.query(forum_model
.Forum
) \
25 .order_by(forum_model
.Forum
.id.asc())
26 return render('/forum/forums.mako')
28 def threads(self
, forum_id
):
30 c
.forum
= meta
.Session
.query(forum_model
.Forum
).get(forum_id
)
34 c
.threads
= c
.forum
.threads
36 return render('/forum/threads.mako')
38 def posts(self
, forum_id
, thread_id
):
40 c
.thread
= meta
.Session
.query(forum_model
.Thread
) \
41 .filter_by(id=thread_id
, forum_id
=forum_id
).one()
45 return render('/forum/posts.mako')
48 def write(self
, forum_id
, thread_id
):
49 """Provides a form for posting to a thread."""
50 if not c
.user
.can('create_forum_post'):
54 c
.thread
= meta
.Session
.query(forum_model
.Thread
) \
55 .filter_by(id=thread_id
, forum_id
=forum_id
).one()
59 c
.form
= WritePostForm(request
.params
)
62 if request
.method
!= 'POST' or not c
.form
.validate():
63 # Failure or initial request; show the form
64 return render('/forum/write.mako')
67 # Otherwise, add the post.
68 c
.thread
= meta
.Session
.query(forum_model
.Thread
) \
69 .with_lockmode('update') \
72 post
= forum_model
.Post(
73 position
= c
.thread
.post_count
+ 1,
74 author_user_id
= c
.user
.id,
75 content
= c
.form
.content
.data
,
78 c
.thread
.posts
.append(post
)
79 c
.thread
.post_count
+= 1
83 # Redirect to the thread
84 # XXX probably to the post instead; anchor? depends on paging scheme
85 h
.flash('Your uniqueness has been added to our own.')
86 redirect_to(controller
='forum', action
='posts',
87 forum_id
=forum_id
, thread_id
=thread_id
,