Skip to content

Commit

Permalink
Merge pull request #411 from justanr/ISSUE-408-n+1-queries
Browse files Browse the repository at this point in the history
Remove n+1 queries in view topic
  • Loading branch information
sh4nks committed Feb 9, 2018
2 parents 9e5ccbd + d5d4988 commit d8397e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
19 changes: 10 additions & 9 deletions flaskbb/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ def get(self, topic_id, slug=None):
topic.views += 1
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
posts = Post.query.\
outerjoin(User, Post.user_id == User.id).\
Expand All @@ -155,15 +165,6 @@ def get(self, topic_id, slug=None):
if len(posts.items) == 0:
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(
'forum/topic.html', topic=topic, posts=posts, last_seen=time_diff(), form=self.form()
)
Expand Down
8 changes: 4 additions & 4 deletions flaskbb/templates/forum/topic.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<!-- 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>
{% endif %}
{% if topic.first_post == post %}
{% if topic.first_post_id == post.id %}
{% 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) }}">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
Expand All @@ -147,14 +147,14 @@
{% endif %}

{% if current_user.permissions.get('makehidden') %}
{% if post.first_post %}
{% if topic.first_post_id == post.id %}
{% 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() }}" />
<button class="btn btn-icon fa fa-user" name="unhide" data-toggle="tooltip" data-placement="top" title="Unhide this topic"></button>
</form>
{% 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() }}" />
<button class="btn btn-icon fa fa-user-secret" name="hide" data-toggle="tooltip" data-placement="top" title="Hide this topic"></button>
</form>
Expand Down

0 comments on commit d8397e2

Please sign in to comment.