Browse Source

Fixed a bug when deleting the second last post in a forum
Also added a check if the forum isn't either a category or has a parent

sh4nks 11 years ago
parent
commit
315708e1d6
2 changed files with 19 additions and 14 deletions
  1. 6 1
      flaskbb/admin/forms.py
  2. 13 13
      flaskbb/forum/models.py

+ 6 - 1
flaskbb/admin/forms.py

@@ -252,6 +252,10 @@ class ForumForm(Form):
         if hasattr(field.data, "id"):
             if field.data.id == self._id:
                 raise ValidationError("A forum cannot be it's own parent!")
+        else:
+            if not self.is_category.data:
+                raise ValidationError("Please choose a parent or is it a \
+                                      category?")
 
     def validate_moderators(self, field):
         if field.data:
@@ -275,7 +279,7 @@ class ForumForm(Form):
                       description=self.description.data,
                       position=self.position.data)
 
-        if self.moderators.data:
+        if self.moderators.data and not self.is_category.data:
             forum.moderators = self.moderators.data
 
         if self.is_category.data:
@@ -283,5 +287,6 @@ class ForumForm(Form):
             forum.parent_id = None
         else:
             forum.parent_id = self.parent.data.id
+            forum.parents.add(self.parent.data.id)
 
         return forum.save()

+ 13 - 13
flaskbb/forum/models.py

@@ -65,6 +65,9 @@ class Post(db.Model):
 
             # Update the parent forums
             parent = topic.forum.parent
+            # TODO: Improvement - store the parent forums in a list and then
+            # get all the forums with one query instead of firing up
+            # for each parent a own query
             while parent is not None and not parent.is_category:
                 parent.last_post_id = self.id
                 parent.post_count += 1
@@ -138,9 +141,8 @@ class Topic(db.Model):
                                  foreign_keys=[first_post_id])
 
     # One-to-one
-    last_post_id = db.Column(db.Integer, db.ForeignKey("posts.id",
-                                                       ondelete="CASCADE",
-                                                       onupdate="CASCADE"))
+    last_post_id = db.Column(db.Integer, db.ForeignKey("posts.id"))
+
     last_post = db.relationship("Post", backref="last_post", uselist=False,
                                 foreign_keys=[last_post_id])
 
@@ -214,14 +216,13 @@ class Topic(db.Model):
         # check if the topic is the most recently one in this forum
         try:
             forum = self.forum
+            # you want to delete the topic with the last post
             if self.id == topic[0].id:
                 # Now the second last post will be the last post
                 while forum is not None and not forum.is_category:
                     forum.last_post_id = topic[1].last_post_id
                     forum.save()
                     forum = forum.parent
-            else:
-                forum.last_post_id = topic[1].last_post_id
         # Catch an IndexError when you delete the last topic in the forum
         except IndexError:
             while forum is not None and not forum.is_category:
@@ -245,12 +246,8 @@ class Topic(db.Model):
                 db.session.commit()
 
         while forum is not None and not forum.is_category:
-            forum.topic_count = Topic.query.filter_by(
-                forum_id=self.forum_id).count()
-
-            forum.post_count = Post.query.filter(
-                Post.topic_id == Topic.id,
-                Topic.forum_id == self.forum_id).count()
+            forum.topic_count -= 1
+            forum.post_count -= 1
 
             forum = forum.parent
 
@@ -328,6 +325,11 @@ class Forum(db.Model):
     post_count = db.Column(db.Integer, default=0)
     topic_count = db.Column(db.Integer, default=0)
 
+    moderators = db.Column(DenormalizedText)
+
+    # A set with all parent forums
+    parents = db.Column(DenormalizedText)
+
     # One-to-one
     last_post_id = db.Column(db.Integer, db.ForeignKey("posts.id"))
     last_post = db.relationship("Post", backref="last_post_forum",
@@ -340,8 +342,6 @@ class Forum(db.Model):
                                backref=db.backref("parent", remote_side=[id]),
                                cascade="all, delete-orphan")
 
-    moderators = db.Column(DenormalizedText)
-
     # Methods
     def __repr__(self):
         """