Просмотр исходного кода

We can now move a topic in another forum.

sh4nks 11 лет назад
Родитель
Сommit
aec4a67e21
3 измененных файлов с 112 добавлено и 53 удалено
  1. 1 1
      README.md
  2. 51 0
      flaskbb/forum/models.py
  3. 60 52
      flaskbb/forum/views.py

+ 1 - 1
README.md

@@ -19,7 +19,7 @@ using the micro framework Flask.
 
 * Searching for members, posts,...
 * "Link to"-Forum type
-* Move a topic in a other forum
+* ~~Move a topic in a other forum~~
 * Merging 2 topics together
 * Reporting posts
 * Userstyles (e.q.: colored username)

+ 51 - 0
flaskbb/forum/models.py

@@ -233,6 +233,34 @@ class Topic(db.Model):
         """
         return "<{} {}>".format(self.__class__.__name__, self.id)
 
+    def move(self, forum):
+        """Moves a topic to the given forum.
+        Returns True if it could successfully move the topic to forum.
+
+        :param forum: The new forum for the topic
+        """
+
+        # if the target forum is the current forum, abort
+        if self.forum_id == forum.id:
+            return False
+
+        old_forum = self.forum
+        self.forum.post_count -= self.post_count
+        self.forum.topic_count -= 1
+        self.forum_id = forum.id
+
+        forum.post_count += self.post_count
+        forum.topic_count += 1
+
+        db.session.commit()
+
+        forum.update_last_post()
+        old_forum.update_last_post()
+
+        TopicsRead.query.filter_by(topic_id=self.id).delete()
+
+        return True
+
     def save(self, user=None, forum=None, post=None):
         """Saves a topic and returns the topic object. If no parameters are
         given, it will only update the topic.
