search: Add search over programs
This commit is contained in:
parent
93b2a52752
commit
23209b8e10
|
@ -4,6 +4,7 @@ from app.models.post import Post
|
|||
from app.models.comment import Comment
|
||||
from app.models.topic import Topic
|
||||
from app.models.forum import Forum
|
||||
from app.models.program import Program
|
||||
from app.utils.render import render
|
||||
from sqlalchemy import text, func
|
||||
from flask import request
|
||||
|
@ -36,22 +37,29 @@ def search(page=1):
|
|||
form = AdvancedSearchForm(request.args)
|
||||
results = list()
|
||||
if form.validate():
|
||||
# Topics are sorted first in results
|
||||
tsquery = websearch_to_tsquery_multilang(form.q.data)
|
||||
# Topics are sorted first in results
|
||||
topic_query = db.session.query(Topic).where(
|
||||
to_tsvector_multilang(Topic.title).bool_op('@@')(tsquery)
|
||||
).group_by(
|
||||
Topic.id,
|
||||
Post.id
|
||||
)
|
||||
# Programms follow directly in the results
|
||||
program_query = db.session.query(Program).where(
|
||||
to_tsvector_multilang(Program.name).bool_op('@@')(tsquery)
|
||||
).group_by(
|
||||
Program.id,
|
||||
Post.id
|
||||
)
|
||||
# Comments are less important than topics and programs
|
||||
comments_query = db.session.query(Comment).where(
|
||||
comment_query = db.session.query(Comment).where(
|
||||
to_tsvector_multilang(Comment.text).bool_op('@@')(tsquery)
|
||||
).group_by(
|
||||
Comment.id,
|
||||
Post.id
|
||||
)
|
||||
results = list(topic_query) + list(comments_query)
|
||||
results = list(topic_query) + list(program_query) + list(comment_query)
|
||||
|
||||
results = paginate(results, page, SEARCH_RESULTS_PER_PAGE)
|
||||
return render('search.html', form=form, results=results)
|
||||
|
|
Loading…
Reference in New Issue