-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_app.py
More file actions
265 lines (200 loc) · 8.44 KB
/
web_app.py
File metadata and controls
265 lines (200 loc) · 8.44 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import streamlit as st
import sqlite3
import pandas as pd
import bcrypt
DB_NAME = "database.db"
# ---------------- DATABASE ----------------
def get_connection():
return sqlite3.connect(DB_NAME)
# ---------------- PASSWORD ----------------
def hash_password(password):
return bcrypt.hashpw(password.encode(), bcrypt.gensalt())
def verify_password(password, hashed):
return bcrypt.checkpw(password.encode(), hashed)
# ---------------- SESSION INIT ----------------
if "logged_in" not in st.session_state:
st.session_state.logged_in = False
st.session_state.username = None
st.session_state.role = None
st.session_state.user_id = None
# ---------------- PAGE CONFIG ----------------
st.set_page_config(page_title="IT Helpdesk Pro", page_icon="🛠️", layout="wide")
st.title("🛠️ IT Helpdesk Management System")
st.markdown("---")
# =========================================================
# ================= AUTH SECTION ==========================
# =========================================================
if not st.session_state.logged_in:
tab1, tab2 = st.tabs(["🔐 Login", "📝 Register"])
# ---------------- LOGIN ----------------
with tab1:
st.subheader("Login")
username = st.text_input("Username", key="login_user")
password = st.text_input("Password", type="password", key="login_pass")
if st.button("Login"):
conn = get_connection()
cursor = conn.cursor()
cursor.execute(
"SELECT user_id, password, role FROM users WHERE username=?",
(username,)
)
user = cursor.fetchone()
conn.close()
if user:
user_id, stored_password, role = user
if verify_password(password, stored_password):
st.session_state.logged_in = True
st.session_state.username = username
st.session_state.role = role
st.session_state.user_id = user_id
st.success("Login Successful")
st.rerun()
else:
st.error("Invalid Credentials")
else:
st.error("User Not Found")
# ---------------- REGISTER ----------------
with tab2:
st.subheader("Register New User")
new_user = st.text_input("New Username")
new_pass = st.text_input("New Password", type="password")
role = st.selectbox("Role", ["Employee", "Admin"])
if st.button("Register"):
if not new_user or not new_pass:
st.warning("All fields required")
else:
hashed = hash_password(new_pass)
conn = get_connection()
cursor = conn.cursor()
try:
cursor.execute(
"INSERT INTO users (username, password, role) VALUES (?, ?, ?)",
(new_user, hashed, role)
)
conn.commit()
st.success("User Registered Successfully")
except sqlite3.IntegrityError:
st.error("Username already exists")
finally:
conn.close()
# =========================================================
# ================= AFTER LOGIN ===========================
# =========================================================
else:
st.success(f"Welcome {st.session_state.username} ({st.session_state.role})")
# =====================================================
# ================= EMPLOYEE PANEL ====================
# =====================================================
if st.session_state.role == "Employee":
menu = st.sidebar.radio(
"Employee Menu",
["Raise Ticket", "View My Tickets", "Logout"]
)
if menu == "Raise Ticket":
st.subheader("🎫 Raise Ticket")
category = st.selectbox("Category", ["Hardware", "Software", "Network"])
description = st.text_area("Description")
priority = st.selectbox("Priority", ["Low", "Medium", "High"])
if st.button("Submit Ticket"):
if description.strip() == "":
st.warning("Description required")
else:
conn = get_connection()
cursor = conn.cursor()
cursor.execute("""
INSERT INTO tickets (user_id, category, description, priority, status)
VALUES (?, ?, ?, ?, 'Open')
""", (
st.session_state.user_id,
category,
description,
priority
))
conn.commit()
conn.close()
st.success("Ticket Created Successfully")
elif menu == "View My Tickets":
st.subheader("📋 My Tickets")
conn = get_connection()
df = pd.read_sql_query("""
SELECT ticket_id, category, description, priority, status
FROM tickets WHERE user_id=?
""", conn, params=(st.session_state.user_id,))
conn.close()
if df.empty:
st.info("No Tickets Found")
else:
st.dataframe(df, use_container_width=True)
elif menu == "Logout":
st.session_state.clear()
st.rerun()
# =====================================================
# ================= ADMIN PANEL =======================
# =====================================================
else:
menu = st.sidebar.radio(
"Admin Menu",
["View All Tickets", "Update Status", "Statistics", "Export CSV", "Logout"]
)
if menu == "View All Tickets":
st.subheader("📊 All Tickets")
conn = get_connection()
df = pd.read_sql_query("SELECT * FROM tickets", conn)
conn.close()
if df.empty:
st.info("No Tickets Available")
else:
st.dataframe(df, use_container_width=True)
elif menu == "Update Status":
st.subheader("🔄 Update Ticket Status")
ticket_id = st.number_input("Ticket ID", min_value=1)
new_status = st.selectbox("New Status", ["Open", "In Progress", "Closed"])
if st.button("Update"):
conn = get_connection()
cursor = conn.cursor()
cursor.execute(
"UPDATE tickets SET status=? WHERE ticket_id=?",
(new_status, ticket_id)
)
conn.commit()
if cursor.rowcount == 0:
st.error("Ticket Not Found")
else:
st.success("Ticket Updated")
conn.close()
elif menu == "Statistics":
st.subheader("📈 Ticket Statistics")
conn = get_connection()
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM tickets")
total = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM tickets WHERE status='Open'")
open_count = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM tickets WHERE status='In Progress'")
progress_count = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM tickets WHERE status='Closed'")
closed_count = cursor.fetchone()[0]
conn.close()
col1, col2, col3, col4 = st.columns(4)
col1.metric("Total", total)
col2.metric("Open", open_count)
col3.metric("In Progress", progress_count)
col4.metric("Closed", closed_count)
elif menu == "Export CSV":
st.subheader("⬇ Export Tickets")
conn = get_connection()
df = pd.read_sql_query("SELECT * FROM tickets", conn)
conn.close()
if df.empty:
st.warning("No Data Available")
else:
csv = df.to_csv(index=False).encode("utf-8")
st.download_button(
"Download Report",
csv,
"tickets_report.csv",
"text/csv"
)
elif menu == "Logout":
st.session_state.clear()
st.rerun()