Browse Source

Merge branch 'search_results_num' into 'master'

Display num of results when searching/filtering tags

Closes #408

See merge request tildes/tildes!142
merge-requests/142/merge
kenc 1 month ago
parent
commit
0c39c0bb5b
  1. 14
      tildes/tildes/models/pagination.py
  2. 3
      tildes/tildes/templates/search.jinja2
  3. 4
      tildes/tildes/templates/topic_listing.jinja2

14
tildes/tildes/models/pagination.py

@ -113,6 +113,13 @@ class PaginatedQuery(ModelQuery):
return self return self
def remove_before_after(self) -> PaginatedQuery:
"""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: def _apply_before_or_after(self) -> PaginatedQuery:
"""Apply the "before" or "after" restrictions if necessary.""" """Apply the "before" or "after" restrictions if necessary."""
# pylint: disable=assignment-from-no-return # pylint: disable=assignment-from-no-return
@ -232,6 +239,13 @@ class PaginatedResults:
self.has_next_page = False self.has_next_page = False
self.has_prev_page = False self.has_prev_page = False
# fetch total number of results without pagination
self.total_count = (
query.request.db_session.query(func.count())
.select_from(query.remove_before_after().statement)
.scalar()
)
def __iter__(self) -> Iterator[Any]: def __iter__(self) -> Iterator[Any]:
"""Iterate over the results.""" """Iterate over the results."""
return iter(self.results) return iter(self.results)

3
tildes/tildes/templates/search.jinja2

@ -5,6 +5,7 @@
{% from 'macros/forms.jinja2' import search_form %} {% from 'macros/forms.jinja2' import search_form %}
{% from 'macros/links.jinja2' import link_to_group with context %} {% from 'macros/links.jinja2' import link_to_group with context %}
{% from 'macros/utils.jinja2' import pluralize %}
{% block title %}Search results: {{ search }}{% endblock %} {% block title %}Search results: {{ search }}{% endblock %}
@ -29,7 +30,7 @@
{% block sidebar %} {% block sidebar %}
{{ search_form(search, group) }} {{ search_form(search, group) }}
<h2>Search results</h2>
<h2>Found {{ pluralize(topics.total_count, "result") }}</h2>
<p><a href="/">Back to home page</a></p> <p><a href="/">Back to home page</a></p>

4
tildes/tildes/templates/topic_listing.jinja2

@ -123,7 +123,7 @@
<div class="topic-listing-filter"> <div class="topic-listing-filter">
{% if tag %} {% if tag %}
{% if is_single_group %} {% 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.total_count }} total).
<a href="{{ request.current_listing_normal_url() }}">Back to normal view</a> / <a href="{{ request.current_listing_normal_url() }}">Back to normal view</a> /
<a href="{{ request.route_url("home", _query={"tag": tag}) }}"> <a href="{{ request.route_url("home", _query={"tag": tag}) }}">
{% if request.user %} {% if request.user %}
@ -133,7 +133,7 @@
{% endif %} {% endif %}
</a> </a>
{% else %} {% else %}
Showing only topics with the tag "{{ tag|replace('_', ' ') }}".
Showing only topics with the tag "{{ tag|replace('_', ' ') }}" ({{ topics.total_count }} total).
<a href="{{ request.current_listing_normal_url() }}">Back to normal view</a> <a href="{{ request.current_listing_normal_url() }}">Back to normal view</a>
{% endif %} {% endif %}

Loading…
Cancel
Save