03aee3ddf74acd5003d2d4dbdb7c3cb88d18d03a
[zzz-floof.git] / floof / controllers / comments.py
1 import logging
2
3 import elixir
4 from pylons import config, request, response, session, tmpl_context as c
5 from pylons.controllers.util import abort, redirect, redirect_to
6
7 from floof.lib.base import BaseController, render
8 from floof.model.art import Art
9 from floof.model.comments import Comment
10
11 log = logging.getLogger(__name__)
12
13 def find_owner(owner_url):
14 """Returns whatever thing owns a group of comments."""
15
16 # Need to prepend a slash to make this an absolute URL
17 route = config['routes.map'].match('/' + owner_url)
18
19 if route['action'] not in ('show', 'view'):
20 abort(404)
21
22 if route['controller'] == 'art':
23 model = Art
24 else:
25 abort(404)
26
27 owner = model.query.get(route['id'])
28 if not owner:
29 abort(404)
30
31 return owner
32
33
34 class CommentsController(BaseController):
35
36 def thread(self, owner_url):
37 """View a thread of comments, either attached to an item or starting
38 from a parent comment belonging to that item.
39 """
40 owner_object = find_owner(owner_url)
41 c.comments = owner_object.discussion.comments
42 return render('/comments/thread.mako')
43
44 def reply(self, owner_url):
45 """Reply to a comment or discussion."""
46 return render('/comments/reply.mako')
47
48 def reply_done(self, owner_url):
49 """Finish replying to a comment or discussion."""
50 # XXX form validation woo
51
52 new_comment = Comment(
53 text=request.params['text'],
54 user=c.user,
55 )
56
57 owner_object = find_owner(owner_url)
58 discussion = owner_object.discussion
59 discussion.comments.append(new_comment)
60 elixir.session.commit()
61
62 return redirect('/' + owner_url, code=301)