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

authorDenis Laxalde <denis.laxalde@logilab.fr>
changeset34b3b2e532c0
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 34b3b2e532c0a8410a0a7b73cf54785b34789962
# 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,22 @@
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 +            root = entity.cw_adapt_to('ITree').root()
47 +            if root.eid == entity.eid:
48 +                break
49 +            else:
50 +                entity = root
51 +        entity.set_attributes(modification_date=datetime.now())
diff --git a/test/unittest_hooks.py b/test/unittest_hooks.py
@@ -22,8 +22,41 @@
52  url: http://testing.fr/cubicweb/blogentry/%s''' %
53                            c.comments[0].eid)
54          self.assertEqual(v.subject(), 'new comment for BlogEntry une news !')
55 
56 
57 +class SetModificationDateTC(CubicWebTC):
58 +
59 +    def setup_database(self):
60 +        self.blog = self.request().create_entity(
61 +            'BlogEntry', title=u"une news !", content=u"cubicweb c'est beau")
62 +
63 +    def test_depth1(self):
64 +        req = self.request()
65 +        md = self.blog.modification_date
66 +        c = req.create_entity('Comment', content=u"Yo",
67 +                              comments=self.blog.eid)
68 +        self.commit()
69 +        self.blog.cw_clear_all_caches()
70 +        self.assertGreater(self.blog.modification_date, md)
71 +
72 +    def test_depth2(self):
73 +        """Check subcomments
74 +        Also check that intermediate comments are not modified"""
75 +        req = self.request()
76 +        c1 = req.create_entity('Comment', content=u"Yo",
77 +                               comments=self.blog.eid)
78 +        self.commit()
79 +        bmd = self.blog.modification_date
80 +        cmd = c1.modification_date
81 +        c2 = req.create_entity('Comment', content=u"plouf",
82 +                               comments=c1.eid)
83 +        self.commit()
84 +        self.blog.cw_clear_all_caches()
85 +        c1.cw_clear_all_caches()
86 +        self.assertGreater(self.blog.modification_date, bmd)
87 +        self.assertEqual(c1.modification_date, cmd)
88 +
89 +
90  if __name__ == '__main__':
91      from logilab.common.testlib import unittest_main
92      unittest_main()