Add 'instance software' selection to Search

(will look messy though, if all software names in DB aren't lower-cased.)
This commit is contained in:
freamon 2024-09-03 16:47:26 +00:00
parent 465790ed41
commit 1aa023c736
3 changed files with 22 additions and 2 deletions

View file

@ -87,6 +87,10 @@ class Instance(db.Model):
def __repr__(self):
return '<Instance {}>'.format(self.domain)
@classmethod
def unique_software_names(cls):
return list(db.session.execute(text('SELECT DISTINCT software FROM instance ORDER BY software')).scalars())
class InstanceRole(db.Model):
instance_id = db.Column(db.Integer, db.ForeignKey('instance.id'), primary_key=True)

View file

@ -3,7 +3,7 @@ from flask_login import login_required, current_user
from flask_babel import _
from sqlalchemy import or_, desc
from app.models import Post, Language, Community
from app.models import Post, Language, Community, Instance
from app.search import bp
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, menu_topics, \
@ -16,6 +16,7 @@ from app.activitypub.util import resolve_remote_post_from_search
def run_search():
languages = Language.query.order_by(Language.name).all()
communities = Community.query.filter(Community.banned == False).order_by(Community.name)
instance_software = Instance.unique_software_names()
if current_user.is_authenticated:
banned_from = communities_banned_from(current_user.id)
communities = communities.filter(Community.id.not_in(banned_from))
@ -26,6 +27,7 @@ def run_search():
community_id = request.args.get('community', 0, type=int)
language_id = request.args.get('language', 0, type=int)
type = request.args.get('type', 0, type=int)
software = request.args.get('software', '')
low_bandwidth = request.cookies.get('low_bandwidth', '0') == '1'
q = request.args.get('q')
sort_by = request.args.get('sort_by', '')
@ -68,6 +70,10 @@ def run_search():
posts = posts.filter(Post.community_id == community_id)
if language_id:
posts = posts.filter(Post.language_id == language_id)
if software:
instances = Instance.query.filter_by(software=software)
instance_ids = [instance.id for instance in instances]
posts = posts.filter(Post.instance_id.in_(instance_ids))
if sort_by == 'date':
posts = posts.order_by(desc(Post.posted_at))
elif sort_by == 'top':
@ -100,7 +106,7 @@ def run_search():
else:
return render_template('search/start.html', title=_('Search'), communities=communities.all(),
languages=languages,
languages=languages, instance_software=instance_software,
moderating_communities=moderating_communities(current_user.get_id()),
joined_communities=joined_communities(current_user.get_id()),
menu_topics=menu_topics(),

View file

@ -41,6 +41,16 @@
{% endfor %}
</select>
</div>
<div class="form-group"><label class="form-control-label" for="software" aria-label="Restrict results by instance software">{{ _('Instance Software') }}</label>
<select class="form-control form-select" id="software" name="software">
<option value="">{{ _('All') }}</option>
{% for software in instance_software %}
{% if not software =='guppe groups' %}
<option value="{{ software }}">{{ software }}</option>
{% endif %}
{% endfor %}
</select>
</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>