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

Merge pull request #253 from kwijybo/redirect_first_unread

Redirect user to first unread post of a topic
Peter Justin 8 лет назад
Родитель
Сommit
2f0f4e0b24
2 измененных файлов с 28 добавлено и 2 удалено
  1. 27 1
      flaskbb/forum/models.py
  2. 1 1
      flaskbb/templates/forum/forum.html

+ 27 - 1
flaskbb/forum/models.py

@@ -15,7 +15,7 @@ from sqlalchemy.orm import aliased
 
 from flaskbb.extensions import db
 from flaskbb.utils.helpers import (slugify, get_categories_and_forums,
-                                   get_forums, time_utcnow)
+                                   get_forums, time_utcnow, topic_is_unread)
 from flaskbb.utils.database import CRUDMixin, UTCDateTime
 from flaskbb.utils.settings import flaskbb_config
 
@@ -322,6 +322,32 @@ class Topic(db.Model, CRUDMixin):
         """Returns the slugified url for the topic."""
         return url_for("forum.view_topic", topic_id=self.id, slug=self.slug)
 
+    def first_unread(self, topicsread, user, forumsread=None):
+        """Returns the url to the first unread post if any else to the topic
+        itself.
+
+        :param topicsread: The topicsread object for the topic
+
+        :param user: The user who should be checked if he has read the last post
+                    in the topic
+
+        :param forumsread: The forumsread object in which the topic is. If you
+                        also want to check if the user has marked the forum as
+                        read, than you will also need to pass an forumsread
+                        object.
+        """
+
+        # If the topic is unread try to get the first unread post
+        if topic_is_unread(self, topicsread, user, forumsread):
+            # 
+            post = Post.query.filter(Post.topic_id == self.id).\
+                filter(Post.date_created > topicsread.last_read).\
+                order_by(Post.id.asc()).first()
+            if post is not None:
+                return post.url
+        
+        return self.url
+
     # Methods
     def __init__(self, title=None, user=None):
         """Creates a topic object with some initial values.

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

@@ -76,7 +76,7 @@
                         </div>
                         <div class="col-md-11 col-sm-10 col-xs-10">
                             <div class="topic-name">
-                                <a href="{{ topic.url }}">{{ topic.title }}</a>
+                                <a href="{{ topic.first_unread(topicread, current_user, forumsread) }}">{{ topic.title }}</a>
                                 <!-- Topic Pagination -->
                                 <span class="topic-pages">{{ topic_pages(topic, flaskbb_config["POSTS_PER_PAGE"]) }}</span>
                             </div>