@@ -443,6 +471,29 @@ class Forum(db.Model):
         """
         return "<{} {}>".format(self.__class__.__name__, self.id)
 
+    def update_last_post(self):
+        """Updates the last post. This is useful if you move a topic
+        in another forum
+        """
+        last_post = Post.query.\
+            filter(Post.topic_id == Topic.id,
+                   Topic.forum_id == self.id).\
+            order_by(Post.date_created.desc()).\
+            first()
+
+        # Last post is none when there are no topics in the forum
+        if last_post is not None:
+
+            # a new last post was found in the forum
+            if not last_post.id == self.last_post_id:
+                self.last_post_id = last_post.id
+
+        # No post found..
+        else:
+            self.last_post_id = 0
+
+        db.session.commit()
+
     def save(self, moderators=None):
         """Saves a forum"""
         if moderators is not None:

+ 60 - 52
flaskbb/forum/views.py

@@ -150,55 +150,6 @@ def view_forum(forum_id):
     return render_template("forum/forum.html", forum=forum, topics=topics)
 
 
-@forum.route("/markread")
-@forum.route("/<int:forum_id>/markread")
-def markread(forum_id=None):
-
-    if not current_user.is_authenticated():
-        flash("You need to be logged in for that feature.", "danger")
-        return redirect(url_for("forum.index"))
-
-    # Mark a single forum as read
-    if forum_id:
-        forum = Forum.query.filter_by(id=forum_id).first_or_404()
-        forumsread = ForumsRead.query.filter_by(user_id=current_user.id,
-                                                forum_id=forum.id).first()
-        TopicsRead.query.filter_by(user_id=current_user.id,
-                                   forum_id=forum.id).delete()
-
-        if not forumsread:
-            forumsread = ForumsRead()
-            forumsread.user_id = current_user.id
-            forumsread.forum_id = forum.id
-
-        forumsread.last_read = datetime.datetime.utcnow()
-        forumsread.cleared = datetime.datetime.utcnow()
-
-        db.session.add(forumsread)
-        db.session.commit()
-
-        return redirect(url_for("forum.view_forum", forum_id=forum.id))
-
-    # Mark all forums as read
-    ForumsRead.query.filter_by(user_id=current_user.id).delete()
-    TopicsRead.query.filter_by(user_id=current_user.id).delete()
-
-    forums = Forum.query.all()
-    forumsread_list = []
-    for forum in forums:
-        forumsread = ForumsRead()
-        forumsread.user_id = current_user.id
-        forumsread.forum_id = forum.id
-        forumsread.last_read = datetime.datetime.utcnow()
-        forumsread.cleared = datetime.datetime.utcnow()
-        forumsread_list.append(forumsread)
-
-    db.session.add_all(forumsread_list)
-    db.session.commit()
-
-    return redirect(url_for("forum.index"))
-
-
 @forum.route("/topic/<int:topic_id>", methods=["POST", "GET"])
 def view_topic(topic_id):
     page = request.args.get('page', 1, type=int)
@@ -320,10 +271,18 @@ def unlock_topic(topic_id):
     return redirect(url_for("forum.view_topic", topic_id=topic.id))
 
 
-@forum.route("/topic/<int:topic_id>/move")
+@forum.route("/topic/<int:topic_id>/move/<int:forum_id>")
 @login_required
-def move_topic(topic_id):
-    pass
+def move_topic(topic_id, forum_id):
+    forum = Forum.query.filter_by(id=forum_id).first_or_404()
+    topic = Topic.query.filter_by(id=topic_id).first_or_404()
+
+    if not topic.move(forum):
+        flash("Could not move the topic to forum %s" % forum.title, "danger")
+        return redirect(url_for("forum.view_topic", topic_id=topic.id))
+
+    flash("Topic was moved to forum %s" % forum.title, "success")
+    return redirect(url_for("forum.view_topic", topic_id=topic.id))
 
 
 @forum.route("/topic/<int:topic_id>/post/new", methods=["POST", "GET"])
@@ -407,6 +366,55 @@ def delete_post(post_id):
     return redirect(url_for('forum.view_topic', topic_id=topic_id))
 
 
+@forum.route("/markread")
+@forum.route("/<int:forum_id>/markread")
+def markread(forum_id=None):
+
+    if not current_user.is_authenticated():
+        flash("You need to be logged in for that feature.", "danger")
+        return redirect(url_for("forum.index"))
+
+    # Mark a single forum as read
+    if forum_id:
+        forum = Forum.query.filter_by(id=forum_id).first_or_404()
+        forumsread = ForumsRead.query.filter_by(user_id=current_user.id,
+                                                forum_id=forum.id).first()
+        TopicsRead.query.filter_by(user_id=current_user.id,
+                                   forum_id=forum.id).delete()
+
+        if not forumsread:
+            forumsread = ForumsRead()
+            forumsread.user_id = current_user.id
+            forumsread.forum_id = forum.id
+
+        forumsread.last_read = datetime.datetime.utcnow()
+        forumsread.cleared = datetime.datetime.utcnow()
+
+        db.session.add(forumsread)
+        db.session.commit()
+
+        return redirect(url_for("forum.view_forum", forum_id=forum.id))
+
+    # Mark all forums as read
+    ForumsRead.query.filter_by(user_id=current_user.id).delete()
+    TopicsRead.query.filter_by(user_id=current_user.id).delete()
+
+    forums = Forum.query.all()
+    forumsread_list = []
+    for forum in forums:
+        forumsread = ForumsRead()
+        forumsread.user_id = current_user.id
+        forumsread.forum_id = forum.id
+        forumsread.last_read = datetime.datetime.utcnow()
+        forumsread.cleared = datetime.datetime.utcnow()
+        forumsread_list.append(forumsread)
+
+    db.session.add_all(forumsread_list)
+    db.session.commit()
+
+    return redirect(url_for("forum.index"))
+
+
 @forum.route("/who_is_online")
 def who_is_online():
     if current_app.config['USE_REDIS']: