342e8e6daeada1ba1e31e4e761f678f4796052eb
[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 try:
33 c.forum = meta.Session.query(forum_model.Forum).get(forum_id)
34 except NoResultFound:
35 abort(404)
36
37 c.write_thread_form = WriteThreadForm()
38
39 c.threads = c.forum.threads
40
41 return render('/forum/threads.mako')
42
43 def posts(self, forum_id, thread_id):
44 try:
45 c.thread = meta.Session.query(forum_model.Thread) \
46 .filter_by(id=thread_id, forum_id=forum_id).one()
47 except NoResultFound:
48 abort(404)
49
50 c.write_post_form = WritePostForm()
51
52 return render('/forum/posts.mako')
53
54
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'):
58 abort(403)
59
60 try:
61 c.forum = meta.Session.query(forum_model.Forum) \
62 .filter_by(id=forum_id).one()
63 except NoResultFound:
64 abort(404)
65
66 c.write_thread_form = WriteThreadForm(request.params)
67
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')
71
72
73 # Otherwise, add the post.
74 c.forum = meta.Session.query(forum_model.Forum) \
75 .with_lockmode('update') \
76 .get(c.forum.id)
77
78 thread = forum_model.Thread(
79 forum_id = c.forum.id,
80 subject = c.write_thread_form.subject.data,
81 post_count = 1,
82 )
83 post = forum_model.Post(
84 position = 1,
85 author_user_id = c.user.id,
86 content = c.write_thread_form.content.data,
87 )
88
89 thread.posts.append(post)
90 c.forum.threads.append(thread)
91
92 meta.Session.commit()
93
94 # Redirect to the new thread
95 h.flash("Contribution to the collective knowledge of the species successfully recorded.")
96 redirect_to(controller='forum', action='posts',
97 forum_id=forum_id, thread_id=thread.id,
98 _code=303)
99
100 def write(self, forum_id, thread_id):
101 """Provides a form for posting to a thread."""
102 if not c.user.can('forum:create-post'):
103 abort(403)
104
105 try:
106 c.thread = meta.Session.query(forum_model.Thread) \
107 .filter_by(id=thread_id, forum_id=forum_id).one()
108 except NoResultFound:
109 abort(404)
110
111 c.write_post_form = WritePostForm(request.params)
112
113 if request.method != 'POST' or not c.write_post_form.validate():
114 # Failure or initial request; show the form
115 return render('/forum/write.mako')
116
117
118 # Otherwise, add the post.
119 c.thread = meta.Session.query(forum_model.Thread) \
120 .with_lockmode('update') \
121 .get(c.thread.id)
122
123 post = forum_model.Post(
124 position = c.thread.post_count + 1,
125 author_user_id = c.user.id,
126 content = c.write_post_form.content.data,
127 )
128
129 c.thread.posts.append(post)
130 c.thread.post_count += 1
131
132 meta.Session.commit()
133
134 # Redirect to the thread
135 # XXX probably to the post instead; anchor? depends on paging scheme
136 h.flash('Your uniqueness has been added to our own.')
137 redirect_to(controller='forum', action='posts',
138 forum_id=forum_id, thread_id=thread_id,
139 _code=303)