diff --git a/app.py b/app.py index 794eaf0..360a122 100644 --- a/app.py +++ b/app.py @@ -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/') @app.route('/sauveteur/') diff --git a/pages.py b/pages.py index 8e6fad7..7505180 100644 --- a/pages.py +++ b/pages.py @@ -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) diff --git a/templates/searchresults.html b/templates/searchresults.html new file mode 100644 index 0000000..178325b --- /dev/null +++ b/templates/searchresults.html @@ -0,0 +1,9 @@ +