-
Notifications
You must be signed in to change notification settings - Fork 0
/
car_engine.py
111 lines (85 loc) · 4.43 KB
/
car_engine.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
from flask import render_template, request, redirect, url_for, flash, Blueprint, session
from db import db,cursor
import mysql.connector
from auth import login_required
manage_car_engine = Blueprint('manage_car_engine',__name__)
# ------------------------------------- Display (select query) Car Engine ---------------------------------------------------
# Route to display all Car Engines
@manage_car_engine.route('/manage_car_engine')
def carengine_table():
cursor.execute("SELECT EngineID,EngineName FROM CarEngine")
data = cursor.fetchall()
return render_template('view/carEngine.html', data=data, )
# ------------------------------------ Add/Insert Car Engine ---------------------------------------------------
# Route to add a new Car Engine
@manage_car_engine.route('/manage_car_engine/add', methods=['GET', 'POST'])
@login_required
def add_carengine():
if request.method == 'POST':
# engine_id = request.form['engine_id']
engine_name = request.form['engine_name']
try:
cursor.execute("INSERT INTO CarEngine (EngineName) VALUES (%s)", (engine_name,))
db.commit()
flash('Car Engine added successfully', 'success')
return redirect(url_for('manage_car_engine.carengine_table'))
except mysql.connector.IntegrityError as e:
db.rollback()
return f'Error adding Car Engine: {e}', 'danger'
return render_template('add/add_carengine.html')
# ------------------------------------ Update/Edit Car Engine ---------------------------------------------------
# Route to edit a Car Engine
@manage_car_engine.route('/manage_car_engine/edit/<int:engine_id>', methods=['GET', 'POST'])
@login_required
def edit_car_engine(engine_id):
if request.method == 'POST':
new_engine_name = request.form['new_engine_name']
try:
update_query = "UPDATE CarEngine SET EngineName = %s WHERE EngineID = %s"
cursor.execute(update_query, (new_engine_name,engine_id))
db.commit()
flash('Car Engine updated successfully', 'success')
# return render_template('success.html')
return redirect(url_for('manage_car_engine.carengine_table'))
except mysql.connector.Error as e:
db.rollback()
flash(f'Error updating Car Engine : {e}', 'danger')
# fetch car color to edit
fetch_query = "Select EngineID,EngineName from CarEngine where EngineID = %s"
cursor.execute(fetch_query,(engine_id,))
engine_data = cursor.fetchone()
if engine_data is None:
flash('Car Engine not found','danger')
return redirect(url_for('manage_car_engine.carengine_table'))
return render_template('update/edit_carengine.html', engine_data = engine_data)
# --------------------------- delete Car Engine ---------------------------------------------------
# Route for deleting a Car Engine
@manage_car_engine.route('/manage_car_engine/delete/<int:engine_id>', methods=['GET', 'POST'])
@login_required
def delete_car_engine(engine_id):
if request.method == 'POST':
if 'confirmation' in request.form:
confirmation = request.form['confirmation']
if confirmation.lower() == 'delete':
try:
# query to delete the engine
delete_query = "DELETE FROM CarEngine WHERE EngineID = %s"
cursor.execute(delete_query, (engine_id,))
db.commit()
flash(f"Car Engine with EngineID: {engine_id} and associated cars deleted successfully", 'success')
return redirect(url_for('manage_car_engine.carengine_table'))
except mysql.connector.Error as e:
db.rollback()
flash(f'Error deleting Car Engine: {e}', 'danger')
else:
flash('Deletion not confirmed. Please type "delete" to confirm.', 'warning')
else:
flash('Invalid request. Confirmation required to delete.', 'warning')
# Fetch car engine to delete
fetch_query = "SELECT EngineID, EngineName FROM CarEngine WHERE EngineID = %s"
cursor.execute(fetch_query, (engine_id,))
engine_data = cursor.fetchone()
if engine_data is None:
flash('Car Engine not found', 'danger')
return redirect(url_for('manage_car_engine.carengine_table'))
return render_template('delete/delete_carengine.html', engine_data=engine_data)