100 lines
3.6 KiB
Python
100 lines
3.6 KiB
Python
from flask import render_template
|
|
import definitions as lib
|
|
from difflib import SequenceMatcher
|
|
|
|
dbname = "site.db"
|
|
|
|
# Fonctions qui seront associées à des URLs
|
|
|
|
def main():
|
|
return render_template("index.html")
|
|
|
|
def error():
|
|
return render_template("error.html")
|
|
|
|
def bateau(idbateau):
|
|
dico = {}
|
|
dico["NomBateau"] = "Le Bateau"
|
|
dico["nom"] = "Le constructeur"
|
|
dico["Dimensions"] = 666
|
|
dico["DateInstallation"] = "Aujourd'hui"
|
|
dico["DateCondamnation"] = "Demain"
|
|
dico["ville"] = "ici"
|
|
dico["HistoireBateau"] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam at odio cursus, rutrum odio non, dignissim risus. Pellentesque non varius ante. Vivamus scelerisque pulvinar mauris, nec imperdiet ante mattis et."
|
|
result = lib.ex_sql(dbname,"SELECT * FROM bateau WHERE ship_id=?",(idbateau,))
|
|
if len(result)>=1:
|
|
result = result[0]
|
|
dico["nom"] = result["nom"]
|
|
print(result)
|
|
dico["Dimensions"] = result["dimension"]
|
|
dico["DateInstallation"] = result["date_naissance"]
|
|
dico["DateCondamnation"] = result["date_mort"]
|
|
return render_template("sauveteur.html",**dico)
|
|
else:
|
|
return error()
|
|
|
|
def expeditions(idexpedition):
|
|
dico = {}
|
|
result = lib.ex_sql(dbname,"SELECT * FROM expedition WHERE expedition_id=?",(idexpedition,))
|
|
if len(result) >= 1:
|
|
result = result[0]
|
|
dico["evenement"] = result["nom"]
|
|
dico["Date"] = result["date"]
|
|
print("dico")
|
|
return render_template("expedition.html",**dico)
|
|
else:
|
|
return error()
|
|
|
|
def sauveteur(idsauveteur):
|
|
dico = {}
|
|
dico["nom"] = "John"
|
|
dico["prenom"] = "Doe"
|
|
dico["nb_sauvetages"] = 666
|
|
dico["nb_sauves"] = 666
|
|
dico["gratifications"] = 1
|
|
dico["description"] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam at odio cursus, rutrum odio non, dignissim risus. Pellentesque non varius ante. Vivamus scelerisque pulvinar mauris, nec imperdiet ante mattis et."
|
|
dico["expeditions"] = []
|
|
result = lib.ex_sql(dbname,"SELECT * FROM sauveteur WHERE personal_id=?",(idsauveteur,))
|
|
if len(result)==1:
|
|
result = result[0]
|
|
dico["nom"] = result["nom"]
|
|
dico["prenom"] = result["prenom"]
|
|
ids = lib.ex_sql(dbname,"SELECT * FROM mission WHERE personal_id=?",(idsauveteur,))
|
|
dico["nb_sauvetages"] = 0
|
|
dico["nb_sauves"] = 0
|
|
for id in ids:
|
|
nbs = lib.ex_sql(dbname,"SELECT * FROM sauvee WHERE expedition_id=?",(id["expedition_id"],))
|
|
dico["nb_sauvetages"]+=1
|
|
dico["nb_sauves"] += len(nbs)
|
|
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.lower(), str(i[key]).lower()).ratio()*r
|
|
print(i["nom"],i["score"])
|
|
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)
|