allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hei...@apache.org
Subject [09/50] allura git commit: [#8020] ticket:866 Add Thread.update_stats() to functions which change Post status
Date Wed, 27 Jan 2016 20:22:23 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/31396ee6
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/31396ee6
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/31396ee6

Branch: refs/heads/hs/8035
Commit: 31396ee6b96e18e5c4f0bc918d561b64f7ea32b9
Parents: 2121b5c
Author: Denis Kotov <deniskkotov@gmail.com>
Authored: Wed Jan 20 18:03:07 2016 +0200
Committer: Dave Brondsema <dave@brondsema.net>
Committed: Wed Jan 27 12:08:16 2016 -0500

----------------------------------------------------------------------
 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/31396ee6/Allura/allura/controllers/discuss.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/discuss.py b/Allura/allura/controllers/discuss.py
index e28a780..9cb5f37 100644
--- a/Allura/allura/controllers/discuss.py
+++ b/Allura/allura/controllers/discuss.py
@@ -356,14 +356,12 @@ 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=self.post.author())
                 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/31396ee6/Allura/allura/model/discuss.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/discuss.py b/Allura/allura/model/discuss.py
index df753e8..e3e6527 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):
@@ -701,7 +702,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':
@@ -769,8 +770,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=self.author())
+        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/31396ee6/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