Newer
Older
"""entity classes for Comment entities
:organization: Logilab
:copyright: 2003-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
"""
__docformat__ = "restructuredtext en"
from logilab.common.textutils import normalize_text
from cubicweb.view import EntityView
from cubicweb.interfaces import ITree
from cubicweb.common.mixins import TreeMixIn
from cubicweb.entities import AnyEntity
class Comment(TreeMixIn, AnyEntity):
"""customized class for Comment entities"""
id = 'Comment'
tree_attribute = 'comments'
__implements__ = AnyEntity.__implements__ + (ITree,)
def dc_title(self):
return u'#%s' % self.eid
def dc_description(self, format='text/plain'):
return self.printable_value('content', format=format)
# some views potentially needed on web *and* server side (for notification)
# so put them here
class CommentFullTextView(EntityView):
id = 'fulltext'
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def cell_call(self, row, col, indentlevel=0, withauthor=True):
e = self.entity(row, col)
if indentlevel:
indentstr = '>'*indentlevel + ' '
else:
indentstr = ''
if withauthor:
_ = self.req._
author = e.created_by and e.created_by[0].login or _("Unknown author")
head = u'%s%s - %s :' % (indentstr,
_('On %s') % self.format_date(e.creation_date, time=True),
_('%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"""
id = 'fullthreadtext'
def cell_call(self, row, col):
e = self.entity(row, col)
strings = []
cpath = e.path()[1:]
indentlevel = len(cpath) - 1
for i, ceid in enumerate(cpath):
comment = self.req.execute('Any C,T,D WHERE C creation_date D, C content T, C eid %(x)s',
{'x': ceid}, 'x', build_descr=True).get_entity(0, 0)
strings.append(comment.view('fulltext', indentlevel=indentlevel-i,
withauthor=i!=indentlevel).strip() + '\n')
strings.append(u'\n%s: %s' % (self.req._('i18n_by_author_field'),
e.dc_creator() or _('unknown author')))
strings.append(u'url: %s' % e.root().absolute_url())
self.w(u'\n'.join(strings))
class CommentFullThreadDescText(CommentFullTextView):
"""same as fullthreadtext, but going from top level object to leaf comments
"""
id = 'fullthreadtext_descending'
def cell_call(self, row, col, indentlevel=0):
e = self.entity(row, col)
self.w(e.view('fulltext', indentlevel=indentlevel).strip() + '\n')
subcommentsrset = e.related('comments', 'object')
if subcommentsrset:
self.wview('fulltext', subcommentsrset, indentlevel=indentlevel+1)