From cac110c2e5c656c8570ca5f0ecdeb0cac636ff0c Mon Sep 17 00:00:00 2001 From: kencx Date: Sun, 18 Jun 2023 13:09:03 +0800 Subject: [PATCH] Fix to display total number of results --- tildes/tildes/models/pagination.py | 10 ++++++++++ tildes/tildes/templates/search.jinja2 | 2 +- tildes/tildes/templates/topic_listing.jinja2 | 4 ++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/tildes/tildes/models/pagination.py b/tildes/tildes/models/pagination.py index 9727502..1462519 100644 --- a/tildes/tildes/models/pagination.py +++ b/tildes/tildes/models/pagination.py @@ -112,6 +112,13 @@ class PaginatedQuery(ModelQuery): return self + def remove_before_after(self): + """Remove all before or after restrictions from query""" + self.before_id = None + self.after_id = None + + return self + def _apply_before_or_after(self) -> PaginatedQuery: """Apply the "before" or "after" restrictions if necessary.""" # pylint: disable=assignment-from-no-return @@ -229,6 +236,9 @@ class PaginatedResults: self.has_next_page = False self.has_prev_page = False + # fetch total number of results without pagination + self.total_count = len(query.remove_before_after().all()) + def __iter__(self) -> Iterator[Any]: """Iterate over the results.""" return iter(self.results) diff --git a/tildes/tildes/templates/search.jinja2 b/tildes/tildes/templates/search.jinja2 index 528be15..d16642d 100644 --- a/tildes/tildes/templates/search.jinja2 +++ b/tildes/tildes/templates/search.jinja2 @@ -30,7 +30,7 @@ {% block sidebar %} {{ search_form(search, group) }} -

Found {{ pluralize(topics|length, "result") }}

+

Found {{ pluralize(topics.total_count, "result") }}

Back to home page

diff --git a/tildes/tildes/templates/topic_listing.jinja2 b/tildes/tildes/templates/topic_listing.jinja2 index 94b5d38..0c12391 100644 --- a/tildes/tildes/templates/topic_listing.jinja2 +++ b/tildes/tildes/templates/topic_listing.jinja2 @@ -104,7 +104,7 @@
{% if tag %} {% if is_single_group %} - Showing only topics in {{ link_to_group(group) }} with the tag "{{ tag|replace('_', ' ') }}" ({{ topics|length }} total). + Showing only topics in {{ link_to_group(group) }} with the tag "{{ tag|replace('_', ' ') }}" ({{ topics.total_count }} total).

{{ pluralize(topic.num_comments, "comment") }}

Back to normal view / @@ -115,7 +115,7 @@ {% endif %} {% else %} - Showing only topics with the tag "{{ tag|replace('_', ' ') }}" ({{ topics|length }} total). + Showing only topics with the tag "{{ tag|replace('_', ' ') }}" ({{ topics.total_count }} total). Back to normal view {% endif %}