search within community, fixes #97

This commit is contained in:
rimu 2024-05-09 20:00:22 +12:00
parent 15a86d0f84
commit da1069aae3
7 changed files with 56 additions and 19 deletions

View file

@ -3,7 +3,7 @@ from flask_login import login_required, current_user
from flask_babel import _ from flask_babel import _
from sqlalchemy import or_ from sqlalchemy import or_
from app.models import Post from app.models import Post, Language, Community
from app.search import bp from app.search import bp
from app.utils import moderating_communities, joined_communities, render_template, blocked_domains, blocked_instances, \ from app.utils import moderating_communities, joined_communities, render_template, blocked_domains, blocked_instances, \
communities_banned_from, recently_upvoted_posts, recently_downvoted_posts, blocked_users communities_banned_from, recently_upvoted_posts, recently_downvoted_posts, blocked_users
@ -11,9 +11,19 @@ from app.utils import moderating_communities, joined_communities, render_templat
@bp.route('/search', methods=['GET', 'POST']) @bp.route('/search', methods=['GET', 'POST'])
def run_search(): def run_search():
languages = Language.query.order_by(Language.name).all()
communities = Community.query.filter(Community.banned == False).order_by(Community.name)
if current_user.is_authenticated:
banned_from = communities_banned_from(current_user.id)
communities = communities.filter(Community.id.not_in(banned_from))
else:
banned_from = []
if request.args.get('q') is not None: if request.args.get('q') is not None:
q = request.args.get('q') 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)
language_id = request.args.get('language', 0, type=int)
low_bandwidth = request.cookies.get('low_bandwidth', '0') == '1' low_bandwidth = request.cookies.get('low_bandwidth', '0') == '1'
posts = Post.query.search(q) posts = Post.query.search(q)
@ -34,7 +44,6 @@ def run_search():
blocked_accounts = blocked_users(current_user.id) blocked_accounts = blocked_users(current_user.id)
if blocked_accounts: if blocked_accounts:
posts = posts.filter(Post.user_id.not_in(blocked_accounts)) posts = posts.filter(Post.user_id.not_in(blocked_accounts))
banned_from = communities_banned_from(current_user.id)
if banned_from: if banned_from:
posts = posts.filter(Post.community_id.not_in(banned_from)) posts = posts.filter(Post.community_id.not_in(banned_from))
else: else:
@ -43,6 +52,10 @@ 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 community_id:
posts = posts.filter(Post.community_id == community_id)
if language_id:
posts = posts.filter(Post.language_id == language_id)
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)
@ -59,6 +72,8 @@ def run_search():
recently_downvoted = [] recently_downvoted = []
return render_template('search/results.html', title=_('Search results for %(q)s', q=q), posts=posts, q=q, return render_template('search/results.html', title=_('Search results for %(q)s', q=q), posts=posts, q=q,
community_id=community_id, language_id=language_id, communities=communities.all(),
languages=languages,
next_url=next_url, prev_url=prev_url, show_post_community=True, next_url=next_url, prev_url=prev_url, show_post_community=True,
recently_upvoted=recently_upvoted, recently_upvoted=recently_upvoted,
recently_downvoted=recently_downvoted, recently_downvoted=recently_downvoted,
@ -67,7 +82,8 @@ def run_search():
site=g.site) site=g.site)
else: else:
return render_template('search/start.html', title=_('Search'), return render_template('search/start.html', title=_('Search'), communities=communities.all(),
languages=languages,
moderating_communities=moderating_communities(current_user.get_id()), moderating_communities=moderating_communities(current_user.get_id()),
joined_communities=joined_communities(current_user.get_id()), joined_communities=joined_communities(current_user.get_id()),
site=g.site) site=g.site)

View file

@ -126,9 +126,10 @@
{% endif %} {% endif %}
</div> </div>
</div> </div>
<!-- <form method="get"> <form method="get" action="/search">
<input type="search" name="search" class="form-control mt-2" placeholder="{{ _('Search this community') }}" /> <input type="search" name="q" class="form-control mt-2" placeholder="{{ _('Search this community') }}" />
</form> --> <input type="hidden" name="community" value="{{ community.id }}">
</form>
</div> </div>
</div> </div>
<div class="card mt-3"> <div class="card mt-3">

View file

@ -56,9 +56,10 @@
</div> </div>
{% endif %} {% endif %}
</div> </div>
<!-- <form method="get"> <form method="get" action="/search">
<input type="search" name="search" class="form-control mt-2" placeholder="{{ _('Search this community') }}" /> <input type="search" name="q" class="form-control mt-2" placeholder="{{ _('Search this community') }}" />
</form> --> <input type="hidden" name="community" value="{{ post.community.id }}">
</form>
</div> </div>
</div> </div>
<div class="card mt-3"> <div class="card mt-3">

View file

@ -107,9 +107,10 @@
</div> </div>
{% endif %} {% endif %}
</div> </div>
<!-- <form method="get"> <form method="get" action="/search">
<input type="search" name="search" class="form-control mt-2" placeholder="{{ _('Search this community') }}" /> <input type="search" name="q" class="form-control mt-2" placeholder="{{ _('Search this community') }}" />
</form> --> <input type="hidden" name="community" value="{{ post.community.id }}">
</form>
</div> </div>
</div> </div>
<div class="card mt-3"> <div class="card mt-3">

View file

@ -182,9 +182,10 @@
{% endif %} {% endif %}
</div> </div>
</div> </div>
<!-- <form method="get"> <form method="get" action="/search">
<input type="search" name="search" class="form-control mt-2" placeholder="{{ _('Search this community') }}" /> <input type="search" name="q" class="form-control mt-2" placeholder="{{ _('Search this community') }}" />
</form> --> <input type="hidden" name="community" value="{{ post.community.id }}">
</form>
</div> </div>
</div> </div>
<div class="card mt-3"> <div class="card mt-3">

View file

@ -52,9 +52,10 @@
</div> </div>
{% endif %} {% endif %}
</div> </div>
<!-- <form method="get"> <form method="get" action="/search">
<input type="search" name="search" class="form-control mt-2" placeholder="{{ _('Search this community') }}" /> <input type="search" name="q" class="form-control mt-2" placeholder="{{ _('Search this community') }}" />
</form> --> <input type="hidden" name="community" value="{{ post.community.id }}">
</form>
</div> </div>
</div> </div>
<div class="card mt-3"> <div class="card mt-3">

View file

@ -12,9 +12,25 @@
<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 visually-hidden" for="search_term" aria-label="Search here">Search</label> <div class="form-group required"><label class="form-control-label" for="search_term" aria-label="Search here">{{ _('Keyword') }}</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" required="" type="search" value="">
</div> </div>
<div class="form-group"><label class="form-control-label" for="community" aria-label="Restrict results by language">{{ _('Community') }}</label>
<select class="form-control" id="community" name="community">
<option value="0">{{ _('All') }}</option>
{% for community in communities %}
<option value="{{ community.id }}">{{ community.display_name() }}</option>
{% endfor %}
</select>
</div>
<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">
<option value="0">{{ _('All') }}</option>
{% for language in languages %}
<option value="{{ language.id }}">{{ language.name }}</option>
{% endfor %}
</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>