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 WriteThreadForm(WritePostForm
):
22 subject
= fields
.TextField('Subject')
24 class ForumController(BaseController
):
27 c
.forums
= meta
.Session
.query(forum_model
.Forum
) \
28 .order_by(forum_model
.Forum
.id.asc())
29 return render('/forum/forums.mako')
31 def threads(self
, forum_id
):
33 c
.forum
= meta
.Session
.query(forum_model
.Forum
).get(forum_id
)
37 c
.write_thread_form
= WriteThreadForm()
39 c
.threads
= c
.forum
.threads
41 return render('/forum/threads.mako')
43 def posts(self
, forum_id
, thread_id
):
45 c
.thread
= meta
.Session
.query(forum_model
.Thread
) \
46 .filter_by(id=thread_id
, forum_id
=forum_id
).one()
50 c
.write_post_form
= WritePostForm()
52 return render('/forum/posts.mako')
55 def write_thread(self
, forum_id
):
56 """Provides a form for posting a new thread."""
57 if not c
.user
.can('create_forum_thread'):
61 c
.forum
= meta
.Session
.query(forum_model
.Forum
) \
62 .filter_by(id=forum_id
).one()
66 c
.write_thread_form
= WriteThreadForm(request
.params
)
68 if request
.method
!= 'POST' or not c
.write_thread_form
.validate():
69 # Failure or initial request; show the form
70 return render('/forum/write_thread.mako')
73 # Otherwise, add the post.
74 c
.forum
= meta
.Session
.query(forum_model
.Forum
) \
75 .with_lockmode('update') \
78 thread
= forum_model
.Thread(
79 forum_id
= c
.forum
.id,
80 subject
= c
.write_thread_form
.subject
.data
,
83 post
= forum_model
.Post(
85 author_user_id
= c
.user
.id,
86 content
= c
.write_thread_form
.content
.data
,
89 thread
.posts
.append(post
)
90 c
.forum
.threads
.append(thread
)
94 # Redirect to the new thread
95 h
.flash("Contribution to the collective knowledge of the species successfully recorded.")
96 redirect_to(controller
='forum', action
='posts',
97 forum_id
=forum_id
, thread_id
=thread
.id,
100 def write(self
, forum_id
, thread_id
):
101 """Provides a form for posting to a thread."""
102 if not c
.user
.can('create_forum_post'):
106 c
.thread
= meta
.Session
.query(forum_model
.Thread
) \
107 .filter_by(id=thread_id
, forum_id
=forum_id
).one()
108 except NoResultFound
:
111 c
.write_post_form
= WritePostForm(request
.params
)
113 if request
.method
!= 'POST' or not c
.write_post_form
.validate():
114 # Failure or initial request; show the form
115 return render('/forum/write.mako')
118 # Otherwise, add the post.
119 c
.thread
= meta
.Session
.query(forum_model
.Thread
) \
120 .with_lockmode('update') \
123 post
= forum_model
.Post(
124 position
= c
.thread
.post_count
+ 1,
125 author_user_id
= c
.user
.id,
126 content
= c
.write_post_form
.content
.data
,
129 c
.thread
.posts
.append(post
)
130 c
.thread
.post_count
+= 1
132 meta
.Session
.commit()
134 # Redirect to the thread
135 # XXX probably to the post instead; anchor? depends on paging scheme
136 h
.flash('Your uniqueness has been added to our own.')
137 redirect_to(controller
='forum', action
='posts',
138 forum_id
=forum_id
, thread_id
=thread_id
,