Browse Source

Only update the reads tracker if a new post has been submitted

sh4nks 11 years ago
parent
commit
261f15c138
2 changed files with 22 additions and 6 deletions
  1. 10 3
      flaskbb/forum/models.py
  2. 12 3
      flaskbb/forum/views.py

+ 10 - 3
flaskbb/forum/models.py

@@ -256,14 +256,12 @@ class Topic(db.Model):
         db.session.commit()
         return self
 
-    def update_read(self, user, forum=None):
+    def update_read(self, user, forum, forumsread=None):
         """
         Update the topics read status if the user hasn't read the latest
         post.
         """
 
-        # TODO: check if a forum has been cleared
-
         read_cutoff = datetime.utcnow() - timedelta(
             days=current_app.config['TRACKER_LENGTH'])
 
@@ -277,6 +275,12 @@ class Topic(db.Model):
             filter(TopicsRead.user_id == user.id,
                    TopicsRead.topic_id == self.id).first()
 
+        # Can be None if the user has never marked the forum as read. If this
+        # condition is false - we need to update the tracker
+        if forumsread and forumsread.cleared is not None and \
+                forumsread.cleared >= self.last_post.date_created:
+            return
+
         # A new post has been submitted that the user hasn't read.
         # Updating...
         if topicread and (topicread.last_read < self.last_post.date_created):
@@ -289,6 +293,7 @@ class Topic(db.Model):
             topicread = TopicsRead()
             topicread.user_id = user.id
             topicread.topic_id = self.id
+            topicread.forum_id = self.forum_id
             topicread.last_read = datetime.utcnow()
             topicread.save()
 
@@ -450,6 +455,8 @@ class TopicsRead(db.Model):
                         primary_key=True)
     topic_id = db.Column(db.Integer, db.ForeignKey("topics.id"),
                          primary_key=True)
+    forum_id = db.Column(db.Integer, db.ForeignKey("forums.id"),
+                         primary_key=True)
     last_read = db.Column(db.DateTime, default=datetime.utcnow())
 
     def save(self):

+ 12 - 3
flaskbb/forum/views.py

@@ -101,13 +101,14 @@ def view_forum(forum_id):
             paginate(page, current_app.config['TOPICS_PER_PAGE'], True)
     else:
         forum = Forum.query.filter(Forum.id == forum_id).first_or_404()
+        forum = (forum, None)
 
-        subforums = Forum.query.filter(Forum.parent_id == forum.id).all()
+        subforums = Forum.query.filter(Forum.parent_id == forum[0].id).all()
         # This isn't really nice imho, but "add_entity" (see above)
         # makes a list with tuples
         subforums = [(item, None) for item in subforums]
 
-        topics = Topic.query.filter_by(forum_id=forum.id).\
+        topics = Topic.query.filter_by(forum_id=forum[0].id).\
             filter(Post.topic_id == Topic.id).\
             order_by(Post.id.desc()).\
             paginate(page, current_app.config['TOPICS_PER_PAGE'], True, True)
@@ -128,6 +129,8 @@ def markread(forum_id=None):
     if forum_id:
         forum = Forum.query.filter_by(id=forum_id).first_or_404()
         forumsread = ForumsRead.query.filter_by(forum_id=forum.id).first()
+        TopicsRead.query.filter_by(user_id=current_user.id,
+                                   forum_id=forum.id).delete()
 
         if not forumsread:
             forumsread = ForumsRead()
@@ -174,7 +177,13 @@ def view_topic(topic_id):
     topic.views += 1
 
     # Update the topicsread status if the user hasn't read it
-    topic.update_read(current_user, topic.forum)
+    forumsread = None
+    if current_user.is_authenticated():
+        forumsread = ForumsRead.query.\
+            filter_by(user_id=current_user.id,
+                      forum_id=topic.forum.id).first()
+
+    topic.update_read(current_user, topic.forum, forumsread)
     topic.save()
 
     form = None