PCv5/app/routes/search.py

42 lines
1.3 KiB
Python
Raw Normal View History

from app import app, db
from app.forms.search import AdvancedSearchForm, SearchForm
from app.models.post import Post
from app.models.comment import Comment
from app.models.topic import Topic
from app.models.forum import Forum
2019-02-03 16:20:05 +01:00
from app.utils.render import render
from sqlalchemy import text, func
from flask import request
2019-02-03 16:20:05 +01:00
def websearch_to_tsquery_multilang(search):
return func.websearch_to_tsquery('french', search)
def to_tsvector_multilang(text):
return func.to_tsvector('french', text)
2019-12-16 23:57:50 +01:00
@app.route('/rechercher')
2019-02-03 16:20:05 +01:00
def search():
form = AdvancedSearchForm(request.args)
results = list()
if form.validate():
# Topics are sorted first in results
tsquery = websearch_to_tsquery_multilang(form.q.data)
results = db.session.query(Topic).where(
to_tsvector_multilang(Topic.title).bool_op('@@')(tsquery)
).group_by(
Topic.id,
Post.id
)
# Comments are less important than topics and programs
res = db.session.query(Comment).where(
to_tsvector_multilang(Comment.text).bool_op('@@')(tsquery)
).group_by(
Comment.id,
Post.id
)
results = list(results) + list(res)
return render('search.html', form=form, results=results)