From 4d88ff77f134f75afb0cf1d5282828b404a56574 Mon Sep 17 00:00:00 2001 From: kencx Date: Fri, 16 Jun 2023 22:00:49 +0800 Subject: [PATCH 1/4] Display num of results when searching/filtering tags --- tildes/tildes/templates/search.jinja2 | 3 ++- tildes/tildes/templates/topic_listing.jinja2 | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/tildes/tildes/templates/search.jinja2 b/tildes/tildes/templates/search.jinja2 index 721b0f9..528be15 100644 --- a/tildes/tildes/templates/search.jinja2 +++ b/tildes/tildes/templates/search.jinja2 @@ -5,6 +5,7 @@ {% from 'macros/forms.jinja2' import search_form %} {% from 'macros/links.jinja2' import link_to_group with context %} +{% from 'macros/utils.jinja2' import pluralize %} {% block title %}Search results: {{ search }}{% endblock %} @@ -29,7 +30,7 @@ {% block sidebar %} {{ search_form(search, group) }} -

Search results

+

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

Back to home page

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

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

Back to normal view / {% if request.user %} @@ -114,7 +115,7 @@ {% endif %} {% else %} - Showing only topics with the tag "{{ tag|replace('_', ' ') }}". + Showing only topics with the tag "{{ tag|replace('_', ' ') }}" ({{ topics|length }} total). Back to normal view {% endif %} From cac110c2e5c656c8570ca5f0ecdeb0cac636ff0c Mon Sep 17 00:00:00 2001 From: kencx Date: Sun, 18 Jun 2023 13:09:03 +0800 Subject: [PATCH 2/4] 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 %} From ee3dc6f7a6edfabd5555b7691df458098223cdbf Mon Sep 17 00:00:00 2001 From: kencx Date: Sun, 18 Jun 2023 13:18:46 +0800 Subject: [PATCH 3/4] Fix to remove stray pasted line --- tildes/tildes/templates/topic_listing.jinja2 | 1 - 1 file changed, 1 deletion(-) diff --git a/tildes/tildes/templates/topic_listing.jinja2 b/tildes/tildes/templates/topic_listing.jinja2 index 0c12391..d263a4a 100644 --- a/tildes/tildes/templates/topic_listing.jinja2 +++ b/tildes/tildes/templates/topic_listing.jinja2 @@ -105,7 +105,6 @@ {% if tag %} {% if is_single_group %} 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 / {% if request.user %} From 2fedd998361e9ea7e46647c0bf893bffa3b311a9 Mon Sep 17 00:00:00 2001 From: kencx Date: Tue, 20 Jun 2023 22:26:45 +0800 Subject: [PATCH 4/4] Refactor to use count() --- tildes/tildes/models/pagination.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tildes/tildes/models/pagination.py b/tildes/tildes/models/pagination.py index 1462519..c1353fd 100644 --- a/tildes/tildes/models/pagination.py +++ b/tildes/tildes/models/pagination.py @@ -112,7 +112,7 @@ class PaginatedQuery(ModelQuery): return self - def remove_before_after(self): + def remove_before_after(self) -> PaginatedQuery: """Remove all before or after restrictions from query""" self.before_id = None self.after_id = None @@ -237,7 +237,11 @@ class PaginatedResults: self.has_prev_page = False # fetch total number of results without pagination - self.total_count = len(query.remove_before_after().all()) + self.total_count = ( + query.request.db_session.query(func.count()) + .select_from(query.remove_before_after().statement) + .scalar() + ) def __iter__(self) -> Iterator[Any]: """Iterate over the results."""