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

Added a method to mark forums and topics as read
I still need to improve the performance on this

sh4nks 11 лет назад
Родитель
Сommit
5efdfcbc9e

+ 2 - 2
flaskbb/app.py

@@ -36,8 +36,8 @@ from flaskbb.utils.helpers import (format_date, time_since, crop_title,
 
 
 DEFAULT_BLUEPRINTS = (
-    (forum, ""),
-    (auth, ""),
+    (forum, "/forum"),
+    (auth, "/auth"),
     (user, "/user"),
     (admin, "/admin")
 )

+ 1 - 1
flaskbb/configs/default.py

@@ -90,7 +90,7 @@ class DefaultConfig(object):
     # The length of the topic title in characters on the index
     TITLE_LENGTH = 15
 
-    # This is really handy if do not have an redis instance like on windows
+    # This is really handy if you do not have an redis instance like on windows
     USE_REDIS = True
     REDIS_HOST = 'localhost'
     REDIS_PORT = 6379

+ 9 - 4
flaskbb/forum/models.py

@@ -8,7 +8,9 @@
     :copyright: (c) 2013 by the FlaskBB Team.
     :license: BSD, see LICENSE for more details.
 """
-from datetime import datetime
+from datetime import datetime, timedelta
+
+from flask import current_app
 
 from flaskbb.extensions import db
 from flaskbb.utils.types import SetType, MutableSet
@@ -260,7 +262,12 @@ class Topic(db.Model):
         post.
         """
         # Don't do anything if the user is a guest
-        if not user.is_authenticated():
+        # or the the last post is is too old - can be specified in the
+        # config via `TRACKER_LENGTH`
+        read_cutoff = datetime.utcnow() - timedelta(
+            days=current_app.config['TRACKER_LENGTH'])
+        if not user.is_authenticated() or \
+                read_cutoff > self.last_post.date_created:
             return
 
         topicread = TopicsRead.query.\
@@ -293,8 +300,6 @@ class Topic(db.Model):
                        db.or_(TopicsRead.last_read == None,
                               TopicsRead.last_read < Topic.last_updated)).\
                 count()
-                # Why doesn't this work with when you pass an last_post object?
-                # e.q.: TopicsRead.last_read < last_post.date_created
 
             # Mark it as read if no unread topics are found
             if unread_count == 0:

+ 26 - 2
flaskbb/forum/views.py

@@ -70,7 +70,7 @@ def index():
                            online_guests=online_guests)
 
 
-@forum.route("/forum/<int:forum_id>")
+@forum.route("/<int:forum_id>")
 def view_forum(forum_id):
     page = request.args.get('page', 1, type=int)
 
@@ -110,6 +110,30 @@ def view_forum(forum_id):
                            forum=forum, topics=topics, subforums=subforums)
 
 
+@forum.route("/markread")
+@forum.route("/<int:forum_id>/markread")
+def markread(forum_id=None):
+
+    # Mark a single forum as read
+    if forum_id:
+        forum = Forum.query.filter_by(id=forum_id).first()
+        for topic in forum.topics:
+            topic.update_read(current_user, forum)
+        return redirect(url_for("forum.view_forum", forum_id=forum.id))
+
+    # Mark all forums as read
+    # TODO: Improve performance
+    forums = Forum.query.all()
+    for forum in forums:
+        if forum.is_category:
+            continue
+
+        for topic in forum.topics:
+            topic.update_read(current_user, forum)
+
+    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)
@@ -155,7 +179,7 @@ def view_post(post_id):
                     "?page=%d#pid%s" % (page, post.id))
 
 
-@forum.route("/forum/<int:forum_id>/topic/new", methods=["POST", "GET"])
+@forum.route("/<int:forum_id>/topic/new", methods=["POST", "GET"])
 @login_required
 def new_topic(forum_id):
     forum = Forum.query.filter_by(id=forum_id).first()

+ 2 - 2
flaskbb/templates/forum/forum.html

@@ -23,7 +23,7 @@
 <div class="pull-right" style="padding-bottom: 10px">
     <!-- star tick pencil -->
     <div class="btn-group">
-        <a href="#" class="btn btn-default"><span class="fa fa-check"></span> Mark as Read</a>
+        <a href="{{ url_for('forum.markread', forum_id=forum.id) }}" class="btn btn-default"><span class="fa fa-check"></span> Mark as Read</a>
         {% if forum.locked %}
         <span class="btn btn-primary"><span class="fa fa-lock"></span> Locked</span>
         {% else %}
@@ -131,7 +131,7 @@
             {% if topic.locked %}
                 <span class="fa fa-locked" style="font-size: 2em"></span>
             {% else %}
-                {% if topicread|is_unread(topic.last_post) %}
+                {% if topicread|is_unread(topic.last_post, topic=topic) %}
                     <span class="fa fa-comment" style="font-size: 2em"></span>
                 {% else %}
                     <span class="fa fa-comment-o" style="font-size: 2em"></span>

+ 3 - 1
flaskbb/utils/helpers.py

@@ -18,7 +18,7 @@ from flaskbb.extensions import redis
 from flaskbb.forum.models import ForumsRead, TopicsRead
 
 
-def is_unread(read_object, last_post, forum=None):
+def is_unread(read_object, last_post, forum=None, topic=None):
     if not (isinstance(read_object, ForumsRead) or
             isinstance(read_object, TopicsRead) or not None):
         raise TypeError("Must be a ForumsRead or TopicsRead object")
@@ -28,6 +28,8 @@ def is_unread(read_object, last_post, forum=None):
 
     if forum and forum.topic_count == 0:
         return False
+    if topic and not read_object:
+        return read_cutoff > last_post.date_created
     if read_object is None:
         return True
     if read_cutoff < last_post.date_created: