allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jetm...@apache.org
Subject [09/10] allura git commit: [#8020] ticket:866 Add Thread.update_stats() to functions which change Post status
Date Thu, 21 Jan 2016 15:29:48 GMT
[#8020] ticket:866 Add Thread.update_stats() to functions which change Post status


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/684e6a24
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/684e6a24
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/684e6a24

Branch: refs/heads/ib/8020b
Commit: 684e6a24633cdd0b171c01a51c5ff16d9c964d94
Parents: 3a94bea
Author: Denis Kotov <deniskkotov@gmail.com>
Authored: Wed Jan 20 18:03:07 2016 +0200
Committer: Denis Kotov <deniskkotov@gmail.com>
Committed: Wed Jan 20 18:03:07 2016 +0200

----------------------------------------------------------------------
 Allura/allura/controllers/discuss.py         |  4 +--
 Allura/allura/model/discuss.py               | 16 +++++++++---
 Allura/allura/tests/model/test_discussion.py | 30 ++++++++++++++++++-----
 3 files changed, 37 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/684e6a24/Allura/allura/controllers/discuss.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/discuss.py b/Allura/allura/controllers/discuss.py
index 41a8cf1..b9cb92a 100644
--- a/Allura/allura/controllers/discuss.py
+++ b/Allura/allura/controllers/discuss.py
@@ -356,15 +356,13 @@ class PostController(BaseController):
             self.post.spam()
         elif kw.pop('undo', None):
             prev_status = kw.pop('prev_status', None)
-            if prev_status:
-                self.post.status = prev_status
+            self.post.undo(prev_status)
         elif kw.pop('approve', None):
             if self.post.status != 'ok':
                 self.post.approve()
                 g.spam_checker.submit_ham(
                     self.post.text, artifact=self.post, user=c.user)
                 self.post.thread.post_to_feed(self.post)
-        self.thread.update_stats()
         return dict(result='success')
 
     @h.vardec

http://git-wip-us.apache.org/repos/asf/allura/blob/684e6a24/Allura/allura/model/discuss.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/discuss.py b/Allura/allura/model/discuss.py
index 96096da..b999b18 100644
--- a/Allura/allura/model/discuss.py
+++ b/Allura/allura/model/discuss.py
@@ -265,7 +265,8 @@ class Thread(Artifact, ActivityObject):
         """Helper function to avoid code duplication."""
         p = self.post(**kw)
         p.commit(update_stats=False)
-        self.num_replies += 1
+        session(self).flush(self)
+        self.update_stats()
         if not self.first_post:
             self.first_post_id = p._id
         self.post_to_feed(p)
@@ -343,7 +344,7 @@ class Thread(Artifact, ActivityObject):
 
     def update_stats(self):
         self.num_replies = self.post_class().query.find(
-            dict(thread_id=self._id, status='ok', deleted=False)).count() - 1
+            dict(thread_id=self._id, status='ok', deleted=False)).count()
 
     @property
     def last_post(self):
@@ -683,7 +684,7 @@ class Post(Message, VersionedArtifact, ActivityObject):
     def delete(self):
         self.deleted = True
         session(self).flush(self)
-        self.thread.num_replies = max(0, self.thread.num_replies - 1)
+        self.thread.update_stats()
 
     def approve(self, file_info=None, notify=True, notification_text=None):
         if self.status == 'ok':
@@ -751,8 +752,15 @@ class Post(Message, VersionedArtifact, ActivityObject):
 
     def spam(self):
         self.status = 'spam'
-        self.thread.num_replies = max(0, self.thread.num_replies - 1)
         g.spam_checker.submit_spam(self.text, artifact=self, user=c.user)
+        session(self).flush(self)
+        self.thread.update_stats()
+
+    def undo(self, prev_status):
+        if prev_status in ('ok', 'pending'):
+            self.status = prev_status
+            session(self).flush(self)
+            self.thread.update_stats()
 
 
 class DiscussionAttachment(BaseAttachment):

http://git-wip-us.apache.org/repos/asf/allura/blob/684e6a24/Allura/allura/tests/model/test_discussion.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/model/test_discussion.py b/Allura/allura/tests/model/test_discussion.py
index c94f2a6..7b15c1d 100644
--- a/Allura/allura/tests/model/test_discussion.py
+++ b/Allura/allura/tests/model/test_discussion.py
@@ -114,7 +114,8 @@ def test_thread_methods():
     assert '_id' in jsn
     assert_equals(len(jsn['posts']), 3)
     (p.approve() for p in (p0, p1))
-    assert t.num_replies == 2
+    ThreadLocalORMSession.flush_all()
+    assert t.num_replies == 3
     t.spam()
     assert t.num_replies == 0
     ThreadLocalORMSession.flush_all()
@@ -165,13 +166,14 @@ def test_post_methods():
     assert jsn["thread_id"] == t._id
 
     (p.approve() for p in (p, p2))
-    assert t.num_replies == 1
-    p2.spam()
-    assert t.num_replies == 0
+    ThreadLocalORMSession.flush_all()
+    assert t.num_replies == 2
     p.spam()
-    assert t.num_replies == 0
+    assert t.num_replies == 1
+    p.undo('ok')
+    assert t.num_replies == 2
     p.delete()
-    assert t.num_replies == 0
+    assert t.num_replies == 1
 
 
 @with_setup(setUp, tearDown)
@@ -325,6 +327,21 @@ def test_post_delete():
 
 
 @with_setup(setUp, tearDown)
+def test_post_undo():
+    d = M.Discussion(shortname='test', name='test')
+    t = M.Thread.new(discussion_id=d._id, subject='Test Thread')
+    p = t.post('This is a post')
+    t.post('This is a post2')
+    t.post('This is a post3')
+    ThreadLocalORMSession.flush_all()
+    assert t.num_replies == 3
+    p.spam()
+    assert t.num_replies == 2
+    p.undo('ok')
+    assert t.num_replies == 3
+
+
+@with_setup(setUp, tearDown)
 def test_post_permission_check():
     d = M.Discussion(shortname='test', name='test')
     t = M.Thread.new(discussion_id=d._id, subject='Test Thread')
@@ -519,6 +536,7 @@ def test_post_count():
 def test_spam_num_replies(spam_checker):
     d = M.Discussion(shortname='test', name='test')
     t = M.Thread(discussion_id=d._id, subject='Test Thread', num_replies=2)
+    M.Post(discussion_id=d._id, thread_id=t._id, status='ok')
     p1 = M.Post(discussion_id=d._id, thread_id=t._id, status='spam')
     p1.spam()
     assert_equal(t.num_replies, 1)


Mime
View raw message