language filter on community list #51

This commit is contained in:
rimu 2024-05-08 19:55:04 +12:00
parent 2d5bbbb3bf
commit cba60c2aee
2 changed files with 17 additions and 5 deletions

View file

@ -29,7 +29,7 @@ from app.utils import render_template, get_setting, gibberish, request_etag_matc
blocked_instances, communities_banned_from, topic_tree, recently_upvoted_posts, recently_downvoted_posts, \ blocked_instances, communities_banned_from, topic_tree, recently_upvoted_posts, recently_downvoted_posts, \
generate_image_from_video_url, blocked_users generate_image_from_video_url, blocked_users
from app.models import Community, CommunityMember, Post, Site, User, utcnow, Domain, Topic, File, Instance, \ from app.models import Community, CommunityMember, Post, Site, User, utcnow, Domain, Topic, File, Instance, \
InstanceRole, Notification InstanceRole, Notification, Language, community_language
from PIL import Image from PIL import Image
import pytesseract import pytesseract
@ -185,20 +185,22 @@ def list_communities():
verification_warning() verification_warning()
search_param = request.args.get('search', '') search_param = request.args.get('search', '')
topic_id = int(request.args.get('topic_id', 0)) topic_id = int(request.args.get('topic_id', 0))
language_id = int(request.args.get('language_id', 0))
sort_by = text('community.' + request.args.get('sort_by') if request.args.get('sort_by') else 'community.post_reply_count desc') sort_by = text('community.' + request.args.get('sort_by') if request.args.get('sort_by') else 'community.post_reply_count desc')
topics = Topic.query.order_by(Topic.name).all() topics = Topic.query.order_by(Topic.name).all()
languages = Language.query.order_by(Language.name).all()
communities = Community.query.filter_by(banned=False) communities = Community.query.filter_by(banned=False)
if search_param == '': if search_param == '':
pass pass
else: else:
communities = communities.filter(or_(Community.title.ilike(f"%{search_param}%"), Community.ap_id.ilike(f"%{search_param}%"))) communities = communities.filter(or_(Community.title.ilike(f"%{search_param}%"), Community.ap_id.ilike(f"%{search_param}%")))
#query = search(select(Community), search_param, sort=True) # todo: exclude banned communities from search
#communities = db.session.scalars(query).all()
if topic_id != 0: if topic_id != 0:
communities = communities.filter_by(topic_id=topic_id) communities = communities.filter_by(topic_id=topic_id)
if language_id != 0:
communities = communities.join(community_language).filter(community_language.c.language_id == language_id)
if current_user.is_authenticated: if current_user.is_authenticated:
banned_from = communities_banned_from(current_user.id) banned_from = communities_banned_from(current_user.id)
if banned_from: if banned_from:
@ -207,7 +209,7 @@ def list_communities():
return render_template('list_communities.html', communities=communities.order_by(sort_by).all(), search=search_param, title=_('Communities'), return render_template('list_communities.html', communities=communities.order_by(sort_by).all(), search=search_param, title=_('Communities'),
SUBSCRIPTION_PENDING=SUBSCRIPTION_PENDING, SUBSCRIPTION_MEMBER=SUBSCRIPTION_MEMBER, SUBSCRIPTION_PENDING=SUBSCRIPTION_PENDING, SUBSCRIPTION_MEMBER=SUBSCRIPTION_MEMBER,
SUBSCRIPTION_OWNER=SUBSCRIPTION_OWNER, SUBSCRIPTION_MODERATOR=SUBSCRIPTION_MODERATOR, SUBSCRIPTION_OWNER=SUBSCRIPTION_OWNER, SUBSCRIPTION_MODERATOR=SUBSCRIPTION_MODERATOR,
topics=topics, topic_id=topic_id, sort_by=sort_by, topics=topics, languages=languages, topic_id=topic_id, language_id=language_id, sort_by=sort_by,
low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1', moderating_communities=moderating_communities(current_user.get_id()), low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1', moderating_communities=moderating_communities(current_user.get_id()),
joined_communities=joined_communities(current_user.get_id())) joined_communities=joined_communities(current_user.get_id()))

View file

@ -33,6 +33,16 @@
</select> </select>
</form> </form>
{% endif %} {% endif %}
{% if languages %}
<form method="get" style="display:inline;">Language:
<select name="language_id" class="form-control-sm submit_on_change" aria-label="{{ _('Choose a language to filter communities by') }}">
<option value="0">All</option>
{% for language in languages %}
<option value="{{ language.id }}" {{ 'selected' if language.id == language_id }}>{{ language.name }}</option>
{% endfor %}
</select>
</form>
{% endif %}
<form method="get" style="display:inline;"><input type="search" name="search" placeholder="{{ _('Search') }}" value="{{ search }}"></form> <form method="get" style="display:inline;"><input type="search" name="search" placeholder="{{ _('Search') }}" value="{{ search }}"></form>
</div> </div>
<div class="col-auto"> <div class="col-auto">