Browse Source

Remove n+1 queries in view topic

Closes #408
Alec Nikolas Reiter 7 years ago
parent
commit
d5d4988525
2 changed files with 14 additions and 13 deletions
  1. 10 9
      flaskbb/forum/views.py
  2. 4 4
      flaskbb/templates/forum/topic.html

+ 10 - 9
flaskbb/forum/views.py

@@ -143,6 +143,16 @@ class ViewTopic(MethodView):
         topic.views += 1
         topic.views += 1
         topic.save()
         topic.save()
 
 
+
+        # Update the topicsread status if the user hasn't read it
+        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(real(current_user), topic.forum, forumsread)
+
         # fetch the posts in the topic
         # fetch the posts in the topic
         posts = Post.query.\
         posts = Post.query.\
             outerjoin(User, Post.user_id == User.id).\
             outerjoin(User, Post.user_id == User.id).\
@@ -155,15 +165,6 @@ class ViewTopic(MethodView):
         if len(posts.items) == 0:
         if len(posts.items) == 0:
             abort(404)
             abort(404)
 
 
-        # Update the topicsread status if the user hasn't read it
-        forumsread = None
-        if current_user.is_authenticated:
-            forumsread = ForumsRead.query.\
-                filter_by(user_id=real(current_user).id,
-                          forum_id=topic.forum.id).first()
-
-        topic.update_read(real(current_user), topic.forum, forumsread)
-
         return render_template(
         return render_template(
             'forum/topic.html', topic=topic, posts=posts, last_seen=time_diff(), form=self.form()
             'forum/topic.html', topic=topic, posts=posts, last_seen=time_diff(), form=self.form()
         )
         )

+ 4 - 4
flaskbb/templates/forum/topic.html

@@ -123,7 +123,7 @@
                             <!-- Edit Post -->
                             <!-- Edit Post -->
                             <a href="{{ url_for('forum.edit_post', post_id=post.id) }}" class="btn btn-icon icon-edit" data-toggle="tooltip" data-placement="top" title="Edit this post"></a>
                             <a href="{{ url_for('forum.edit_post', post_id=post.id) }}" class="btn btn-icon icon-edit" data-toggle="tooltip" data-placement="top" title="Edit this post"></a>
                             {% endif %}
                             {% endif %}
-                            {% if topic.first_post == post %}
+                            {% if topic.first_post_id == post.id %}
                                 {% if current_user|delete_topic(topic) %}
                                 {% if current_user|delete_topic(topic) %}
                                 <form class="inline-form" method="post" action="{{ url_for('forum.delete_topic', topic_id=topic.id, slug=topic.slug) }}">
                                 <form class="inline-form" method="post" action="{{ url_for('forum.delete_topic', topic_id=topic.id, slug=topic.slug) }}">
                                     <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
                                     <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
@@ -147,14 +147,14 @@
                             {% endif %}
                             {% endif %}
 
 
                             {% if current_user.permissions.get('makehidden') %}
                             {% if current_user.permissions.get('makehidden') %}
-                                {% if post.first_post %}
+                                {% if topic.first_post_id == post.id %}
                                     {% if topic.hidden %}
                                     {% if topic.hidden %}
-                                    <form class="inline-form" method="post" action="{{ url_for('forum.unhide_topic', topic_id=post.topic.id) }}">
+                                    <form class="inline-form" method="post" action="{{ url_for('forum.unhide_topic', topic_id=topic.id) }}">
                                         <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
                                         <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
                                         <button class="btn btn-icon fa fa-user" name="unhide" data-toggle="tooltip" data-placement="top" title="Unhide this topic"></button>
                                         <button class="btn btn-icon fa fa-user" name="unhide" data-toggle="tooltip" data-placement="top" title="Unhide this topic"></button>
                                     </form>
                                     </form>
                                     {% else %}
                                     {% else %}
-                                    <form class="inline-form" method="post" action="{{ url_for('forum.hide_topic', topic_id=post.topic.id) }}">
+                                    <form class="inline-form" method="post" action="{{ url_for('forum.hide_topic', topic_id=topic.id) }}">
                                         <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
                                         <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
                                         <button class="btn btn-icon fa fa-user-secret" name="hide" data-toggle="tooltip" data-placement="top" title="Hide this topic"></button>
                                         <button class="btn btn-icon fa fa-user-secret" name="hide" data-toggle="tooltip" data-placement="top" title="Hide this topic"></button>
                                     </form>
                                     </form>