b15302cb159eb7999754c10b04a7b392ef87520a
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 import spline
.lib
.markdown
14 from splinext
.forum
import model
as forum_model
16 log
= logging
.getLogger(__name__
)
19 class WritePostForm(wtforms
.Form
):
20 content
= fields
.TextAreaField('Content')
22 class WriteThreadForm(WritePostForm
):
23 subject
= fields
.TextField('Subject')
25 class ForumController(BaseController
):
28 c
.forums
= meta
.Session
.query(forum_model
.Forum
) \
29 .order_by(forum_model
.Forum
.id.asc())
30 return render('/forum/forums.mako')
32 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('forum:create-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.")
97 url(controller
='forum', action
='posts',
98 forum_id
=forum_id
, thread_id
=thread
.id),
102 def write(self
, forum_id
, thread_id
):
103 """Provides a form for posting to a thread."""
104 if not c
.user
.can('forum:create-post'):
108 c
.thread
= meta
.Session
.query(forum_model
.Thread
) \
109 .filter_by(id=thread_id
, forum_id
=forum_id
).one()
110 except NoResultFound
:
113 c
.write_post_form
= WritePostForm(request
.params
)
115 if request
.method
!= 'POST' or not c
.write_post_form
.validate():
116 # Failure or initial request; show the form
117 return render('/forum/write.mako')
120 # Otherwise, add the post.
121 c
.thread
= meta
.Session
.query(forum_model
.Thread
) \
122 .with_lockmode('update') \
125 source
= c
.write_post_form
.content
.data
126 post
= forum_model
.Post(
127 position
= c
.thread
.post_count
+ 1,
128 author_user_id
= c
.user
.id,
129 raw_content
= source
,
130 content
= spline
.lib
.markdown
.translate(source
),
133 c
.thread
.posts
.append(post
)
134 c
.thread
.post_count
+= 1
136 meta
.Session
.commit()
138 # Redirect to the thread
139 # XXX probably to the post instead; anchor? depends on paging scheme
140 h
.flash('Your uniqueness has been added to our own.')
142 url(controller
='forum', action
='posts',
143 forum_id
=forum_id
, thread_id
=thread_id
),