# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1276182144 -7200
#      Thu Jun 10 17:02:24 2010 +0200
# Node ID 68cdbd97a6d36520638af92e9044e6cb5b4c5546
# Parent  c6e6d5adc287a3996fa5fd47e5bc88ba29c6257d
update to 3.9 api by using an ITree adapter

diff --git a/entities.py b/entities.py
--- a/entities.py
+++ b/entities.py
@@ -9,21 +9,18 @@
 from logilab.common.textutils import normalize_text
 
 from cubicweb.view import EntityView
-from cubicweb.interfaces import ITree
-from cubicweb.mixins import TreeMixIn
 from cubicweb.selectors import implements
 from cubicweb.entities import AnyEntity, fetch_config
+from cubicweb.entities.adapters import ITreeAdapter
 
 
 def subcomments_count(commentable):
     return sum([len(commentable.reverse_comments)]
                + [subcomments_count(c) for c in commentable.reverse_comments])
 
-class Comment(TreeMixIn, AnyEntity):
+class Comment(AnyEntity):
     """customized class for Comment entities"""
     __regid__ = 'Comment'
-    tree_attribute = 'comments'
-    __implements__ = AnyEntity.__implements__ + (ITree,)
     fetch_attrs, fetch_order = fetch_config(('creation_date',),
                                              'creation_date', order='ASC')
 
@@ -33,13 +30,12 @@
     def dc_description(self, format='text/plain'):
         return self.printable_value('content', format=format)
 
-    def after_deletion_path(self):
-        """return (path, parameters) which should be used as redirect
-        information when this entity is being deleted
-        """
-        return self.root().rest_path(), {}
+    subcomments_count = subcomments_count
+
 
-    subcomments_count = subcomments_count
+class CommentITreeAdapter(ITreeAdapter):
+    __select__ = implements('Comment')
+    tree_relation = 'comments'
 
 
 # some views potentially needed on web *and* server side (for notification)
@@ -78,7 +74,8 @@
     def cell_call(self, row, col):
         e = self.cw_rset.get_entity(row,col)
         strings = []
-        cpath = e.path()[1:]
+        itree = e.cw_adapt_to('ITree')
+        cpath = itree.path()[1:]
         indentlevel = len(cpath) - 1
         for i, ceid in enumerate(cpath):
             comment = self._cw.execute('Any C,T,D WHERE C creation_date D, C content T, C eid %(x)s',
@@ -87,7 +84,7 @@
                                         withauthor=i!=indentlevel).strip() + '\n')
         strings.append(u'\n%s: %s' % (self._cw._('i18n_by_author_field'),
                                       e.dc_creator() or _('unknown author')))
-        strings.append(u'url: %s' % e.root().absolute_url())
+        strings.append(u'url: %s' % itree.root().absolute_url())
         self.w(u'\n'.join(strings))
 
 
diff --git a/hooks.py b/hooks.py
--- a/hooks.py
+++ b/hooks.py
@@ -1,7 +1,7 @@
 """Comment notification hooks
 
 :organization: Logilab
-:copyright: 2003-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+:copyright: 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
 """
 __docformat__ = "restructuredtext en"
@@ -18,7 +18,7 @@
     msgid_timestamp = False
 
     def subject(self):
-        root = self.cw_rset.get_entity(self.cw_row, self.cw_col).root()
+        root = self.cw_rset.get_entity(self.cw_row, self.cw_col).cw_adapt_to('ITree').root()
         return '%s %s %s' % (self._cw._('new comment for'),
                              root.dc_type(),
                              root.dc_title())
diff --git a/test/unittest_comment.py b/test/unittest_comment.py
--- a/test/unittest_comment.py
+++ b/test/unittest_comment.py
@@ -83,11 +83,13 @@
     def test_path(self):
         req = self.request()
         c1 = req.create_entity('Comment', content=u"oijzr", comments=self.b)
+        itreec1 = c1.cw_adapt_to('ITree')
         c11 = req.create_entity('Comment', content=u"duh?", comments=c1)
