From d76f539fcf63fc295552fee8e66aaa8fa834a5c5 Mon Sep 17 00:00:00 2001 From: Deimos Date: Fri, 25 Jan 2019 16:50:02 -0700 Subject: [PATCH] Fixed MergedPaginatedResults operating in reverse MergedPaginatedResults didn't have the correct logic for trimming down a result set when the query is reversed (when there's a "before" condition). --- tildes/tildes/models/pagination.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/tildes/tildes/models/pagination.py b/tildes/tildes/models/pagination.py index 6a78017..0f36609 100644 --- a/tildes/tildes/models/pagination.py +++ b/tildes/tildes/models/pagination.py @@ -273,6 +273,10 @@ class MixedPaginatedResults(PaginatedResults): if any([r.query.sort_desc != reverse_sort for r in paginated_results]): raise ValueError("All results must by sorted in the same direction.") + is_query_reversed = paginated_results[0].query.is_reversed + if any([r.query.is_reversed != is_query_reversed for r in paginated_results]): + raise ValueError("All results must have the same directionality.") + # merge all the results into one list and sort it self.results = sorted( chain.from_iterable(paginated_results), @@ -282,13 +286,16 @@ class MixedPaginatedResults(PaginatedResults): self.per_page = min([r.per_page for r in paginated_results]) - if len(self.results) > self.per_page: - self.has_next_page = True - self.results = self.results[: self.per_page] - else: - self.has_next_page = any([r.has_next_page for r in paginated_results]) - self.has_prev_page = any([r.has_prev_page for r in paginated_results]) + self.has_next_page = any([r.has_next_page for r in paginated_results]) + + if len(self.results) > self.per_page: + if is_query_reversed: + self.results = self.results[-self.per_page :] + self.has_prev_page = True + else: + self.results = self.results[: self.per_page] + self.has_next_page = True @property def next_page_after_id36(self) -> str: