Update modification_date of root entity when commented (closes #2961579)

authorDenis Laxalde <denis.laxalde@logilab.fr>
changeset5d98ba3d4fb1
branchstable
phasedraft
hiddenyes
parent revision#6f1a425a2c11 Added tag cubicweb-comment-version-1.9.0, cubicweb-comment-debian-version-1.9.0-1 for changeset 0871c8f86573
child revision<not specified>
files modified by this revision
README
hooks.py
test/unittest_hooks.py
# HG changeset patch
# User Denis Laxalde <denis.laxalde@logilab.fr>
# Date 1372258387 -7200
# Wed Jun 26 16:53:07 2013 +0200
# Branch stable
# Node ID 5d98ba3d4fb180a3ad220369f1ec1b1f81a5870a
# Parent 6f1a425a2c11f43b9c10cddc34fb04df3855ce18
Update modification_date of root entity when commented (closes #2961579)

diff --git a/README b/README
@@ -52,10 +52,13 @@
1          cardinality = '1*'
2 
3  Once this relation is defined, you can post comments and view threadable
4  comments automatically on blog entry's primary view.
5 
6 +This cube also ships with a hook which updates the ``modification_date``
7 +attribute of a commentable entity upon comment.
8 +
9  Documentation
10  -------------
11 
12  Look in the ``doc/`` subdirectory or read
13  http://www.cubicweb.org/doc/en/
diff --git a/hooks.py b/hooks.py
@@ -4,13 +4,16 @@
14  :copyright: 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
15  :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
16  """
17  __docformat__ = "restructuredtext en"
18 
19 +from datetime import datetime
20 +
21  from cubicweb import RegistryException
22  from cubicweb.predicates import is_instance
23  from cubicweb.sobjects import notification
24 +from cubicweb.server import hook
25 
26 
27  class CommentAddedView(notification.NotificationView):
28      """get notified from new comments"""
29      __regid__ = 'notif_after_add_relation_comments'
@@ -29,5 +32,18 @@
30              view = self._cw.vreg['views'].select('fullthreadtext', self._cw,
31                                               rset=self.cw_rset, row=row, col=col)
32          except RegistryException:
33              return
34          return view.render(row=row, col=col, w=self.w, **kwargs)
35 +
36 +
37 +class SetModificationDateAfterAddComment(hook.Hook):
38 +    """update all parent entities modification date after adding a comment"""
39 +    __regid__ = 'comment.set_modification_date_after_comment'
40 +    events = ('after_add_relation',)
41 +    __select__ = hook.Hook.__select__ & hook.match_rtype('comments',)
42 +
43 +    def __call__(self):
44 +        entity = self._cw.entity_from_eid(self.eidto)
45 +        while entity.e_schema == 'Comment':
46 +            entity = entity.cw_adapt_to('ITree').root()
47 +        entity.set_attributes(modification_date=datetime.now())
diff --git a/test/unittest_hooks.py b/test/unittest_hooks.py
@@ -22,8 +22,41 @@
48  url: http://testing.fr/cubicweb/blogentry/%s''' %
49                            c.comments[0].eid)
50          self.assertEqual(v.subject(), 'new comment for BlogEntry une news !')
51 
52 
53 +class SetModificationDateTC(CubicWebTC):
54 +
55 +    def setup_database(self):
56 +        self.blog = self.request().create_entity(
57 +            'BlogEntry', title=u"une news !", content=u"cubicweb c'est beau")
58 +
59 +    def test_depth1(self):
60 +        req = self.request()
61 +        md = self.blog.modification_date
62 +        c = req.create_entity('Comment', content=u"Yo",
63 +                              comments=self.blog.eid)
64 +        self.commit()
65 +        self.blog.cw_clear_all_caches()
66 +        self.assertGreater(self.blog.modification_date, md)
67 +
68 +    def test_depth2(self):
69 +        """Check subcomments
70 +        Also check that intermediate comments are not modified"""
71 +        req = self.request()
72 +        c1 = req.create_entity('Comment', content=u"Yo",
73 +                               comments=self.blog.eid)
74 +        self.commit()
75 +        bmd = self.blog.modification_date
76 +        cmd = c1.modification_date
77 +        c2 = req.create_entity('Comment', content=u"plouf",
78 +                               comments=c1.eid)
79 +        self.commit()
80 +        self.blog.cw_clear_all_caches()
81 +        c1.cw_clear_all_caches()
82 +        self.assertGreater(self.blog.modification_date, bmd)
83 +        self.assertEqual(c1.modification_date, cmd)
84 +
85 +
86  if __name__ == '__main__':
87      from logilab.common.testlib import unittest_main
88      unittest_main()