forked from MysteriousBaboon/Flask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
118 lines (93 loc) · 3.46 KB
/
app.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from flask import Flask, render_template, request, redirect
import psycopg2
import os
from dotenv import load_dotenv
app = Flask(__name__)
load_dotenv()
DATABASE_HOST = os.environ.get('DATABASE_HOST')
DATABASE_PORT = os.environ.get('DATABASE_PORT')
DATABASE_NAME = os.environ.get('DATABASE_NAME')
DATABASE_USER = os.environ.get('DATABASE_USER')
DATABASE_PASSWORD = os.environ.get('DATABASE_PASSWORD')
def create_connection():
connection = psycopg2.connect(
host=DATABASE_HOST,
port=DATABASE_PORT,
database=DATABASE_NAME,
user=DATABASE_USER,
password=DATABASE_PASSWORD
)
return connection
@app.route('/')
def index():
connection = create_connection()
cursor = connection.cursor()
# Récupérer et afficher toutes les données de la table app_student
retrieve_app_student_query = '''
SELECT * FROM app_student;
'''
cursor.execute(retrieve_app_student_query)
app_student_data = cursor.fetchall()
cursor.close()
connection.close()
return render_template('index.html', app_student_data=app_student_data)
@app.route('/add_student', methods=['POST'])
def add_student():
connection = create_connection()
cursor = connection.cursor()
# Récupérer les données du formulaire
first_name = request.form['first_name']
last_name = request.form['last_name']
score = request.form['score']
# Insérer les nouvelles données dans la table app_student
insert_student_query = '''
INSERT INTO app_student (first_name, last_name, score) VALUES (%s, %s, %s);
'''
cursor.execute(insert_student_query, (first_name, last_name, score))
connection.commit()
cursor.close()
connection.close()
return redirect('/')
@app.route('/delete_student/<int:student_id>')
def delete_student(student_id):
connection = create_connection()
cursor = connection.cursor()
# Supprimer l'étudiant spécifié par son ID
delete_student_query = '''
DELETE FROM app_student WHERE id = %s;
'''
cursor.execute(delete_student_query, (student_id,))
connection.commit()
cursor.close()
connection.close()
return redirect('/')
@app.route('/edit_student/<int:student_id>', methods=['GET', 'POST'])
def edit_student(student_id):
connection = create_connection()
cursor = connection.cursor()
if request.method == 'GET':
# Récupérer les données de l'étudiant pour pré-remplir le formulaire
retrieve_student_query = '''
SELECT * FROM app_student WHERE id = %s;
'''
cursor.execute(retrieve_student_query, (student_id,))
student_data = cursor.fetchone()
cursor.close()
connection.close()
return render_template('edit_student.html', student_data=student_data)
elif request.method == 'POST':
# Récupérer les nouvelles données du formulaire
new_first_name = request.form['first_name']
new_last_name = request.form['last_name']
new_score = request.form['score']
# Mettre à jour les données de l'étudiant
update_student_query = '''
UPDATE app_student SET first_name = %s, last_name = %s, score = %s WHERE id = %s;
'''
cursor.execute(update_student_query, (new_first_name, new_last_name, new_score, student_id))
connection.commit()
cursor.close()
connection.close()
return redirect('/')
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000, debug=False)