6cd4bfbff6f482e6e5cf7f4c68b387de44bc7278
[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
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(
96 url(controller='forum', action='posts',
97 forum_id=forum_id, thread_id=thread.id),
98 code=303,
99 )
100
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'):
104 abort(403)
105
106 try:
107 c.thread = meta.Session.query(forum_model.Thread) \
108 .filter_by(id=thread_id, forum_id=forum_id).one()
109 except NoResultFound:
110 abort(404)
111
112 c.write_post_form = WritePostForm(request.params)
113
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')
117
118
119 # Otherwise, add the post.
120 c.thread = meta.Session.query(forum_model.Thread) \
121 .with_lockmode('update') \
122 .get(c.thread.id)
123
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,
128 )
129
130 c.thread.posts.append(post)
131 c.thread.post_count += 1
132
133 meta.Session.commit()
134
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.')
138 redirect(
139 url(controller='forum', action='posts',
140 forum_id=forum_id, thread_id=thread_id),
141 code=303,
142 )