Browse Source

Added methods to recalculate the post and topic counts

sh4nks 9 years ago
parent
commit
cae902afd1
2 changed files with 35 additions and 0 deletions
  1. 28 0
      flaskbb/forum/models.py
  2. 7 0
      flaskbb/user/models.py

+ 28 - 0
flaskbb/forum/models.py

@@ -416,6 +416,13 @@ class Topic(db.Model, CRUDMixin):
 
         return updated
 
+    def recalculate(self):
+        """Recalculates the post count in the topic."""
+        post_count = Post.query.filter_by(topic_id=self.id).count()
+        self.post_count = post_count
+        self.save()
+        return self
+
     def move(self, new_forum):
         """Moves a topic to the given forum.
         Returns True if it could successfully move the topic to forum.
@@ -710,6 +717,27 @@ class Forum(db.Model, CRUDMixin):
         # topicsread
         return False
 
+    def recalculate(self, last_post=False):
+        """Recalculates the post_count and topic_count in the forum.
+        Returns the forum with the recounted stats.
+
+        :param last_post: If set to ``True`` it will also try to update
+                          the last post columns in the forum.
+        """
+        topic_count = Topic.query.filter_by(forum_id=self.id).count()
+        post_count = Post.query.\
+            filter(Post.topic_id == Topic.id,
+                   Topic.forum_id == self.id).\
+            count()
+        self.topic_count = topic_count
+        self.post_count = post_count
+
+        if last_post:
+            self.update_last_post()
+
+        self.save()
+        return self
+
     def save(self, groups=None):
         """Saves a forum
 

+ 7 - 0
flaskbb/user/models.py

@@ -250,6 +250,13 @@ class User(db.Model, UserMixin, CRUDMixin):
             data = False
         return expired, invalid, data
 
+    def recalculate(self):
+        """Recalculates the post count from the user."""
+        post_count = Post.query.filter_by(user_id=self.id).count()
+        self.post_count = post_count
+        self.save()
+        return self
+
     def all_topics(self, page):
         """Returns a paginated result with all topics the user has created."""