b15302cb159eb7999754c10b04a7b392ef87520a
[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 import spline.lib.markdown
14 from splinext.forum import model as forum_model
15
16 log = logging.getLogger(__name__)
17
18
19 class WritePostForm(wtforms.Form):
20 content = fields.TextAreaField('Content')
21
22 class WriteThreadForm(WritePostForm):
23 subject = fields.TextField('Subject')
24
25 class ForumController(BaseController):
26
27 def forums(self):
28 c.forums = meta.Session.query(forum_model.Forum) \
29 .order_by(forum_model.Forum.id.asc())
30 return render('/forum/forums.mako')
31
32 def threads(self, forum_id):
33 c.forum = meta.Session.query(forum_model.Forum).get(forum_id)
34 if not c.forum:
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(
97 url(controller='forum', action='posts',
98 forum_id=forum_id, thread_id=thread.id),
99 code=303,
100 )
101
102 def write(self, forum_id, thread_id):
103 """Provides a form for posting to a thread."""
104 if not c.user.can('forum:create-post'):
105 abort(403)
106
107 try:
108 c.thread = meta.Session.query(forum_model.Thread) \
109 .filter_by(id=thread_id, forum_id=forum_id).one()
110 except NoResultFound:
111 abort(404)
112
113 c.write_post_form = WritePostForm(request.params)
114
115 if request.method != 'POST' or not c.write_post_form.validate():
116 # Failure or initial request; show the form
117 return render('/forum/write.mako')
118
119
120 # Otherwise, add the post.
121 c.thread = meta.Session.query(forum_model.Thread) \
122 .with_lockmode('update') \
123 .get(c.thread.id)
124
125 source = c.write_post_form.content.data
126 post = forum_model.Post(
127 position = c.thread.post_count + 1,
128 author_user_id = c.user.id,
129 raw_content = source,
130 content = spline.lib.markdown.translate(source),
131 )
132
133 c.thread.posts.append(post)
134 c.thread.post_count += 1
135
136 meta.Session.commit()
137
138 # Redirect to the thread
139 # XXX probably to the post instead; anchor? depends on paging scheme
140 h.flash('Your uniqueness has been added to our own.')
141 redirect(
142 url(controller='forum', action='posts',
143 forum_id=forum_id, thread_id=thread_id),
144 code=303,
145 )