Browse Source

Refactor topic/comment queries to join voting data

Previously this was using a subquery, which appears to be less efficient
and is more confusing in some ways.
merge-requests/85/head
Deimos 5 years ago
parent
commit
e9096152c3
  1. 36
      tildes/tildes/models/comment/comment_query.py
  2. 30
      tildes/tildes/models/topic/topic_query.py
  3. 4
      tildes/tildes/templates/macros/comments.jinja2
  4. 2
      tildes/tildes/templates/topic.jinja2

36
tildes/tildes/models/comment/comment_query.py

@ -7,8 +7,7 @@ from typing import Any
from pyramid.request import Request
from sqlalchemy import func
from sqlalchemy.orm import aliased
from sqlalchemy.sql.expression import and_
from sqlalchemy.sql.expression import and_, label
from tildes.enums import CommentSortOption
from tildes.models.pagination import PaginatedQuery
@ -41,22 +40,16 @@ class CommentQuery(PaginatedQuery):
return self._attach_vote_data()._attach_bookmark_data()
def _attach_vote_data(self) -> "CommentQuery":
"""Add a subquery to include whether the user has voted."""
vote_subquery = (
self.request.query(aliased(CommentVote))
.filter(
"""Join the data related to whether the user has voted on the comment."""
query = self.join(
CommentVote,
and_(
CommentVote.comment_id == Comment.comment_id,
CommentVote.user_id == self.request.user.user_id,
)
.exists()
.label("user_voted")
CommentVote.user == self.request.user,
),
isouter=(not self._only_user_voted),
)
query = self.add_columns(vote_subquery)
if self._only_user_voted:
query = query.add_columns(CommentVote.created_time)
query = query.filter(vote_subquery)
query = query.add_columns(label("voted_time", CommentVote.created_time))
return query
@ -70,7 +63,9 @@ class CommentQuery(PaginatedQuery):
),
isouter=(not self._only_bookmarked),
)
query = query.add_columns(CommentBookmark.created_time)
query = query.add_columns(
label("bookmarked_time", CommentBookmark.created_time)
)
return query
@ -81,12 +76,11 @@ class CommentQuery(PaginatedQuery):
# the result is already a Comment, no merging needed
comment = result
comment.user_voted = False
comment.bookmark_created_time = None
comment.user_bookmarked = False
else:
comment = result.Comment
comment.user_voted = result.user_voted
comment.bookmark_created_time = result.created_time
comment.user_voted = bool(result.voted_time)
comment.user_bookmarked = bool(result.bookmarked_time)
return comment

30
tildes/tildes/models/topic/topic_query.py

@ -7,8 +7,7 @@ from typing import Any, Sequence
from pyramid.request import Request
from sqlalchemy import func
from sqlalchemy.sql.expression import and_, null
from sqlalchemy.orm import aliased
from sqlalchemy.sql.expression import and_, label, null
from tildes.enums import TopicSortOption
from tildes.lib.datetime import SimpleHoursPeriod, utc_now
@ -46,22 +45,16 @@ class TopicQuery(PaginatedQuery):
return self._attach_vote_data()._attach_visit_data()._attach_bookmark_data()
def _attach_vote_data(self) -> "TopicQuery":
"""Add a subquery to include whether the user has voted."""
vote_subquery = (
self.request.query(aliased(TopicVote))
.filter(
"""Join the data related to whether the user has voted on the topic."""
query = self.join(
TopicVote,
and_(
TopicVote.topic_id == Topic.topic_id,
TopicVote.user == self.request.user,
)
.exists()
.label("user_voted")
),
isouter=(not self._only_user_voted),
)
query = self.add_columns(vote_subquery)
if self._only_user_voted:
query = query.filter(vote_subquery)
query = query.add_columns(TopicVote.created_time)
query = query.add_columns(label("voted_time", TopicVote.created_time))
return query
@ -75,7 +68,7 @@ class TopicQuery(PaginatedQuery):
),
isouter=(not self._only_bookmarked),
)
query = query.add_columns(TopicBookmark.created_time)
query = query.add_columns(label("bookmarked_time", TopicBookmark.created_time))
return query
@ -112,9 +105,8 @@ class TopicQuery(PaginatedQuery):
else:
topic = result.Topic
topic.user_voted = result.user_voted
topic.bookmark_created_time = result.created_time
topic.user_voted = bool(result.voted_time)
topic.user_bookmarked = bool(result.bookmarked_time)
topic.last_visit_time = result.visit_time

4
tildes/tildes/templates/macros/comments.jinja2

@ -151,7 +151,7 @@
{% endif %}
{% if request.has_permission('vote', comment) %}
{% if comment.user_voted is defined and comment.user_voted %}
{% if comment.user_voted %}
<li><button class="btn-post-action btn-post-action-used" name="unvote"
data-ic-delete-from="{{ request.route_url(
'ic_comment_vote',
@ -204,7 +204,7 @@
{% endif %}
{% if request.has_permission('bookmark', comment) %}
{{ post_action_toggle_button("bookmark", comment, is_toggled=comment.bookmark_created_time) }}
{{ post_action_toggle_button("bookmark", comment, is_toggled=comment.user_bookmarked) }}
{% endif %}
{% if request.has_permission("remove", comment) %}

2
tildes/tildes/templates/topic.jinja2

@ -153,7 +153,7 @@
{% endif %}
{% if request.has_permission('bookmark', topic) %}
{{ post_action_toggle_button("bookmark", topic, is_toggled=topic.bookmark_created_time) }}
{{ post_action_toggle_button("bookmark", topic, is_toggled=topic.user_bookmarked) }}
{% endif %}
{% if request.has_permission("remove", topic) %}

Loading…
Cancel
Save