-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGestionCaracteristiques.py
85 lines (77 loc) · 4.15 KB
/
GestionCaracteristiques.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import pymysql
import pymysql.cursors
from ConnectDB import DbUtil
from constants import RANG
from flask import Blueprint, render_template, redirect, request, session
routeCaracteristiques = Blueprint('caracteristiques', __name__, template_folder='templates')
# Liste toutes les caractéristiques ainsi que les valeurs qu'elles peuvent prendre
@routeCaracteristiques.route('/list')
def caracteristiques():
if 'ID_Client' in session:
if session['Rang'] >= RANG['VENDEUR']:
try:
connection = DbUtil.get_connection()
cursor = connection.cursor()
cursor.execute("SELECT ID_Caracteristique, Nom_Cara FROM t_caracteristique ORDER BY Nom_Cara")
data_caracteristiques = cursor.fetchall()
for data_caracteristique in data_caracteristiques:
cursor.execute("SELECT Valeur FROM t_valeur_caracteristique WHERE FK_Caracteristique = %s ORDER BY Valeur", data_caracteristique['ID_Caracteristique'])
data_caracteristique['Valeurs'] = cursor.fetchall()
except (pymysql.err.OperationalError, pymysql.ProgrammingError, pymysql.InternalError, pymysql.IntegrityError, TypeError) as error:
print("Problème avec la BD ! : %s", error)
connection.rollback()
finally:
connection.close()
cursor.close()
return render_template('caracteristiques/caracteristiques.html', caracteristiques=data_caracteristiques)
return render_template('erreurs/403.html'), 403
# Modification d'une caractéristique ainsi que de ses valeurs
@routeCaracteristiques.route('/edit/<int:id_caracteristique>', methods=['GET', 'POST'])
def caracteristique(id_caracteristique = 0):
if 'ID_Client' in session:
if session['Rang'] >= RANG['VENDEUR']:
try:
connection = DbUtil.get_connection()
cursor = connection.cursor()
if request.method == 'POST':
if id_caracteristique == 0: #Si nouvelle cara alors insert
cursor.execute("INSERT INTO t_caracteristique(Nom_Cara) VALUES(%s)", (request.form['nom_cara']))
connection.commit()
iserted = True
id_caracteristique = cursor.lastrowid
else: #Si cara existante alors update
cursor.execute("UPDATE t_caracteristique SET Nom_Cara = %s WHERE ID_Caracteristique = %s", (request.form['nom_cara'], id_caracteristique))
connection.commit()
iserted = False
req = request.form
# Boucle for sur tous les paramètre POST
for key, value in req.items():
if key.startswith("valeur"): # Si la paramètre commence par "valeur"
id_valeur = key.split("valeur")[1] # On récupère le numéro qui suit valeur
if value:
if id_valeur == "0": # 0 si c'est une nouvelle valeur
cursor.execute("INSERT INTO t_valeur_caracteristique(Valeur, FK_Caracteristique) VALUES(%s, %s)", (value, id_caracteristique))
connection.commit()
else: # Sinon, on update dans la db
cursor.execute("UPDATE t_valeur_caracteristique SET Valeur = %s, FK_Caracteristique = %s WHERE ID_Valeur_Caracteristique = %s", (value, id_caracteristique, id_valeur))
connection.commit()
if key.startswith("delete"): # Même chose si ça commence par "delete"
id_valeur = key.split("delete")[1] # On récupère l'id puis on l'efface
cursor.execute("DELETE FROM t_valeur_caracteristique WHERE ID_Valeur_Caracteristique = %s", id_valeur)
connection.commit()
if iserted:
connection.close()
cursor.close()
return redirect('/caracteristiques/edit/'+str(id_caracteristique))
cursor.execute("SELECT Nom_Cara FROM t_caracteristique WHERE ID_Caracteristique = %s", id_caracteristique)
data_caracteristiques = cursor.fetchone()
cursor.execute("SELECT ID_Valeur_Caracteristique, Valeur FROM t_valeur_caracteristique WHERE FK_Caracteristique = %s ORDER BY Valeur", id_caracteristique)
data_valeurs = cursor.fetchall()
except (pymysql.err.OperationalError, pymysql.ProgrammingError, pymysql.InternalError, pymysql.IntegrityError, TypeError) as error:
print("Problème avec la BD ! : %s", error)
connection.rollback()
finally:
connection.close()
cursor.close()
return render_template('caracteristiques/caracteristique.html', caracteristique=data_caracteristiques, valeurs=data_valeurs)
return render_template('403.html'), 403