Commits

murarth  committed 7cc10a0

Fixed double increment/decrement of num_comments and num_replies fields for approval/removal/deletion of comments

  • Participants
  • Parent commits a84839c

Comments (0)

Files changed (1)

File reddish/model/__init__.py

             return User.get(self.removed)
 
     def get_comments(self, user = None):
+        # TODO: Varying types of comment sorting
         return Comment.comment_tree(user,
             Comment.find(post = self._id)
-                .sort([('total_value', -1), ('created', -1)]).limit(500))
+                .sort([('total_value', -1), ('created', 1)]).limit(500))
 
     def get_community(self):
         return Community.get(self.community)
                 tree.append(c)
                 tree_d[c._id] = (replies, {})
 
+        roots.sort(key = lambda cm: (cm.total_value, cm.created), reverse = True)
+
         return roots
 
     @classmethod
         except exc.NotFound:
             raise ClientError('You cannot delete this comment')
 
-        self.set_value()
-        Session.update(Post, { '_id': self.post },
-            { '$inc': { 'num_comments': -1 } })
-
-        if self.parents:
-            Session.update(Comment, { '_id': { '$in': self.parents } },
-                { '$inc': { 'num_replies': -1 } }, multi = True)
+        if not (self.removed or self.hidden):
+            self.set_value()
+            Session.update(Post, { '_id': self.post },
+                { '$inc': { 'num_comments': -1 } })
+
+            if self.parents:
+                Session.update(Comment, { '_id': { '$in': self.parents } },
+                    { '$inc': { 'num_replies': -1 } }, multi = True)
 
         if is_admin:
             AdminLog.log(user, 'delete',
                 { '_id': self._id, 'approved': None, 'removed': self.NON_NULL },
                 { '$unset': { 'removed': 1 },
                     '$set': { 'approved': mod._id } })
-
+        except exc.NotFound:
+            return
+
+        if not (self.deleted or self.hidden):
             Session.update(Post, { '_id': self.post },
                 { '$inc': { 'num_comments': 1 } })
-        except exc.NotFound:
-            return
+
+            if self.parents:
+                Session.update(Comment, { '_id': { '$in': self.parents } },
+                    { '$inc': { 'num_replies': 1 } }, multi = True)
 
         self.get_community().log(mod, 'approve', comment = self._id)
 
             Session.find_and_modify(Comment,
                 { '_id': self._id, 'removed': None },
                 { '$set': { 'removed': mod._id }, '$unset': { 'approved': 1 } })
-
+        except exc.NotFound:
+            return
+
+        if not (self.deleted or self.hidden):
+            self.set_value()
             Session.update(Post, { '_id': self.post },
                 { '$inc': { 'num_comments': -1 } })
-        except exc.NotFound:
-            return
-
-        self.set_value()
+
+            if self.parents:
+                Session.update(Comment, { '_id': { '$in': self.parents } },
+                    { '$inc': { 'num_replies': -1 } }, multi = True)
+
         self.get_community().log(mod, 'remove', comment = self._id)
 
     def report(self, user, report):