From: Eevee Date: Thu, 17 Jun 2010 01:00:58 +0000 (-0700) Subject: Thread creation and some tiny posting polish. X-Git-Tag: veekun-promotions/2010070701~1 X-Git-Url: http://git.veekun.com/zzz-spline-forum.git/commitdiff_plain/dfc83133694768eb466d4a55bbb2c501ac508e92 Thread creation and some tiny posting polish. --- diff --git a/splinext/forum/__init__.py b/splinext/forum/__init__.py index 6914409..e234c14 100644 --- a/splinext/forum/__init__.py +++ b/splinext/forum/__init__.py @@ -15,6 +15,7 @@ def add_routes_hook(map, *args, **kwargs): 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') diff --git a/splinext/forum/controllers/forum.py b/splinext/forum/controllers/forum.py index d75bfe6..b5bacab 100644 --- a/splinext/forum/controllers/forum.py +++ b/splinext/forum/controllers/forum.py @@ -18,6 +18,9 @@ log = logging.getLogger(__name__) class WritePostForm(wtforms.Form): content = fields.TextAreaField('Content') +class WriteThreadForm(WritePostForm): + subject = fields.TextField('Subject') + class ForumController(BaseController): def forums(self): @@ -31,6 +34,8 @@ class ForumController(BaseController): except NoResultFound: abort(404) + c.write_thread_form = WriteThreadForm() + c.threads = c.forum.threads return render('/forum/threads.mako') @@ -42,9 +47,56 @@ class ForumController(BaseController): 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'): @@ -56,10 +108,9 @@ class ForumController(BaseController): 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') @@ -72,7 +123,7 @@ class ForumController(BaseController): 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) diff --git a/splinext/forum/model/__init__.py b/splinext/forum/model/__init__.py index 444f5b8..a137866 100644 --- a/splinext/forum/model/__init__.py +++ b/splinext/forum/model/__init__.py @@ -50,7 +50,7 @@ Index('thread_position', Post.thread_id, Post.position, unique=True) # 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) diff --git a/splinext/forum/templates/forum/forums.mako b/splinext/forum/templates/forum/forums.mako index d925e5b..21c0289 100644 --- a/splinext/forum/templates/forum/forums.mako +++ b/splinext/forum/templates/forum/forums.mako @@ -2,6 +2,7 @@ <%def name="title()">Forums +

Forums

diff --git a/splinext/forum/templates/forum/lib.mako b/splinext/forum/templates/forum/lib.mako new file mode 100644 index 0000000..e2aafde --- /dev/null +++ b/splinext/forum/templates/forum/lib.mako @@ -0,0 +1,28 @@ +<%namespace name="lib" file="/lib.mako" /> + +<%def name="write_thread_form(forum)"> +% if c.user.can('create_forum_thread'): +

Create new thread

+${h.form(url(controller='forum', action='write_thread', forum_id=forum.id))} +
+ ${lib.field('subject', form=c.write_thread_form)} + ${lib.field('content', form=c.write_thread_form, rows=12, cols=80)} + +
+
+${h.end_form()} +% endif ## can create post + + +<%def name="write_post_form(thread)"> +% if c.user.can('create_forum_post'): +

Reply

+${h.form(url(controller='forum', action='write', forum_id=thread.forum.id, thread_id=thread.id))} +
+ ${lib.field('content', form=c.write_post_form, rows=12, cols=80)} + +
+
+${h.end_form()} +% endif ## can create post + diff --git a/splinext/forum/templates/forum/posts.mako b/splinext/forum/templates/forum/posts.mako index 24dd875..b97954f 100644 --- a/splinext/forum/templates/forum/posts.mako +++ b/splinext/forum/templates/forum/posts.mako @@ -1,4 +1,5 @@ <%inherit file="/base.mako" /> +<%namespace name="forumlib" file="/forum/lib.mako" /> <%def name="title()">${c.thread.subject} - Forums @@ -28,3 +29,5 @@ % endfor + +${forumlib.write_post_form(c.thread)} diff --git a/splinext/forum/templates/forum/threads.mako b/splinext/forum/templates/forum/threads.mako index 3add2ad..8aab297 100644 --- a/splinext/forum/templates/forum/threads.mako +++ b/splinext/forum/templates/forum/threads.mako @@ -1,4 +1,5 @@ <%inherit file="/base.mako" /> +<%namespace name="forumlib" file="/forum/lib.mako" /> <%def name="title()">${c.forum.name} - Forums @@ -25,3 +26,5 @@ % endfor
+ +${forumlib.write_thread_form(c.forum)} diff --git a/splinext/forum/templates/forum/write.mako b/splinext/forum/templates/forum/write.mako index 79138f3..a9ec60b 100644 --- a/splinext/forum/templates/forum/write.mako +++ b/splinext/forum/templates/forum/write.mako @@ -1,12 +1,15 @@ <%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 name="title()">Reply to ${c.thread.subject} - Forums -${h.form(url.current())} -
- ${lib.field('content', rows=12, cols=80)} +<%def name="title_in_page()"> + + -
-
-${h.end_form()} +${forumlib.write_post_form(c.thread)} diff --git a/splinext/forum/templates/forum/write_thread.mako b/splinext/forum/templates/forum/write_thread.mako new file mode 100644 index 0000000..11774df --- /dev/null +++ b/splinext/forum/templates/forum/write_thread.mako @@ -0,0 +1,14 @@ +<%inherit file="/base.mako" /> +<%namespace name="forumlib" file="/forum/lib.mako" /> + +<%def name="title()">Create a thread in ${c.forum.name} - Forums + +<%def name="title_in_page()"> + + + +${forumlib.write_thread_form(c.forum)}