Crash fix: /forums/999999 would crash instead of 404.
[zzz-spline-forum.git] / splinext / forum / controllers / forum.py
1 import logging
2
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
7 import wtforms
8 from wtforms import fields
9
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
14
15 log = logging.getLogger(__name__)
16
17
18 class WritePostForm(wtforms.Form):
19 content = fields.TextAreaField('Content')
20
21 class WriteThreadForm(WritePostForm):
22 subject = fields.TextField('Subject')
23
24 class ForumController(BaseController):
25
26 def forums(self):
27 c.forums = meta.Session.query(forum_model.Forum) \
28 .order_by(forum_model.Forum.id.asc())
29 return render('/forum/forums.mako')
30
31 def threads(self, forum_id):
32 c.forum = meta.Session.query(forum_model.Forum).get(forum_id)
33 if not c.forum:
34 abort(404)
35
36 c.write_thread_form = WriteThreadForm()
37
38 c.threads = c.forum.threads
39
40 return render('/forum/threads.mako')
41
42 def posts(self, forum_id, thread_id):
43 try:
44 c.thread = meta.Session.query(forum_model.Thread) \
45 .filter_by(id=thread_id, forum_id=forum_id).one()
46 except NoResultFound:
47 abort(404)
48
49 c.write_post_form = WritePostForm()
50
51 return render('/forum/posts.mako')
52
53
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'):
57 abort(403)
58
59 try:
60 c.forum = meta.Session.query(forum_model.Forum) \
61 .filter_by(id=forum_id).one()
62 except NoResultFound:
63 abort(404)
64
65 c.write_thread_form = WriteThreadForm(request.params)
66
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')
70
71
72 # Otherwise, add the post.
73 c.forum = meta.Session.query(forum_model.Forum) \
74 .with_lockmode('update') \
75 .get(c.forum.id)
76
77 thread = forum_model.Thread(
78 forum_id = c.forum.id,
79 subject = c.write_thread_form.subject.data,
80 post_count = 1,
81 )
82 post = forum_model.Post(
83 position = 1,
84 author_user_id = c.user.id,
85 content = c.write_thread_form.content.data,
86 )
87
88 thread.posts.append(post)
89 c.forum.threads.append(thread)
90
91 meta.Session.commit()
92
93 # Redirect to the new thread
94 h.flash("Contribution to the collective knowledge of the species successfully recorded.")
95 redirect_to(controller='forum', action='posts',
96 forum_id=forum_id, thread_id=thread.id,
97 _code=303)
98
99 def write(self, forum_id, thread_id):
100 """Provides a form for posting to a thread."""
101 if not c.user.can('forum:create-post'):
102 abort(403)
103
104 try:
105 c.thread = meta.Session.query(forum_model.Thread) \
106 .filter_by(id=thread_id, forum_id=forum_id).one()
107 except NoResultFound:
108 abort(404)
109
110 c.write_post_form = WritePostForm(request.params)
111
112 if request.method != 'POST' or not c.write_post_form.validate():
113 # Failure or initial request; show the form
114 return render('/forum/write.mako')
115
116
117 # Otherwise, add the post.
118 c.thread = meta.Session.query(forum_model.Thread) \
119 .with_lockmode('update') \
120 .get(c.thread.id)
121
122 post = forum_model.Post(
123 position = c.thread.post_count + 1,
124 author_user_id = c.user.id,
125 content = c.write_post_form.content.data,
126 )
127
128 c.thread.posts.append(post)
129 c.thread.post_count += 1
130
131 meta.Session.commit()
132
133 # Redirect to the thread
134 # XXX probably to the post instead; anchor? depends on paging scheme
135 h.flash('Your uniqueness has been added to our own.')
136 redirect_to(controller='forum', action='posts',
137 forum_id=forum_id, thread_id=thread_id,
138 _code=303)