-
Notifications
You must be signed in to change notification settings - Fork 0
/
car_variant.py
163 lines (137 loc) · 6.68 KB
/
car_variant.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
from flask import render_template, Blueprint, request, redirect, url_for, flash, session
import mysql.connector
from db import db, cursor
from auth import login_required
# BluePrint
manage_car_variant = Blueprint('manage_car_variant', __name__)
# ------------------------------------- Display (select query) Car Model ---------------------------------------------------
# Route to display all Car Variants with details from related tables
@manage_car_variant.route('/manage_car_variant')
def carvariant_table():
query = """
SELECT cv.VariantID,
m.ModelName,
c.ColorName,
cc.CategoryName,
cv.VariantName,
cv.Mileage,
cv.Price
FROM CarVariant cv
INNER JOIN CarModel m ON cv.ModelID = m.ModelID
INNER JOIN CarColor c ON cv.ColorID = c.ColorID
INNER JOIN CarCategory cc ON cv.CategoryID = cc.CategoryID ORDER BY cv.VariantID ASC
"""
cursor.execute(query)
data = cursor.fetchall()
return render_template('view/car_variant.html', data=data)
# ------------------------------------ Add/Insert Car Variant ---------------------------------------------------
# Route to add a new Car Variant
@manage_car_variant.route('/manage_car_variant/add', methods=['GET', 'POST'])
@login_required
def add_carvariant():
if request.method == 'POST':
model_id = request.form['model_id']
color_id = request.form['color_id']
category_id = request.form['category_id']
variant_name = request.form['variant_name']
mileage = request.form['mileage']
price = request.form['price']
if price == '':
price = None
if mileage == '':
mileage = 24
cursor.execute("INSERT INTO CarVariant (ModelID, ColorID, CategoryID, VariantName, Mileage, Price) VALUES (%s, %s, %s, %s, %s, %s)",
(model_id, color_id, category_id, variant_name, mileage,price))
db.commit()
flash('Car Variant added successfully', 'success')
return redirect('/manage_car_variant')
# Fetch the list of models, colors, and categories to populate dropdowns in the form
cursor.execute("SELECT ModelID, ModelName FROM CarModel")
models = cursor.fetchall()
cursor.execute("SELECT ColorID, ColorName FROM CarColor")
colors = cursor.fetchall()
cursor.execute("SELECT CategoryID, CategoryName FROM CarCategory")
categories = cursor.fetchall()
return render_template('add/add_carvariant.html', models=models, colors=colors, categories=categories)
# ---------------------------------- Route to edit Car Variant ----------------------------------------------------------------
# Route to edit a Car Variant
@manage_car_variant.route('/carvariant/edit/<int:variant_id>', methods=['GET', 'POST'])
@login_required
def edit_carvariant(variant_id):
if request.method == 'POST':
# Retrieve the data from the form
new_variant_name = request.form['new_variant_name']
new_model_id = request.form['new_model_id']
new_color_id = request.form['new_color_id']
new_category_id = request.form['new_category_id']
new_mileage = request.form['new_mileage']
new_price = request.form['new_price']
try:
# Update the Car Variant in the database
update_query = """
UPDATE CarVariant
SET VariantName = %s, ModelID = %s, ColorID = %s, CategoryID = %s,
Mileage = %s, Price = %s
WHERE VariantID = %s
"""
cursor.execute(update_query, (new_variant_name, new_model_id, new_color_id, new_category_id, new_mileage, new_price, variant_id))
db.commit()
flash('Car Variant updated successfully', 'success')
# return render_template('success.html') # Redirect to the Car Variant list after editing
return redirect(url_for('manage_car_variant.carvariant_table'))
except mysql.connector.Error as e:
db.rollback()
flash(f'Error updating Car Variant: {e}', 'danger')
# Fetch Car Variant data for editing and related dropdowns
fetch_query = """
SELECT
cv.VariantID,cv.ModelID, cv.ColorID, cv.CategoryID,cv.VariantName,
cv.Mileage, cv.Price
FROM CarVariant cv
WHERE cv.VariantID = %s
"""
cursor.execute(fetch_query, (variant_id,))
variant_data = cursor.fetchone()
if variant_data is None:
flash('Car Variant not found', 'danger')
return redirect(url_for('manage_car_variant.carvariant_table')) # Redirect to manage variants page
# Fetch related data for dropdowns
cursor.execute("SELECT ModelID, ModelName FROM CarModel")
models = cursor.fetchall()
cursor.execute("SELECT ColorID, ColorName FROM CarColor")
colors = cursor.fetchall()
cursor.execute("SELECT CategoryID, CategoryName FROM CarCategory")
categories = cursor.fetchall()
return render_template('update/edit_car_variant.html', variant_data=variant_data, models=models, colors=colors, categories=categories)
# --------------------------------- Route to delete Car Variant ---------------------------------------------
@manage_car_variant.route('/carvariant/delete/<int:variant_id>', methods=['GET', 'POST'])
@login_required
def delete_carvariant(variant_id):
if request.method == 'POST':
try:
delete_query = "DELETE FROM CarVariant WHERE VariantID = %s"
cursor.execute(delete_query, (variant_id,))
db.commit()
flash(f"Car Variant with VariantID: {variant_id} deleted successfully", 'success')
# return render_template('success.html')
return redirect(url_for('manage_car_variant.carvariant_table'))
except mysql.connector.Error as e:
db.rollback()
flash(f'Error deleting Car Variant: {e}', 'danger')
# Fetch car variant data for confirmation
fetch_query = """
SELECT
cv.VariantID,m.ModelName, c.ColorName, cc.CategoryName,
cv.VariantName, cv.Mileage, cv.Price
FROM CarVariant cv
INNER JOIN CarModel m ON cv.ModelID = m.ModelID
INNER JOIN CarColor c ON cv.ColorID = c.ColorID
INNER JOIN CarCategory cc ON cv.CategoryID = cc.CategoryID
WHERE VariantID = %s
"""
cursor.execute(fetch_query, (variant_id,))
variant_data = cursor.fetchone()
if variant_data is None:
flash("Car Variant not found", 'danger')
return redirect(url_for('manage_car_variant.carvariant_table'))
return render_template('delete/delete_car_variant.html', variant_data=variant_data)