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}/write', controller='forum', action='write_thread')
map.connect('/forums/{forum_id}/threads/{thread_id}/write', controller='forum', action='write')
class WritePostForm(wtforms.Form):
content = fields.TextAreaField('Content')
+class WriteThreadForm(WritePostForm):
+ subject = fields.TextField('Subject')
+
class ForumController(BaseController):
def forums(self):
except NoResultFound:
abort(404)
+ c.write_thread_form = WriteThreadForm()
+
c.threads = c.forum.threads
return render('/forum/threads.mako')
except NoResultFound:
abort(404)
+ c.write_post_form = WritePostForm()
+
return render('/forum/posts.mako')
+ def write_thread(self, forum_id):
+ """Provides a form for posting a new thread."""
+ if not c.user.can('create_forum_thread'):
+ abort(403)
+
+ try:
+ c.forum = meta.Session.query(forum_model.Forum) \
+ .filter_by(id=forum_id).one()
+ except NoResultFound:
+ abort(404)
+
+ c.write_thread_form = WriteThreadForm(request.params)
+
+ if request.method != 'POST' or not c.write_thread_form.validate():
+ # Failure or initial request; show the form
+ return render('/forum/write_thread.mako')
+
+
+ # Otherwise, add the post.
+ c.forum = meta.Session.query(forum_model.Forum) \
+ .with_lockmode('update') \
+ .get(c.forum.id)
+
+ thread = forum_model.Thread(
+ forum_id = c.forum.id,
+ subject = c.write_thread_form.subject.data,
+ post_count = 1,
+ )
+ post = forum_model.Post(
+ position = 1,
+ author_user_id = c.user.id,
+ content = c.write_thread_form.content.data,
+ )
+
+ thread.posts.append(post)
+ c.forum.threads.append(thread)
+
+ meta.Session.commit()
+
+ # Redirect to the new thread
+ h.flash("Contribution to the collective knowledge of the species successfully recorded.")
+ redirect_to(controller='forum', action='posts',
+ forum_id=forum_id, thread_id=thread.id,
+ _code=303)
+
def write(self, forum_id, thread_id):
"""Provides a form for posting to a thread."""
if not c.user.can('create_forum_post'):
except NoResultFound:
abort(404)
- c.form = WritePostForm(request.params)
- c.write_mode = 'post'
+ c.write_post_form = WritePostForm(request.params)
- if request.method != 'POST' or not c.form.validate():
+ if request.method != 'POST' or not c.write_post_form.validate():
# Failure or initial request; show the form
return render('/forum/write.mako')
post = forum_model.Post(
position = c.thread.post_count + 1,
author_user_id = c.user.id,
- content = c.form.content.data,
+ content = c.write_post_form.content.data,
)
c.thread.posts.append(post)
# XXX sort by time, how?
Forum.threads = relation(Thread, order_by=Thread.id.desc(), lazy='dynamic', backref='forum')
-Thread.posts = relation(Post, order_by=Post.posted_time.desc(), lazy='dynamic', backref='thread')
+Thread.posts = relation(Post, order_by=Post.position.asc(), lazy='dynamic', backref='thread')
Thread.first_post = relation(Post, primaryjoin=and_(Post.thread_id == Thread.id, Post.position == 1), uselist=False)
Thread.last_post = relation(Post, primaryjoin=and_(Post.thread_id == Thread.id, Post.position == Thread.post_count), uselist=False)
<%def name="title()">Forums</%def>
+<h1>Forums</h1>
<table class="forum-list striped-rows">
<thead>
<tr class="header-row">
--- /dev/null
+<%namespace name="lib" file="/lib.mako" />
+
+<%def name="write_thread_form(forum)">
+% if c.user.can('create_forum_thread'):
+<h1>Create new thread</h1>
+${h.form(url(controller='forum', action='write_thread', forum_id=forum.id))}
+<dl class="standard-form">
+ ${lib.field('subject', form=c.write_thread_form)}
+ ${lib.field('content', form=c.write_thread_form, rows=12, cols=80)}
+
+ <dd><button type="submit">Post!</button></dd>
+</dl>
+${h.end_form()}
+% endif ## can create post
+</%def>
+
+<%def name="write_post_form(thread)">
+% if c.user.can('create_forum_post'):
+<h1>Reply</h1>
+${h.form(url(controller='forum', action='write', forum_id=thread.forum.id, thread_id=thread.id))}
+<dl class="standard-form">
+ ${lib.field('content', form=c.write_post_form, rows=12, cols=80)}
+
+ <dd><button type="submit">Post!</button></dd>
+</dl>
+${h.end_form()}
+% endif ## can create post
+</%def>
<%inherit file="/base.mako" />
+<%namespace name="forumlib" file="/forum/lib.mako" />
<%def name="title()">${c.thread.subject} - Forums</%def>
</div>
% endfor
</div>
+
+${forumlib.write_post_form(c.thread)}
<%inherit file="/base.mako" />
+<%namespace name="forumlib" file="/forum/lib.mako" />
<%def name="title()">${c.forum.name} - Forums</%def>
% endfor
</tbody>
</table>
+
+${forumlib.write_thread_form(c.forum)}
<%inherit file="/base.mako" />
-<%namespace name="lib" file="/lib.mako" />
+<%namespace name="forumlib" file="/forum/lib.mako" />
-<%def name="title()">Post to ${c.thread.subject} - ${c.thread.forum.name} - Forums</%def>
+<%def name="title()">Reply to ${c.thread.subject} - Forums</%def>
-${h.form(url.current())}
-<dl class="standard-form">
- ${lib.field('content', rows=12, cols=80)}
+<%def name="title_in_page()">
+<ul id="breadcrumbs">
+ <li><a href="${url(controller='forum', action='forums')}">Forums</a></li>
+ <li><a href="${url(controller='forum', action='threads', forum_id=c.thread.forum.id)}">${c.thread.forum.name}</a></li>
+ <li><a href="${url(controller='forum', action='posts', thread_id=c.thread.id, forum_id=c.thread.forum.id)}">${c.thread.subject}</a></li>
+ <li>Reply</li>
+</ul>
+</%def>
- <dd><button type="submit">Post!</button></dd>
-</dl>
-${h.end_form()}
+${forumlib.write_post_form(c.thread)}
--- /dev/null
+<%inherit file="/base.mako" />
+<%namespace name="forumlib" file="/forum/lib.mako" />
+
+<%def name="title()">Create a thread in ${c.forum.name} - Forums</%def>
+
+<%def name="title_in_page()">
+<ul id="breadcrumbs">
+ <li><a href="${url(controller='forum', action='forums')}">Forums</a></li>
+ <li><a href="${url(controller='forum', action='threads', forum_id=c.forum.id)}">${c.forum.name}</a></li>
+ <li>Create a thread</li>
+</ul>
+</%def>
+
+${forumlib.write_thread_form(c.forum)}