Browse Source

Add basic group-specific search

If you search while inside a group, it will now search only inside that
group. A note at the top of the results page will explain this, and
includes a link to search across all groups instead.
merge-requests/76/head
Deimos 5 years ago
parent
commit
20e99909a5
  1. 2
      tildes/tildes/__init__.py
  2. 2
      tildes/tildes/routes.py
  3. 12
      tildes/tildes/templates/macros/forms.jinja2
  4. 12
      tildes/tildes/templates/search.jinja2
  5. 6
      tildes/tildes/templates/topic_listing.jinja2
  6. 10
      tildes/tildes/views/topic.py

2
tildes/tildes/__init__.py

@ -137,6 +137,7 @@ def current_listing_base_url(
base_vars_by_route: Dict[str, Tuple[str, ...]] = {
"bookmarks": ("per_page", "type"),
"group": ("order", "period", "per_page", "tag", "unfiltered"),
"group_search": ("order", "period", "per_page", "q"),
"home": ("order", "period", "per_page", "tag", "unfiltered"),
"search": ("order", "period", "per_page", "q"),
"user": ("order", "per_page", "type"),
@ -169,6 +170,7 @@ def current_listing_normal_url(
normal_vars_by_route: Dict[str, Tuple[str, ...]] = {
"bookmarks": ("order", "period", "per_page"),
"group": ("order", "period", "per_page"),
"group_search": ("order", "period", "per_page", "q"),
"home": ("order", "period", "per_page"),
"notifications": ("per_page",),
"search": ("order", "period", "per_page", "q"),

2
tildes/tildes/routes.py

@ -36,6 +36,8 @@ def includeme(config: Configurator) -> None:
config.add_route("group_topics", "/topics", factory=group_by_path)
config.add_route("group_search", "/search", factory=group_by_path)
config.add_route("group_wiki", "/wiki", factory=group_by_path)
# if you change this from "new_page" make sure to also edit

12
tildes/tildes/templates/macros/forms.jinja2

@ -61,8 +61,16 @@
</div>
{% endmacro %}
{% macro search_form(existing_query=None) %}
<form class="form-search" method="GET" action="/search">
{% macro search_form(existing_query=None, group=None) %}
<form
class="form-search"
method="GET"
{% if group %}
action="/~{{ group.path }}/search"
{% else %}
action="/search"
{% endif %}
>
<div class="input-group">
<input
type="text"

12
tildes/tildes/templates/search.jinja2

@ -4,13 +4,23 @@
{% extends 'topic_listing.jinja2' %}
{% from 'macros/forms.jinja2' import search_form %}
{% from 'macros/links.jinja2' import group_linked %}
{% block title %}Search results for "{{ search }}"{% endblock %}
{% block header_context_link %}<span class="site-header-context">Search: "{{ search }}"</span>{% endblock %}
{% block filter_info %}
{% if group %}
<div class="topic-listing-filter">
Search results from inside {{ group_linked(group.path) }} only.
<a href="{{ request.route_url("search", _query={"q": search}) }}">Search all groups</a>
</div>
{% endif %}
{% endblock %}
{% block sidebar %}
{{ search_form(search) }}
{{ search_form(search, group) }}
<h2>Search results</h2>

6
tildes/tildes/templates/topic_listing.jinja2

@ -99,7 +99,7 @@
{% endif %}
</div>
{% if search is not defined %}
{% block filter_info %}
<div class="topic-listing-filter">
{% if tag %}
{% if is_single_group %}
@ -124,7 +124,7 @@
<a href="{{ request.current_listing_normal_url({'unfiltered': 'true'}) }}">View unfiltered list</a>
{% endif %}
</div>
{% endif %}
{% endblock %}
{% if topics.has_prev_page %}
<div class="pagination">
@ -206,7 +206,7 @@
{% endblock %}
{% block sidebar %}
{{ search_form() }}
{{ search_form(group=group) }}
<h3>~{{ group.path }}</h3>

10
tildes/tildes/views/topic.py

@ -246,6 +246,7 @@ def get_group_topics(
@view_config(route_name="search", renderer="search.jinja2")
@view_config(route_name="group_search", renderer="search.jinja2")
@use_kwargs(TopicListingSchema(only=("after", "before", "order", "per_page", "period")))
@use_kwargs({"search": String(load_from="q", missing="")})
def get_search(
@ -259,6 +260,10 @@ def get_search(
) -> dict:
"""Get a list of search results."""
# pylint: disable=too-many-arguments
group = None
if isinstance(request.context, Group):
group = request.context
if order is missing:
order = TopicSortOption.NEW
@ -272,6 +277,10 @@ def get_search(
.apply_sort_option(order)
)
# if searching from inside a group, restrict to that group alone
if group:
query = query.inside_groups([group])
# restrict the time period, if not set to "all time"
if period:
query = query.inside_time_period(period)
@ -295,6 +304,7 @@ def get_search(
return {
"search": search,
"topics": topics,
"group": group,
"order": order,
"order_options": TopicSortOption,
"period": period,

Loading…
Cancel
Save