Skip to content
Snippets Groups Projects
entities.py 3.9 KiB
Newer Older
Nicolas Chauvat's avatar
Nicolas Chauvat committed
"""entity classes for Comment entities

:organization: Logilab
Sylvain Thénault's avatar
1.8
Sylvain Thénault committed
:copyright: 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
Nicolas Chauvat's avatar
Nicolas Chauvat committed
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
"""
__docformat__ = "restructuredtext en"

sylvain thenault's avatar
sylvain thenault committed
from logilab.common.textutils import normalize_text
sylvain thenault's avatar
sylvain thenault committed

from cubicweb.view import EntityView
from cubicweb.selectors import is_instance
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(AnyEntity):
Nicolas Chauvat's avatar
Nicolas Chauvat committed
    """customized class for Comment entities"""
    __regid__ = 'Comment'
    fetch_attrs, fetch_order = fetch_config(('creation_date',),
                                             'creation_date', order='ASC')
Nicolas Chauvat's avatar
Nicolas Chauvat committed

    def dc_title(self):
        return u'#%s' % self.eid

    def dc_description(self, format='text/plain'):
        return self.printable_value('content', format=format)

    subcomments_count = subcomments_count

class CommentITreeAdapter(ITreeAdapter):
    __select__ = is_instance('Comment')
    tree_relation = 'comments'
Nicolas Chauvat's avatar
Nicolas Chauvat committed

# some views potentially needed on web *and* server side (for notification)
# so put them here

class CommentFullTextView(EntityView):
    __regid__ = 'fulltext'
    __select__ = is_instance('Comment')
Nicolas Chauvat's avatar
Nicolas Chauvat committed

    def cell_call(self, row, col, indentlevel=0, withauthor=True):
Arthur Lutz's avatar
Arthur Lutz committed
        e = self.cw_rset.get_entity(row,col)
Nicolas Chauvat's avatar
Nicolas Chauvat committed
        if indentlevel:
            indentstr = '>'*indentlevel + ' '
        else:
            indentstr = ''
        if withauthor:
Arthur Lutz's avatar
Arthur Lutz committed
            _ = self._cw._
Nicolas Chauvat's avatar
Nicolas Chauvat committed
            author = e.created_by and e.created_by[0].login or _("Unknown author")
            head = u'%s%s - %s :' % (indentstr,
                                     _('On %s') %  self._cw.format_date(e.creation_date, time=True),
Nicolas Chauvat's avatar
Nicolas Chauvat committed
                                     _('%s wrote') % author)
            lines = [head]
        else:
            lines = []
        content = e.printable_value('content', format='text/plain')
        lines.append(normalize_text(content, 80, indentstr,
                                    rest=e.content_format=='text/rest'))
        lines.append(indentstr[:-2])
        self.w(u'\n'.join(lines))


class CommentFullThreadText(CommentFullTextView):
    """display a comment and its parents"""
    __regid__ = 'fullthreadtext'
Nicolas Chauvat's avatar
Nicolas Chauvat committed
    def cell_call(self, row, col):
Arthur Lutz's avatar
Arthur Lutz committed
        e = self.cw_rset.get_entity(row,col)
Nicolas Chauvat's avatar
Nicolas Chauvat committed
        strings = []
        itree = e.cw_adapt_to('ITree')
        cpath = itree.path()[1:]
Nicolas Chauvat's avatar
Nicolas Chauvat committed
        indentlevel = len(cpath) - 1
        for i, ceid in enumerate(cpath):
            try:
                comment = self._cw.execute('Any C,T,D WHERE C creation_date D, C content T, C eid %(x)s',
                                           {'x': ceid}, build_descr=True).get_entity(0, 0)
            except TypeResolverException:
                # not a comment
                break
Nicolas Chauvat's avatar
Nicolas Chauvat committed
            strings.append(comment.view('fulltext', indentlevel=indentlevel-i,
                                        withauthor=i!=indentlevel).strip() + '\n')
Arthur Lutz's avatar
Arthur Lutz committed
        strings.append(u'\n%s: %s' % (self._cw._('i18n_by_author_field'),
Nicolas Chauvat's avatar
Nicolas Chauvat committed
                                      e.dc_creator() or _('unknown author')))
        strings.append(u'url: %s' % itree.root().absolute_url())
Nicolas Chauvat's avatar
Nicolas Chauvat committed
        self.w(u'\n'.join(strings))


class CommentFullThreadDescText(CommentFullTextView):
    """same as fullthreadtext, but going from top level object to leaf comments
    """
    __regid__ = 'fullthreadtext_descending'
Nicolas Chauvat's avatar
Nicolas Chauvat committed
    def cell_call(self, row, col, indentlevel=0):
Arthur Lutz's avatar
Arthur Lutz committed
        e = self.cw_rset.get_entity(row,col)
Nicolas Chauvat's avatar
Nicolas Chauvat committed
        self.w(e.view('fulltext', indentlevel=indentlevel).strip() + '\n')
        subcommentsrset = e.related('comments', 'object')
        if subcommentsrset:
            self.wview('fulltext', subcommentsrset, indentlevel=indentlevel+1)