This commit is contained in:
Jaenai Rugengande Ihimbazwe 2021-12-03 03:31:40 +01:00
commit 98098871c9
3 changed files with 38 additions and 2 deletions

3
app.py
View File

@ -13,8 +13,7 @@ def main():
@app.route('/search', methods=['GET', 'POST'])
def search():
texte = request.form["search"]
print(texte)
return pages.main()
return pages.search(texte)
@app.route('/sauveteurs/<int:idsauveteur>')
@app.route('/sauveteur/<int:idsauveteur>')

View File

@ -1,5 +1,6 @@
from flask import render_template
import definitions as lib
from difflib import SequenceMatcher
dbname = "site.db"
@ -48,3 +49,30 @@ def sauveteur(idsauveteur):
return render_template("sauveteur.html",**dico)
else:
return error()
def search(texte):
sauveurs = lib.ex_sql(dbname,"SELECT * FROM sauveteur")
bateaux = lib.ex_sql(dbname,"SELECT * FROM bateau")
totals = sauveurs+bateaux
for i in totals:
i["score"] = 0
for key in i.keys():
if key!="score":
r = 1
if key=="nom":
r = 3
i["score"] += SequenceMatcher(None, texte, str(i[key])).ratio()*r
totals.sort(key=lambda x : x["score"])
totals.reverse()
totals = totals[:8]
results = []
for i in totals:
r = {}
if "ship_id" in i.keys():
r["lien"] = "/bateaux/"+str(i["ship_id"])
elif "personal_id" in i.keys():
r["lien"] = "/sauveteurs/"+str(i["personal_id"])
r["nom"] = i["nom"]
results.append(r)
return render_template("searchresults.html",liste=results)

View File

@ -0,0 +1,9 @@
<ul>
{% for result in liste %}
<li><a href="{{result.lien}}">{{result.nom}}</a></li>
{% endfor %}
</ul>