2021-12-02 19:41:51 +01:00
from flask import render_template
2021-12-02 19:05:44 +01:00
import definitions as lib
2021-12-03 03:28:53 +01:00
from difflib import SequenceMatcher
2021-12-02 19:05:44 +01:00
2021-12-03 01:08:46 +01:00
dbname = " site.db "
2021-12-02 19:41:51 +01:00
# Fonctions qui seront associées à des URLs
2021-12-02 19:05:44 +01:00
def main ( ) :
2021-12-02 20:17:23 +01:00
return render_template ( " index.html " )
2021-12-02 20:14:13 +01:00
2021-12-03 05:44:18 +01:00
def modifs ( ) :
return render_template ( " modification.html " )
2021-12-02 20:14:13 +01:00
def error ( ) :
return render_template ( " error.html " )
2021-12-03 01:08:46 +01:00
2021-12-03 02:05:05 +01:00
def bateau ( idbateau ) :
dico = { }
dico [ " NomBateau " ] = " Le Bateau "
dico [ " nom " ] = " Le constructeur "
2021-12-03 05:30:50 +01:00
dico [ " date_naissance " ] = " Aujourd ' hui "
dico [ " date_mort " ] = " Demain "
2021-12-03 02:05:05 +01:00
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 )
2021-12-03 05:30:50 +01:00
dico [ " date_naissance " ] = result [ " date_naissance " ]
dico [ " date_mort " ] = result [ " date_mort " ]
dico [ " Histoire " ] = result [ " histoire " ]
2021-12-03 06:16:05 +01:00
dico [ " dimensions " ] = result [ " dimension " ]
2021-12-03 05:30:50 +01:00
return render_template ( " bateau.html " , * * dico )
2021-12-03 03:52:32 +01:00
else :
return error ( )
def expeditions ( idexpedition ) :
dico = { }
2021-12-03 05:58:51 +01:00
dico [ " Nom " ] = " joe "
dico [ " Date " ] = " quatrieme univers "
2021-12-03 03:52:32 +01:00
result = lib . ex_sql ( dbname , " SELECT * FROM expedition WHERE expedition_id=? " , ( idexpedition , ) )
if len ( result ) > = 1 :
result = result [ 0 ]
2021-12-03 05:58:51 +01:00
dico [ " nom " ] = result [ " nom " ]
dico [ " date " ] = result [ " date " ]
2021-12-03 03:52:32 +01:00
return render_template ( " expedition.html " , * * dico )
2021-12-03 02:05:05 +01:00
else :
return error ( )
2021-12-03 01:08:46 +01:00
def sauveteur ( idsauveteur ) :
dico = { }
dico [ " nom " ] = " John "
dico [ " prenom " ] = " Doe "
dico [ " nb_sauvetages " ] = 666
dico [ " nb_sauves " ] = 666
2021-12-03 05:58:51 +01:00
dico [ " naissance " ] = " vie "
dico [ " deces " ] = " mort "
2021-12-03 01:08:46 +01:00
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. "
2021-12-03 04:34:07 +01:00
dico [ " expeditions " ] = [ ]
2021-12-03 02:05:05 +01:00
result = lib . ex_sql ( dbname , " SELECT * FROM sauveteur WHERE personal_id=? " , ( idsauveteur , ) )
if len ( result ) == 1 :
result = result [ 0 ]
2021-12-03 05:58:51 +01:00
print ( result [ " date_naissance " ] )
print ( result [ " date_mort " ] )
dico [ " nom " ] = result [ " nom " ]
dico [ " prenom " ] = result [ " prenom " ]
dico [ " date_naissance " ] = result [ " date_naissance " ]
dico [ " date_deces " ] = result [ " date_mort " ]
2021-12-03 06:16:05 +01:00
dico [ " description " ] = result [ " description " ]
2021-12-03 05:58:51 +01:00
print ( dico [ " naissance " ] )
print ( dico [ " deces " ] )
2021-12-03 04:34:07 +01:00
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 )
2021-12-03 02:05:05 +01:00
return render_template ( " sauveteur.html " , * * dico )
else :
return error ( )
2021-12-03 03:28:53 +01:00
def search ( texte ) :
sauveurs = lib . ex_sql ( dbname , " SELECT * FROM sauveteur " )
bateaux = lib . ex_sql ( dbname , " SELECT * FROM bateau " )
2021-12-03 06:12:33 +01:00
expeditions = lib . ex_sql ( dbname , " SELECT * FROM expedition " )
totals = sauveurs + bateaux + expeditions
2021-12-03 03:28:53 +01:00
for i in totals :
i [ " score " ] = 0
for key in i . keys ( ) :
if key != " score " :
r = 1
if key == " nom " :
r = 3
2021-12-03 04:45:58 +01:00
i [ " score " ] + = SequenceMatcher ( None , texte . lower ( ) , str ( i [ key ] ) . lower ( ) ) . ratio ( ) * r
print ( i [ " nom " ] , i [ " score " ] )
2021-12-03 03:28:53 +01:00
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 " ] )
2021-12-03 06:12:33 +01:00
n = " Bateau "
2021-12-03 03:28:53 +01:00
elif " personal_id " in i . keys ( ) :
r [ " lien " ] = " /sauveteurs/ " + str ( i [ " personal_id " ] )
2021-12-03 06:12:33 +01:00
n = " Sauveteurs "
elif " expedition_id " in i . keys ( ) :
r [ " lien " ] = " /expeditions/ " + str ( i [ " expedition_id " ] )
n = " Expeditions "
r [ " nom " ] = i [ " nom " ] + " - " + n
2021-12-03 03:28:53 +01:00
results . append ( r )
return render_template ( " searchresults.html " , liste = results )