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

Merge pull request #7 from RJacksonm1/master

Fixed is_unread not working on forums. (issue #6)
sh4nks 11 лет назад
Родитель
Сommit
686123f66d
3 измененных файлов с 22 добавлено и 16 удалено
  1. 20 14
      flaskbb/forum/models.py
  2. 1 1
      flaskbb/forum/views.py
  3. 1 1
      flaskbb/templates/forum/index.html

+ 20 - 14
flaskbb/forum/models.py

@@ -211,7 +211,6 @@ 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()
 
@@ -365,24 +364,33 @@ class Forum(db.Model):
         breadcrumbs.reverse()
         return breadcrumbs
 
-    def is_unread(self, user):
+    def is_unread(self, user, include_children=True):
         """
-        Returns True if the user has read the topic
+        Returns True if the user hasn't read the topic
         """
         if not user.is_authenticated():
             return True
 
-        # make a count, and if the count is > 0, there are still unread topics
-        # in the forum
-        topicsread = TopicsRead.query.\
-            filter(TopicsRead.forum_id == self.id). \
-            filter(TopicsRead.last_read < self.last_post.date_created).\
-            count()
+        if include_children:
+            topicread = TopicsRead.query.\
+                filter(TopicsRead.user_id == user.id). \
+                filter(TopicsRead.topic_id == Topic.id). \
+                filter(Topic.forum_id.in_(get_forum_ids(self))). \
+                order_by(TopicsRead.last_read.desc()). \
+                first()
+        else:
+            topicread = TopicsRead.query.\
+                filter(TopicsRead.user_id == user.id). \
+                filter(TopicsRead.topic_id == Topic.id). \
+                filter(Topic.forum_id == self.id). \
+                order_by(TopicsRead.last_read.desc()). \
+                first()
 
-        # If no entry is found, return true
-        if not topicsread:
+        # If no entry is found, the user hasn't read the topic
+        if not topicread:
             return True
-        if topicsread > 0:
+        # If the entry is older than the last post, the user hasn't read it
+        if topicread.last_read < self.last_post.date_created:
             return True
         return False
 
@@ -481,8 +489,6 @@ class TopicsRead(db.Model):
     user_id = db.Column(db.Integer, db.ForeignKey("users.id"), 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 __repr__(self):

+ 1 - 1
flaskbb/forum/views.py

@@ -41,7 +41,7 @@ def index():
                            user_count=user_count,
                            topic_count=topic_count,
                            post_count=post_count,
-                           newest_user=newest_user.username,
+                           newest_user=newest_user,
                            online_users=len(get_online_users()),
                            online_guests=len(get_online_users(guest=True)))
 

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

@@ -26,7 +26,7 @@
                 Total number of posts: <strong>{{ post_count }}</strong> <br />
             </td>
             <td>
-                Newest registered user: <a href="{{ url_for('user.profile', username=newest_user) }}">{{ newest_user }}</a> <br />
+                Newest registered user: {% if newest_user %}<a href="{{ url_for('user.profile', username=newest_user.username) }}">{{ newest_user.username }}</a>{% else %}No users{% endif %}<br />
                 Registered users online: <strong>{{ online_users }}</strong> <br />
                 Guests online: <strong>{{ online_guests }}</strong> <br />
             </td>