3 from pylons
import config
, request
, response
, session
, tmpl_context
as c
, url
4 from pylons
.controllers
.util
import abort
, redirect
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
):
32 c
.forum
= meta
.Session
.query(forum_model
.Forum
).get(forum_id
)
36 c
.write_thread_form
= WriteThreadForm()
38 c
.threads
= c
.forum
.threads
40 return render('/forum/threads.mako')
42 def posts(self
, forum_id
, thread_id
):
44 c
.thread
= meta
.Session
.query(forum_model
.Thread
) \
45 .filter_by(id=thread_id
, forum_id
=forum_id
).one()
49 c
.write_post_form
= WritePostForm()
51 return render('/forum/posts.mako')
54 def write_thread(self
, forum_id
):
55 """Provides a form for posting a new thread."""
56 if not c
.user
.can('forum:create-thread'):
60 c
.forum
= meta
.Session
.query(forum_model
.Forum
) \
61 .filter_by(id=forum_id
).one()
65 c
.write_thread_form
= WriteThreadForm(request
.params
)
67 if request
.method
!= 'POST' or not c
.write_thread_form
.validate():
68 # Failure or initial request; show the form
69 return render('/forum/write_thread.mako')
72 # Otherwise, add the post.
73 c
.forum
= meta
.Session
.query(forum_model
.Forum
) \
74 .with_lockmode('update') \
77 thread
= forum_model
.Thread(
78 forum_id
= c
.forum
.id,
79 subject
= c
.write_thread_form
.subject
.data
,
82 post
= forum_model
.Post(
84 author_user_id
= c
.user
.id,
85 content
= c
.write_thread_form
.content
.data
,
88 thread
.posts
.append(post
)
89 c
.forum
.threads
.append(thread
)
93 # Redirect to the new thread
94 h
.flash("Contribution to the collective knowledge of the species successfully recorded.")
96 url(controller
='forum', action
='posts',
97 forum_id
=forum_id
, thread_id
=thread
.id),
101 def write(self
, forum_id
, thread_id
):
102 """Provides a form for posting to a thread."""
103 if not c
.user
.can('forum:create-post'):
107 c
.thread
= meta
.Session
.query(forum_model
.Thread
) \
108 .filter_by(id=thread_id
, forum_id
=forum_id
).one()
109 except NoResultFound
:
112 c
.write_post_form
= WritePostForm(request
.params
)
114 if request
.method
!= 'POST' or not c
.write_post_form
.validate():
115 # Failure or initial request; show the form
116 return render('/forum/write.mako')
119 # Otherwise, add the post.
120 c
.thread
= meta
.Session
.query(forum_model
.Thread
) \
121 .with_lockmode('update') \
124 post
= forum_model
.Post(
125 position
= c
.thread
.post_count
+ 1,
126 author_user_id
= c
.user
.id,
127 content
= c
.write_post_form
.content
.data
,
130 c
.thread
.posts
.append(post
)
131 c
.thread
.post_count
+= 1
133 meta
.Session
.commit()
135 # Redirect to the thread
136 # XXX probably to the post instead; anchor? depends on paging scheme
137 h
.flash('Your uniqueness has been added to our own.')
139 url(controller
='forum', action
='posts',
140 forum_id
=forum_id
, thread_id
=thread_id
),