a slew of new search filters

This commit is contained in:
rimu 2024-09-03 18:50:11 +12:00
parent 24c7d6fd55
commit 465790ed41
2 changed files with 40 additions and 13 deletions

View file

@ -1,7 +1,7 @@
from flask import request, flash, json, url_for, current_app, redirect, g from flask import request, flash, json, url_for, current_app, redirect, g
from flask_login import login_required, current_user from flask_login import login_required, current_user
from flask_babel import _ from flask_babel import _
from sqlalchemy import or_ from sqlalchemy import or_, desc
from app.models import Post, Language, Community from app.models import Post, Language, Community
from app.search import bp from app.search import bp
@ -22,14 +22,16 @@ def run_search():
else: else:
banned_from = [] banned_from = []
if request.args.get('q') is not None:
q = request.args.get('q')
page = request.args.get('page', 1, type=int) page = request.args.get('page', 1, type=int)
community_id = request.args.get('community', 0, type=int) community_id = request.args.get('community', 0, type=int)
language_id = request.args.get('language', 0, type=int) language_id = request.args.get('language', 0, type=int)
type = request.args.get('type', 0, type=int)
low_bandwidth = request.cookies.get('low_bandwidth', '0') == '1' low_bandwidth = request.cookies.get('low_bandwidth', '0') == '1'
q = request.args.get('q')
sort_by = request.args.get('sort_by', '')
posts = Post.query.search(q) if q is not None or type != 0 or language_id != 0 or community_id != 0:
posts = Post.query
if current_user.is_authenticated: if current_user.is_authenticated:
if current_user.ignore_bots == 1: if current_user.ignore_bots == 1:
posts = posts.filter(Post.from_bot == False) posts = posts.filter(Post.from_bot == False)
@ -58,10 +60,18 @@ def run_search():
posts = posts.filter(Post.nsfw == False) posts = posts.filter(Post.nsfw == False)
posts = posts.filter(Post.indexable == True) posts = posts.filter(Post.indexable == True)
if q is not None:
posts = posts.search(q, sort=True if sort_by == '' else False)
if type != 0:
posts = posts.filter(Post.type == type)
if community_id: if community_id:
posts = posts.filter(Post.community_id == community_id) posts = posts.filter(Post.community_id == community_id)
if language_id: if language_id:
posts = posts.filter(Post.language_id == language_id) posts = posts.filter(Post.language_id == language_id)
if sort_by == 'date':
posts = posts.order_by(desc(Post.posted_at))
elif sort_by == 'top':
posts = posts.order_by(desc(Post.up_votes - Post.down_votes))
posts = posts.paginate(page=page, per_page=100 if current_user.is_authenticated and not low_bandwidth else 50, posts = posts.paginate(page=page, per_page=100 if current_user.is_authenticated and not low_bandwidth else 50,
error_out=False) error_out=False)

View file

@ -12,25 +12,42 @@
<div class="card-body p-6"> <div class="card-body p-6">
<div class="card-title">{{ _('Search for posts') }}</div> <div class="card-title">{{ _('Search for posts') }}</div>
<form action="" method="get" class="form" role="form"> <form action="" method="get" class="form" role="form">
<div class="form-group required"><label class="form-control-label" for="search_term" aria-label="Search here">{{ _('Keyword') }}</label> <div class="form-group"><label class="form-control-label" for="search_term" aria-label="Search here">{{ _('Keyword') }} ({{ _('optional') }})</label>
<input autofocus="" class="form-control" id="search_term" name="q" required="" type="search" value=""> <input autofocus="" class="form-control" id="search_term" name="q" type="search" value="">
</div> </div>
<div class="form-group"><label class="form-control-label" for="community" aria-label="Restrict results by language">{{ _('Community') }}</label> <div class="form-group"><label class="form-control-label" for="community" aria-label="Restrict results by community">{{ _('Community') }}</label>
<select class="form-control" id="community" name="community"> <select class="form-control form-select" id="community" name="community">
<option value="0">{{ _('All') }}</option> <option value="0">{{ _('All') }}</option>
{% for community in communities %} {% for community in communities %}
<option value="{{ community.id }}">{{ community.display_name() }}</option> <option value="{{ community.id }}">{{ community.display_name() }}</option>
{% endfor %} {% endfor %}
</select> </select>
</div> </div>
<div class="form-group"><label class="form-control-label" for="type" aria-label="Restrict results by type">{{ _('Type') }}</label>
<select class="form-control form-select" id="type" name="type">
<option value="0">{{ _('All') }}</option>
<option value="{{ POST_TYPE_ARTICLE }}">{{ _('Discussion') }}</option>
<option value="{{ POST_TYPE_LINK }}">{{ _('Link') }}</option>
<option value="{{ POST_TYPE_IMAGE }}">{{ _('Image') }}</option>
<option value="{{ POST_TYPE_VIDEO }}">{{ _('Video') }}</option>
<option value="{{ POST_TYPE_POLL }}">{{ _('Poll') }}</option>
</select>
</div>
<div class="form-group"><label class="form-control-label" for="language" aria-label="Restrict results by language">{{ _('Language') }}</label> <div class="form-group"><label class="form-control-label" for="language" aria-label="Restrict results by language">{{ _('Language') }}</label>
<select class="form-control" id="language" name="language"> <select class="form-control form-select" id="language" name="language">
<option value="0">{{ _('All') }}</option> <option value="0">{{ _('All') }}</option>
{% for language in languages %} {% for language in languages %}
<option value="{{ language.id }}">{{ language.name }}</option> <option value="{{ language.id }}">{{ language.name }}</option>
{% endfor %} {% endfor %}
</select> </select>
</div> </div>
<div class="form-group"><label class="form-control-label" for="type" aria-label="Sort results">{{ _('Sort by') }}</label>
<select class="form-control form-select" id="sort_by" name="sort_by">
<option value="">{{ _('Relevance') }}</option>
<option value="date">{{ _('Recent first') }}</option>
<option value="top">{{ _('Top') }}</option>
</select>
</div>
<input class="btn btn-primary btn-md" id="submit" name="submit" type="submit" value="Search"> <input class="btn btn-primary btn-md" id="submit" name="submit" type="submit" value="Search">
</form> </form>
<h6 class="mt-5">{{ _('Example searches:') }} </h6> <h6 class="mt-5">{{ _('Example searches:') }} </h6>