# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1254813206 -7200
#      Tue Oct 06 09:13:26 2009 +0200
# Node ID 89b12aa77a7953813498603e1b68643ba4bb4e82
# Parent  f2324563557174a2c38b29a5ab53a8a3f423ba7d
refactor and cleanup

diff --git a/views.py b/views.py
--- a/views.py
+++ b/views.py
@@ -36,12 +36,14 @@
 uicfg.primaryview_section.tag_subject_of(('*', 'comments', '*'),  'hidden')
 uicfg.primaryview_section.tag_object_of(('*', 'comments', '*'), 'hidden')
 
-def _login_register_link(appobj):
+def _login_register_link(req):
     link = u''
-    if appobj.req.cnx.anonymous_connection:
-        if 'registration' in appobj.vreg.config.cubes():
-            link += u' <a href="/register">%s</a> or' % appobj.req._(u'register')
-        link += u' <a href="/login">%s</a> %s' % (appobj.req._(u'login'), appobj.req._(u'to'))
+    if req.cnx.anonymous_connection:
+        if 'registration' in req.vreg.config.cubes():
+            link += u' <a href="%s">%s</a> or' % (req.build_url('register'),
+                                                  req._(u'register')
+        link += u' <a href="%s">%s</a>' % (req.build_url('login'),
+                                           req._(u'login'))
     return link
 
 # comment views ###############################################################
@@ -102,46 +104,43 @@
     __select__ = implements('Comment')
 
     def cell_call(self, row, col, **kwargs):
-        _ = self.req._
         self.req.add_js('cubicweb.ajax.js')
         self.req.add_css('cubes.comment.css')
         entity = self.entity(row, col)
         actions = self.vreg['actions']
-        action = actions.select_object('reply_comment', self.req,
-                                       rset=self.rset, row=row)
-        editaction = actions.select_object('edit_comment', self.req,
-                                           rset=self.rset, row=row)
-        if action is None:
-            reply = u''
-        else:
-            url = self.req.build_ajax_replace_url(
-                'comment%sHolder' % entity.eid, rql_for_eid(entity.eid),
-                'inlinecomment')
-            reply = ' | %s <a href="%s"> %s </a>' % ( _login_register_link(self),
-                                                      xml_escape(url),
-                                                      _(action.title))
-        if editaction is None:
-            edit = u''
-        else:
-            url = self.req.build_ajax_replace_url(
-                'comment%s' % entity.eid, rql_for_eid(entity.eid),
-                'editcomment')
-            edit = ' | <a href="%s">%s</a>' % (xml_escape(url),
-                                               _(editaction.title))
-        text = entity.printable_value('content')
-        if not kwargs.get('full'):
-            maxsize = self.req.property_value('navigation.short-line-size')
-            text = safe_cut(text, maxsize)
         self.w(u'<div class="commentInfo">')
         self.w(self.format_date(entity.creation_date))
         self.w(u' %s' % self.format_time(entity.creation_date))
         if entity.creator:
             authorlink = entity.creator.view('oneline')
-            self.w(u', %s <span class="author">%s</span> \n' % (_('written by'),
-                                                                authorlink,))
-        self.w(u'<span class="replyto"> %s </span>' % reply)
-        self.w(u'<span class="replyto"> %s </span>' % edit)
+            self.w(u', %s <span class="author">%s</span> \n'
+                   % (self.req._('written by'), authorlink,))
+        replyaction = actions.select_object('reply_comment', self.req,
+                                       rset=self.rset, row=row)
+        if replyaction is not None:
+            url = self.req.build_ajax_replace_url(
+                'comment%sHolder' % entity.eid, rql_for_eid(entity.eid),
+                'inlinecomment')
+            if req.cnx.anonymous_connection:
+                self.w(u' | <span class="replyto">%s <a href="%s">%s</a></span>'
+                       % (_login_register_link(self.req),
+                          xml_escape(url), self.req._(replyaction.title))
+            else:
+                self.w(u' | <span class="replyto"><a href="%s">%s</a></span>'
+                       % (xml_escape(url), self.req._(replyaction.title))
+        editaction = actions.select_object('edit_comment', self.req,
+                                           rset=self.rset, row=row)
+        if editaction is not None:
+            url = self.req.build_ajax_replace_url(
+                'comment%s' % entity.eid, rql_for_eid(entity.eid),
+                'editcomment')
+            self.w(u' | <span class="replyto"><a href="%s">%s</a></span>'
+                   % (xml_escape(url), self.req._(editaction.title))
         self.w(u'</div>\n')
+        text = entity.printable_value('content')
+        if not kwargs.get('full'):
+            maxsize = self.req.property_value('navigation.short-line-size')
+            text = safe_cut(text, maxsize)
         self.w(u'<div class="commentBody">%s</div>\n' % text)
         self.w(u'<div id="comment%sHolder"></div>' % entity.eid)
 
@@ -226,33 +225,30 @@
         eid = self.rset[row][col]
         self.w(u'<div id="%s" class="%s" cubicweb:rooteid="%s">' % (
             self.div_id(), self.div_class(), eid))
-        action = self.vreg['actions'].select_object('reply_comment', req,
-                                                    rset=self.rset, row=0)
-        if action is not None:
-            url = self.req.build_ajax_replace_url(
-                'comment%sHolder' % eid, rql_for_eid(eid), 'inlinecomment')
-            reply = u' (<a href="%s">%s</a>)' % (url, req._(action.title))
-            if req.use_fckeditor() and req.property_value('ui.default-text-format') == 'text/html':
-                req.fckeditor_config()
-        else:
-            reply = u''
         rql = u'Any C,CD,CC,CCF,U,UL,US,UF ORDERBY CD WHERE C is Comment, '\
               'C comments X, C creation_date CD, C content CC, C content_format CCF, ' \
               'C created_by U?, U login UL, U firstname UF, U surname US, X eid %(x)s'
         rset = req.execute(rql, {'x': eid}, 'x')
         if rset.rowcount:
-            self.w(u'<h4>%s</h4>%s' % (req._('Comment_plural'), reply))
-            self.w(u'%s %s' % (_login_register_link(self), self.req._(u'comment')))
-
-            self.w(u'<div id="comment%sHolder"></div>' % eid)
+            self.w(u'<h4>%s</h4>' % (req._('Comment_plural')))
+        addcomment = self.vreg['actions'].select_object('reply_comment', req,
+                                                        rset=self.rset,
+                                                        row=row, col=col)
+        if addcomment is not None:
+            url = req.build_ajax_replace_url(
+                'comment%sHolder' % eid, rql_for_eid(eid), 'inlinecomment')
+            self.w(u' (<a href="%s">%s</a>)' % (url, req._(addcomment.title)))
+            # XXX still necessary?
+            #if req.use_fckeditor() and req.property_value('ui.default-text-format') == 'text/html':
+            #    req.fckeditor_config()
+        if req.cnx.anonymous_connection:
+            self.w(u'%s' % (_login_register_link(req), req._(u'to comment')))
+        self.w(u'<div id="comment%sHolder"></div>' % eid)
+        if rset.rowcount:
             self.w(u'<ul class="comment">')
             for i in xrange(rset.rowcount):
                 self.wview('tree', rset, row=i, full=True)
             self.w(u'</ul>')
-        elif reply:
-            self.w(reply)
-            self.w(u'%s %s' % (_login_register_link(self), self.req._(u'to comment')))
-            self.w(u'<div id="comment%sHolder"></div>' % eid)
         self.w(u'</div>')
 
 
@@ -271,8 +267,7 @@
     order = 111
 
     def url(self):
-        rset = self.rset
-        comment = rset.get_entity(self.row or 0, self.col or 0)
+        comment = self.rset.get_entity(self.row or 0, self.col or 0)
         linkto = '%s:%s:%s' % (self.rtype, comment.eid, self.target)
         return self.build_url(vid='creation', etype=self.etype,
                               __linkto=linkto,