-
Notifications
You must be signed in to change notification settings - Fork 0
/
car.py
229 lines (191 loc) · 8.5 KB
/
car.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
from flask import Flask, render_template, Blueprint, request, redirect, url_for, flash, session
import mysql.connector
from db import db, cursor
from auth import login_required
from flask_paginate import Pagination
# BluePrint
manage_car = Blueprint('manage_car', __name__)
#--------------------------- Route to fetch car table --------------------------------------------------------
@manage_car.route('/manage_car')
def manage_car_table():
page = request.args.get('page', type=int, default=1)
per_page = 10 # Number of cars per page
offset = (page - 1) * per_page
cursor.execute("""
SELECT
C.CarID,
CV.VariantName,
CC.ColorName,
CAT.CategoryName,
CE.EngineName,
CM.ModelName,
C.VIN,
C.Mileage,
C.YearOfManufacture,
C.BrandCompany,
CV.VariantID,
CC.ColorID,
CAT.CategoryID,
CE.EngineID,
CM.ModelID
FROM Car AS C
JOIN CarVariant AS CV ON C.VariantID = CV.VariantID
JOIN CarColor AS CC ON C.ColorID = CC.ColorID
JOIN CarCategory AS CAT ON C.CategoryID = CAT.CategoryID
JOIN CarEngine AS CE ON C.EngineID = CE.EngineID
JOIN CarModel AS CM ON C.ModelID = CM.ModelID
ORDER BY C.CarID ASC
LIMIT %s OFFSET %s;
""", (per_page, offset))
cars = cursor.fetchall()
# has_next is a boolean variable that is being assigned the result of the comparison len(cars) == per_page.
has_next = len(cars) == per_page
pagination = Pagination(page=page, per_page=per_page)
return render_template('view/car.html', cars=cars, pagination=pagination, has_next=has_next, page=page, per_page = per_page)
# ------------------------------------ Add/Insert Car ---------------------------------------------------
# Route to add a new Car
@manage_car.route('/manage_car/add', methods=['GET', 'POST'])
@login_required
def add_car():
if request.method == 'POST':
variant_id = request.form['variant_id']
category_id = request.form['category_id']
engine_id = request.form['engine_id']
color_id = request.form['color_id']
model_id = request.form['model_id']
vin = request.form['vin']
mileage = request.form['mileage']
year_of_manufacture = request.form['year_of_manufacture']
brand_company = request.form['brand_company']
try:
cursor.execute("INSERT INTO Car (VariantID, CategoryID, EngineID, ColorID, ModelID, VIN, Mileage, YearOfManufacture, BrandCompany) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
(variant_id, category_id, engine_id, color_id, model_id, vin, mileage, year_of_manufacture, brand_company))
db.commit()
flash('Car added successfully', 'success')
return redirect(url_for('manage_car.manage_car_table')) # Redirect to the manage car page
except mysql.connector.IntegrityError as e:
db.rollback()
flash(f'Error adding Car: {e}', 'danger')
# Fetch the list of variants, categories, engines, colors, and models to populate dropdowns in the form
cursor.execute("SELECT VariantID, VariantName FROM CarVariant")
variants = cursor.fetchall()
cursor.execute("SELECT CategoryID, CategoryName FROM CarCategory")
categories = cursor.fetchall()
cursor.execute("SELECT EngineID, EngineName FROM CarEngine")
engines = cursor.fetchall()
cursor.execute("SELECT ColorID, ColorName FROM CarColor")
colors = cursor.fetchall()
cursor.execute("SELECT ModelID, ModelName FROM CarModel")
models = cursor.fetchall()
return render_template('add/add_car.html', variants=variants, categories=categories, engines=engines, colors=colors, models=models)
# ---------------------------------- Route to edit Car ----------------------------------------------------------------
# Route to edit a car record
@manage_car.route('/car/edit/<int:car_id>', methods=['GET', 'POST'])
@login_required
def edit_car(car_id):
if request.method == 'POST':
variant_id = request.form.get('variant_id')
category_id = request.form.get('category_id')
engine_id = request.form.get('engine_id')
color_id = request.form.get('color_id')
model_id = request.form.get('model_id')
mileage = request.form.get('mileage')
year_manufacture = request.form.get('year_manufacture')
brand_company = request.form.get('brand_company')
try:
update_query = """
UPDATE Car
SET VariantID = %s, CategoryID = %s, EngineID = %s,
ColorID = %s, ModelID = %s,
Mileage = %s, YearOfManufacture = %s, BrandCompany = %s
WHERE CarID = %s
"""
cursor.execute(update_query, (variant_id, category_id, engine_id, color_id, model_id,
mileage, year_manufacture, brand_company, car_id))
db.commit()
flash('Car updated successfully', 'success')
# return render_template('success.html')
return redirect(url_for('manage_car.manage_car_table')) # Redirect to the manage car page
except mysql.connector.Error as e:
db.rollback()
flash(f'Error updating car: {e}', 'danger')
# Fetch car data for editing and dropdowns
fetch_query = """
SELECT
C.CarID,
CV.VariantId,
CAT.CategoryID,
CE.EngineID,
CC.ColorId,
CM.ModelId,
C.Mileage,
C.YearOfManufacture,
C.BrandCompany
FROM Car AS C
JOIN CarVariant AS CV ON C.VariantID = CV.VariantID
JOIN CarColor AS CC ON C.ColorID = CC.ColorID
JOIN CarCategory AS CAT ON C.CategoryID = CAT.CategoryID
JOIN CarEngine AS CE ON C.EngineID = CE.EngineID
JOIN CarModel AS CM ON C.ModelID = CM.ModelID
WHERE C.CarID = %s
"""
cursor.execute(fetch_query, (car_id,))
car_data = cursor.fetchone()
if car_data is None:
flash('Car not found', 'danger')
return redirect(url_for('manage_car.manage_car_table')) # Redirect to the manage car page
# Fetch data for dropdowns
cursor.execute("SELECT * FROM CarVariant")
variants = cursor.fetchall()
# print("variants = ", variants)
cursor.execute("SELECT * FROM CarCategory")
categories = cursor.fetchall()
cursor.execute("SELECT * FROM CarEngine")
engines = cursor.fetchall()
cursor.execute("SELECT * FROM CarColor")
colors = cursor.fetchall()
cursor.execute("SELECT * FROM CarModel")
models = cursor.fetchall()
# Pass the data to the HTML template
return render_template('update/edit_car.html', car_data=car_data, variants=variants, categories=categories, engines=engines, colors=colors, models=models)
# --------------------------------- Route to delete Car ------------------------------------------------------
@manage_car.route('/car/delete/<int:car_id>', methods=['GET', 'POST'])
@login_required
def delete_car(car_id):
if request.method == 'POST':
try:
delete_query = "DELETE FROM Car WHERE CarID = %s"
cursor.execute(delete_query, (car_id,))
db.commit()
flash(f'Car with CarID: { car_id } deleted successfully', 'success')
return redirect(url_for('manage_car.manage_car_table')) # Redirect to the manage car page
# return render_template('success.html')
except mysql.connector.Error as e:
db.rollback()
flash(f'Error deleting car: {e}', 'danger')
# Fetch car data for confirmation
fetch_query = """
SELECT
C.CarID,
CV.VariantName,
CAT.CategoryName,
CE.EngineName,
CC.ColorName,
CM.ModelName,
C.Mileage,
C.YearOfManufacture,
C.BrandCompany
FROM Car AS C
JOIN CarVariant AS CV ON C.VariantID = CV.VariantID
JOIN CarColor AS CC ON C.ColorID = CC.ColorID
JOIN CarCategory AS CAT ON C.CategoryID = CAT.CategoryID
JOIN CarEngine AS CE ON C.EngineID = CE.EngineID
JOIN CarModel AS CM ON C.ModelID = CM.ModelID
WHERE C.CarID = %s
"""
cursor.execute(fetch_query, (car_id,))
car_data = cursor.fetchone()
if car_data is None:
flash('Car not found', 'danger')
return redirect(url_for('manage_car.manage_car_table')) # Redirect to the manage car page
return render_template('delete/delete_car.html', car_data=car_data)