Browse Source

Merging two topics together. Fixes #18

sh4nks 11 years ago
parent
commit
59a9323455

+ 25 - 0
flaskbb/forum/models.py

@@ -326,6 +326,31 @@ class Topic(db.Model):
 
 
         return True
         return True
 
 
+    def merge(self, topic):
+        """Merges a topic with another topic together
+
+        :param topic: The new topic for the posts in this topic
+        """
+
+        # You can only merge a topic with a differrent topic in the same forum
+        if self.id == topic.id or not self.forum_id == topic.forum_id:
+            return False
+
+        # Update the topic id
+        Post.query.filter_by(topic_id=self.id).\
+            update({Post.topic_id: topic.id})
+
+        # Increase the post and views count
+        topic.post_count += self.post_count
+        topic.views += self.views
+
+        topic.save()
+
+        # Finally delete the old topic
+        Topic.query.filter_by(id=self.id).delete()
+
+        return True
+
     def save(self, user=None, forum=None, post=None):
     def save(self, user=None, forum=None, post=None):
         """Saves a topic and returns the topic object. If no parameters are
         """Saves a topic and returns the topic object. If no parameters are
         given, it will only update the topic.
         given, it will only update the topic.

+ 15 - 0
flaskbb/forum/views.py

@@ -293,6 +293,21 @@ def move_topic(topic_id, forum_id, topic_slug=None, forum_slug=None):
     return redirect(topic.url)
     return redirect(topic.url)
 
 
 
 
+@forum.route("/topic/<int:old_id>/merge/<int:new_id>")
+@forum.route("/topic/<int:old_id>-<old_slug>/merge/<int:new_id>-<new_slug>")
+@login_required
+def merge_topic(old_id, new_id, old_slug=None, new_slug=None):
+    old_topic = Topic.query.filter_by(id=old_id).first_or_404()
+    new_topic = Topic.query.filter_by(id=new_id).first_or_404()
+
+    if not old_topic.merge(new_topic):
+        flash("Could not merge the topic.", "danger")
+        return redirect(old_topic.url)
+
+    flash("Topic succesfully merged.", "success")
+    return redirect(new_topic.url)
+
+
 @forum.route("/topic/<int:topic_id>/post/new", methods=["POST", "GET"])
 @forum.route("/topic/<int:topic_id>/post/new", methods=["POST", "GET"])
 @forum.route("/topic/<int:topic_id>-<slug>/post/new", methods=["POST", "GET"])
 @forum.route("/topic/<int:topic_id>-<slug>/post/new", methods=["POST", "GET"])
 @login_required
 @login_required

+ 3 - 2
flaskbb/user/models.py

@@ -44,8 +44,9 @@ class Group(db.Model):
     editpost = db.Column(db.Boolean, default=True, nullable=False)
     editpost = db.Column(db.Boolean, default=True, nullable=False)
     deletepost = db.Column(db.Boolean, default=False, nullable=False)
     deletepost = db.Column(db.Boolean, default=False, nullable=False)
     deletetopic = db.Column(db.Boolean, default=False, nullable=False)
     deletetopic = db.Column(db.Boolean, default=False, nullable=False)
-    locktopic = db.Column(db.Boolean, default=True, nullable=False)
-    movetopic = db.Column(db.Boolean, default=True, nullable=False)
+    locktopic = db.Column(db.Boolean, default=False, nullable=False)
+    movetopic = db.Column(db.Boolean, default=False, nullable=False)
+    mergetopic = db.Column(db.Boolean, default=False, nullable=False)
     posttopic = db.Column(db.Boolean, default=True, nullable=False)
     posttopic = db.Column(db.Boolean, default=True, nullable=False)
     postreply = db.Column(db.Boolean, default=True, nullable=False)
     postreply = db.Column(db.Boolean, default=True, nullable=False)
 
 

+ 6 - 0
flaskbb/utils/permissions.py

@@ -79,6 +79,12 @@ def can_move_topic(user, forum):
     return check_perm(user=user, perm='movetopic', forum=forum)
     return check_perm(user=user, perm='movetopic', forum=forum)
 
 
 
 
+def can_merge_topic(user, forum):
+    """Check if the user is allowed to merge a topic in the forum"""
+
+    return check_perm(user=user, perm='mergetopic', forum=forum)
+
+
 def can_post_reply(user, forum):
 def can_post_reply(user, forum):
     """Check if the user is allowed to post in the forum"""
     """Check if the user is allowed to post in the forum"""
 
 

+ 6 - 0
flaskbb/utils/populate.py

@@ -30,6 +30,7 @@ GROUPS = OrderedDict((
         'postreply': True,
         'postreply': True,
         'locktopic': True,
         'locktopic': True,
         'movetopic': True,
         'movetopic': True,
+        'mergetopic': True,
         'viewtopic': True,
         'viewtopic': True,
         'viewprofile': True
         'viewprofile': True
     }),
     }),
@@ -47,6 +48,7 @@ GROUPS = OrderedDict((
         'postreply': True,
         'postreply': True,
         'locktopic': True,
         'locktopic': True,
         'movetopic': True,
         'movetopic': True,
+        'mergetopic': True,
         'viewtopic': True,
         'viewtopic': True,
         'viewprofiles': True
         'viewprofiles': True
     }),
     }),
@@ -64,6 +66,7 @@ GROUPS = OrderedDict((
         'postreply': True,
         'postreply': True,
         'locktopic': True,
         'locktopic': True,
         'movetopic': True,
         'movetopic': True,
+        'mergetopic': True,
         'viewtopic': True,
         'viewtopic': True,
         'viewprofile': True
         'viewprofile': True
     }),
     }),
@@ -81,6 +84,7 @@ GROUPS = OrderedDict((
         'postreply': True,
         'postreply': True,
         'locktopic': False,
         'locktopic': False,
         'movetopic': False,
         'movetopic': False,
+        'mergetopic': False,
         'viewtopic': True,
         'viewtopic': True,
         'viewprofile': True
         'viewprofile': True
     }),
     }),
@@ -98,6 +102,7 @@ GROUPS = OrderedDict((
         'postreply': False,
         'postreply': False,
         'locktopic': False,
         'locktopic': False,
         'movetopic': False,
         'movetopic': False,
+        'mergetopic': False,
         'viewtopic': False,
         'viewtopic': False,
         'viewprofile': False
         'viewprofile': False
     }),
     }),
@@ -115,6 +120,7 @@ GROUPS = OrderedDict((
         'postreply': False,
         'postreply': False,
         'locktopic': False,
         'locktopic': False,
         'movetopic': False,
         'movetopic': False,
+        'mergetopic': False,
         'viewtopic': False,
         'viewtopic': False,
         'viewprofile': False
         'viewprofile': False
     })
     })