-        self.assertEquals(c1.path(), [self.b.eid, c1.eid])
-        self.assertEquals(c1.root().eid, self.b.eid)
-        self.assertEquals(c11.path(), [self.b.eid, c1.eid, c11.eid])
-        self.assertEquals(c11.root().eid, self.b.eid)
+        itreec11 = c11.cw_adapt_to('ITree')
+        self.assertEquals(itreec1.path(), [self.b.eid, c1.eid])
+        self.assertEquals(itreec1.root().eid, self.b.eid)
+        self.assertEquals(itreec11.path(), [self.b.eid, c1.eid, c11.eid])
+        self.assertEquals(itreec11.root().eid, self.b.eid)
 
     def test_comments_ascending_order(self):
         req = self.request()
@@ -97,7 +99,7 @@
         c2 = req.create_entity('Comment', content=u"two", comments=self.b)
         self.assertEquals([c.eid for c in self.b.reverse_comments],
                           [c1.eid, c2.eid])
-        self.assertEquals([c.eid for c in c1.children()],
+        self.assertEquals([c.eid for c in c1.cw_adapt_to('ITree').children()],
                           [c11.eid, c12.eid])
 
     def test_subcomments_count(self):
diff --git a/data/external_resources b/uiprops.py
rename from data/external_resources
rename to uiprops.py
--- a/data/external_resources
+++ b/uiprops.py
@@ -1,1 +1,1 @@
-COMMENT_ICON = DATADIR/icon_comment.gif
+COMMENT_ICON = data('icon_comment.gif')
diff --git a/views.py b/views.py
--- a/views.py
+++ b/views.py
@@ -4,6 +4,8 @@
 :copyright: 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
 """
+from __future__ import with_statement
+
 __docformat__ = "restructuredtext en"
 _ = unicode
 
@@ -23,7 +25,7 @@
 from cubicweb.mixins import TreeViewMixIn
 from cubicweb.web import stdmsgs, uicfg, component, form, formwidgets as fw
 from cubicweb.web.action import LinkToEntityAction, Action
-from cubicweb.web.views import primary, baseviews, xmlrss, basecontrollers
+from cubicweb.web.views import primary, baseviews, xmlrss, basecontrollers, treeview
 
 _afs = uicfg.autoform_section
 _afs.tag_subject_of(('*', 'comments', '*'), formtype='main', section='hidden')
@@ -80,7 +82,7 @@
 
     def cell_call(self, row, col, **kwargs):
         entity = self.cw_rset.get_entity(row, col)
-        root = entity.root()
+        root = entity.cw_adapt_to('ITree').root()
         self.w(u'<a href="%s">%s %s</a> ' % (
             xml_escape(root.absolute_url()),
             xml_escape(root.dc_type()),
@@ -131,18 +133,17 @@
         replyaction = actions.select_or_none('reply_comment', self._cw,
                                        rset=self.cw_rset, row=row)
         if replyaction is not None:
-            url = self._cw.build_ajax_replace_url(
-                'comment%sHolder' % entity.eid, rql_for_eid(entity.eid),
-                'addcommentform')
+            url = self._cw.ajax_replace_url('comment%sHolder' % entity.eid,
+                                            eid=entity.eid, vid='addcommentform')
             self.w(u' | <span class="replyto"><a href="%s">%s</a></span>'
                    % (xml_escape(url), self._cw._(replyaction.title)))
         editaction = actions.select_or_none('edit_comment', self._cw,
                                             rset=self.cw_rset, row=row)
         if editaction is not None:
             # split(':', 1)[1] to remove javascript:
-            formjs = self._cw.build_ajax_replace_url(
-                cdivid, rql_for_eid(entity.eid),
-                'editcommentform', 'append').split(':', 1)[1]
+            formjs = self._cw.ajax_replace_url(
+                cdivid, 'append', eid=entity.eid,
+                vid='editcommentform').split(':', 1)[1]
             url = "javascript: jQuery('#%s div').hide(); %s" % (cdivid, formjs)
             self.w(u' | <span class="replyto"><a href="%s">%s</a></span>'
                    % (xml_escape(url), self._cw._(editaction.title)))
@@ -150,9 +151,10 @@
         deleteaction = actions.select_or_none('delete_comment', self._cw,
                                              rset=self.cw_rset, row=row)
         if deleteaction is not None:
-            url = self._cw.build_ajax_replace_url(
-                'comment%s' % entity.eid, rql_for_eid(entity.eid),
-                'deleteconf', __redirectpath=entity.root().rest_path())
+            root = entity.cw_adapt_to('ITree').root()
+            url = self._cw.ajax_replace_url(
+                'comment%s' % entity.eid, eid=entity.eid, vid='deleteconf',
+                __redirectpath=root.rest_path())
 
             self.w(u' | <span class="replyto"><a href="%s">%s</a></span>'
                    % (xml_escape(url), self._cw._(deleteaction.title)))
@@ -164,7 +166,7 @@
         self.w(u'</div>\n') # close comment's content div
 
 
-class CommentThreadView(TreeViewMixIn, baseviews.ListView):
+class CommentThreadView(treeview.BaseTreeView):
     """a recursive tree view"""
     __select__ = implements('Comment')
     title = _('thread view')
@@ -182,10 +184,11 @@
         self.w(u'<guid isPermaLink="true">%s</guid>\n'
                % xml_escape(entity.absolute_url()))
         self.render_title_link(entity)
+        root = entity.cw_adapt_to('ITree').root()
         description = entity.dc_description(format='text/html') + \
                       self._cw._(u'about') + \
-                      u' <a href=%s>%s</a>' % (entity.root().absolute_url(),
-                                               entity.root().dc_title())
+                      u' <a href=%s>%s</a>' % (root.absolute_url(),
+                                               root.dc_title())
         self._marker('description', description)
         self._marker('dc:date', entity.dc_date(self.date_format))
         self.render_entity_creator(entity)
@@ -315,7 +318,7 @@
                 params['etype'] = entity.__regid__
             url = req.ajax_replace_url(
                 'comment%sHolder' % eid, vid='addcommentform', **params)
-            self.w(u' (<a href="%s">%s</a>)' % (url, req._(addcomment.title)))
+            self.w(u' (<a href="%s">%s</a>)' % (xml_escape(url), req._(addcomment.title)))
 
 
 class UserLatestCommentsSection(component.EntityVComponent):
@@ -342,6 +345,18 @@
                                  })
             self.w(u'</div>')
 
+# adapters #####################################################################
+
+from cubicweb.web.views.editcontroller import IEditControlAdapter
+
+class CommentIEditControlAdapter(IEditControlAdapter):
+    __select__ = implements('Comment')
+
+    def after_deletion_path(self):
+        """return (path, parameters) which should be used as redirect
+        information when this entity is being deleted
+        """
+        return self.entity.cw_adapt_to('ITree').root().rest_path(), {}
 
 # actions ######################################################################
 
@@ -360,9 +375,10 @@
     def url(self):
         comment = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
         linkto = '%s:%s:subject' % (self.rtype, comment.eid)
+        root = comment.cw_adapt_to('ITree').root()
         return self._cw.build_url(vid='creation', etype=self.target_etype,
                                   __linkto=linkto,
-                                  __redirectpath=comment.root().rest_path(),
+                                  __redirectpath=root.rest_path(),
                                   __redirectvid=self._cw.form.get('vid', ''))
 
 
@@ -396,7 +412,7 @@
 class DeleteCommentAction(Action):
     __regid__ = 'delete_comment'
     __select__ = implements('Comment') & authenticated_user() & \
-                 score_entity(lambda x: not x.reverse_comments and x.has_perm('delete'))
+                 score_entity(lambda x: not x.reverse_comments and x.cw_has_perm('delete'))
 
     title = _('delete comment')
     category = 'hidden'