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 3 months ago
parent
commit
5c2ad1f1da
  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

@ -112,6 +112,13 @@ class PaginatedQuery(ModelQuery):
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:
"""Apply the "before" or "after" restrictions if necessary."""
# pylint: disable=assignment-from-no-return
@ -229,6 +236,13 @@ class PaginatedResults:
self.has_next_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]:
"""Iterate over the results."""
return iter(self.results)

3
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) }}
<h2>Search results</h2>
<h2>Found {{ pluralize(topics.total_count, "result") }}</h2>
<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">
{% 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.total_count }} total).
<a href="{{ request.current_listing_normal_url() }}">Back to normal view</a> /
<a href="{{ request.route_url("home", _query={"tag": tag}) }}">
{% if request.user %}
@ -133,7 +133,7 @@
{% endif %}
</a>
{% 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>
{% endif %}

Loading…
Cancel
Save