-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMechanicGUI.py
91 lines (70 loc) · 2.88 KB
/
MechanicGUI.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
import streamlit as st
import psycopg2
import pandas as pd
from config import Config
def MechanicMain():
user_id = st.session_state["user_id"]
db_params = {
"host": Config.HOST,
"database": Config.DATABASE,
"user": Config.USER,
"password": Config.PASSWORD
}
try:
connection = psycopg2.connect(**db_params)
except Exception as e:
st.error(f"Error: Unable to connect to the database. {e}")
st.stop()
st.title("Mechanic View")
try:
with connection.cursor() as cursor:
cursor.execute("SELECT carunchecked.ucid, carunchecked.description, carunchecked.model, carunchecked.year, carunchecked.status FROM brokencar JOIN carunchecked on brokencar.UCID = carunchecked.UCID;")
data = cursor.fetchall()
colnames = [desc[0] for desc in cursor.description]
except Exception as e:
st.error(f"Error: Unable to fetch data from the database. {e}")
st.stop()
df = pd.DataFrame(data, columns=colnames)
df['carPart'] = ''
df['isFixable'] = False
edited_df = st.data_editor(df, num_rows="dynamic")
submitted = st.button("Submit", type="secondary")
if submitted:
try:
with connection.cursor() as cursor:
for index, row in edited_df.iterrows():
if row['isFixable']:
cursor.execute(f"""
INSERT INTO greatcar (ucid)
VALUES ( {int(row['ucid'])} );
""")
cursor.execute(
"""
DELETE FROM brokencar
WHERE ucid = %s;
""",
(int(row['ucid']),)
)
else:
st.write('car part', row["carPart"])
cursor.execute(f"""
INSERT INTO CarParts (cid, mechanicid, partname)
VALUES ( {int(row['ucid'])}, {int(user_id)}, 'To be updated...' );
""")
cursor.execute(
"""
DELETE FROM brokencar
WHERE ucid = %s;
""",
(int(row['ucid']),)
)
connection.commit()
except Exception as e:
st.error(f"Error: Unable to update the database. {e}")
finally:
print()
back = st.button(":back:", type="secondary")
if back:
st.session_state["authenticated"] = False
st.session_state["username"] = None
st.rerun()