Browse Source

Simplify bookmark page querying, drop pagination

Pagination isn't working correctly for bookmarks yet, and it seems like
it'll take some fiddly work to be able to make PaginatedQuery be able to
handle using the bookmark's created_time instead of the post's.

This just simplifies the method by using an inner join on the bookmarks
instead of an EXISTS subquery, and totally removes pagination for now.
merge-requests/51/head
Deimos 6 years ago
parent
commit
cdd4371d92
  1. 10
      tildes/tildes/models/comment/comment_query.py
  2. 10
      tildes/tildes/models/topic/topic_query.py
  3. 19
      tildes/tildes/views/bookmarks.py

10
tildes/tildes/models/comment/comment_query.py

@ -25,6 +25,8 @@ class CommentQuery(PaginatedQuery):
""" """
super().__init__(Comment, request) super().__init__(Comment, request)
self._only_bookmarked = False
def _attach_extra_data(self) -> "CommentQuery": def _attach_extra_data(self) -> "CommentQuery":
"""Attach the extra user data to the query.""" """Attach the extra user data to the query."""
# pylint: disable=protected-access # pylint: disable=protected-access
@ -48,12 +50,13 @@ class CommentQuery(PaginatedQuery):
def _attach_bookmark_data(self) -> "CommentQuery": def _attach_bookmark_data(self) -> "CommentQuery":
"""Join the data related to whether the user has bookmarked the comment.""" """Join the data related to whether the user has bookmarked the comment."""
query = self.outerjoin(
query = self.join(
CommentBookmark, CommentBookmark,
and_( and_(
CommentBookmark.comment_id == Comment.comment_id, CommentBookmark.comment_id == Comment.comment_id,
CommentBookmark.user == self.request.user, CommentBookmark.user == self.request.user,
), ),
isouter=(not self._only_bookmarked),
) )
query = query.add_columns(CommentBookmark.created_time) query = query.add_columns(CommentBookmark.created_time)
@ -74,3 +77,8 @@ class CommentQuery(PaginatedQuery):
comment.bookmark_created_time = result.created_time comment.bookmark_created_time = result.created_time
return comment return comment
def only_bookmarked(self) -> "CommentQuery":
"""Restrict the comments to ones that the user has bookmarked (generative)."""
self._only_bookmarked = True
return self

10
tildes/tildes/models/topic/topic_query.py

@ -33,6 +33,8 @@ class TopicQuery(PaginatedQuery):
""" """
super().__init__(Topic, request) super().__init__(Topic, request)
self._only_bookmarked = False
def _attach_extra_data(self) -> "TopicQuery": def _attach_extra_data(self) -> "TopicQuery":
"""Attach the extra user data to the query.""" """Attach the extra user data to the query."""
if not self.request.user: if not self.request.user:
@ -56,12 +58,13 @@ class TopicQuery(PaginatedQuery):
def _attach_bookmark_data(self) -> "TopicQuery": def _attach_bookmark_data(self) -> "TopicQuery":
"""Join the data related to whether the user has bookmarked the topic.""" """Join the data related to whether the user has bookmarked the topic."""
query = self.outerjoin(
query = self.join(
TopicBookmark, TopicBookmark,
and_( and_(
TopicBookmark.topic_id == Topic.topic_id, TopicBookmark.topic_id == Topic.topic_id,
TopicBookmark.user == self.request.user, TopicBookmark.user == self.request.user,
), ),
isouter=(not self._only_bookmarked),
) )
query = query.add_columns(TopicBookmark.created_time) query = query.add_columns(TopicBookmark.created_time)
@ -164,3 +167,8 @@ class TopicQuery(PaginatedQuery):
def search(self, query: str) -> "TopicQuery": def search(self, query: str) -> "TopicQuery":
"""Restrict the topics to ones that match a search query (generative).""" """Restrict the topics to ones that match a search query (generative)."""
return self.filter(Topic.search_tsv.op("@@")(func.plainto_tsquery(query))) return self.filter(Topic.search_tsv.op("@@")(func.plainto_tsquery(query)))
def only_bookmarked(self) -> "TopicQuery":
"""Restrict the topics to ones that the user has bookmarked (generative)."""
self._only_bookmarked = True
return self

19
tildes/tildes/views/bookmarks.py

@ -6,8 +6,7 @@ from marshmallow.fields import String
from marshmallow.validate import OneOf from marshmallow.validate import OneOf
from pyramid.request import Request from pyramid.request import Request
from pyramid.view import view_config from pyramid.view import view_config
from sqlalchemy.sql import exists, desc
from sqlalchemy.sql.expression import and_
from sqlalchemy.sql import desc
from webargs.pyramidparser import use_kwargs from webargs.pyramidparser import use_kwargs
from tildes.models.comment import Comment, CommentBookmark from tildes.models.comment import Comment, CommentBookmark
@ -28,6 +27,7 @@ def get_bookmarks(
post_type: Optional[str] = None, post_type: Optional[str] = None,
) -> dict: ) -> dict:
"""Generate the bookmarks page.""" """Generate the bookmarks page."""
# pylint: disable=unused-argument
user = request.user user = request.user
bookmark_cls: Union[Type[CommentBookmark], Type[TopicBookmark]] bookmark_cls: Union[Type[CommentBookmark], Type[TopicBookmark]]
@ -41,18 +41,7 @@ def get_bookmarks(
query = ( query = (
request.query(post_cls) request.query(post_cls)
.filter(
exists()
.where(
and_(
bookmark_cls.user == user,
bookmark_cls.topic_id == post_cls.topic_id
if post_cls == Topic
else bookmark_cls.comment_id == post_cls.comment_id,
)
)
.correlate(bookmark_cls)
)
.only_bookmarked()
.order_by(desc(bookmark_cls.created_time)) .order_by(desc(bookmark_cls.created_time))
) )
@ -64,6 +53,6 @@ def get_bookmarks(
query = query.join_all_relationships() query = query.join_all_relationships()
posts = query.get_page(per_page)
posts = query.all()
return {"user": user, "posts": posts, "post_type": post_type} return {"user": user, "posts": posts, "post_type": post_type}
Loading…
Cancel
Save