4 from pylons
import config
, request
, response
, session
, tmpl_context
as c
5 from pylons
.controllers
.util
import abort
, redirect
, redirect_to
6 from sqlalchemy
import and_
8 from floof
.lib
.base
import BaseController
, render
9 from floof
.model
import Art
10 from floof
.model
.comments
import Comment
12 log
= logging
.getLogger(__name__
)
14 def find_owner(owner_url
):
15 """Returns whatever thing owns a group of comments."""
17 # Need to prepend a slash to make this an absolute URL
18 route
= config
['routes.map'].match('/' + owner_url
)
20 if route
['action'] not in ('show', 'view'):
23 if route
['controller'] == 'art':
28 owner
= model
.query
.get(route
['id'])
35 class CommentsController(BaseController
):
37 def thread(self
, owner_url
, id=None):
38 """View a thread of comments, either attached to an item or starting
39 from a parent comment belonging to that item.
41 owner_object
= find_owner(owner_url
)
42 c
.owner_url
= owner_url
43 c
.root_comment_id
= id
46 # Get a thread starting from a certain point
47 c
.root_comment
= Comment
.query
.get(id)
48 if c
.root_comment
.discussion
!= owner_object
.discussion
:
51 c
.comments
= Comment
.query
.filter(and_(
52 Comment
.discussion_id
== owner_object
.discussion_id
,
53 Comment
.left
> c
.root_comment
.left
,
54 Comment
.right
< c
.root_comment
.right
59 c
.comments
= owner_object
.discussion
.comments
61 return render('/comments/thread.mako')
63 def reply(self
, owner_url
, id=None):
64 """Reply to a comment or discussion."""
66 c
.parent_comment_id
= id
68 c
.parent_comment
= Comment
.query
.get(id)
70 c
.parent_comment
= None
72 return render('/comments/reply.mako')
74 def reply_done(self
, owner_url
, id=None):
75 """Finish replying to a comment or discussion."""
76 # XXX form validation woo
78 owner_object
= find_owner(owner_url
)
81 parent_comment
= Comment
.query
.get(id)
85 new_comment
= Comment(
86 text
=request
.params
['text'],
88 discussion
=owner_object
.discussion
,
89 parent
=parent_comment
,
91 elixir
.session
.commit()
93 return redirect('/' + owner_url
, code
=301)