Hadeeratef91 commited on
Commit
fd9223f
·
verified ·
1 Parent(s): 9bc3746

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -116
app.py CHANGED
@@ -22,7 +22,7 @@ def init_db():
22
  password TEXT,
23
  role TEXT
24
  )''')
25
-
26
  conn.execute('''CREATE TABLE IF NOT EXISTS complaints (
27
  id INTEGER PRIMARY KEY AUTOINCREMENT,
28
  complaint_id TEXT UNIQUE,
@@ -58,7 +58,7 @@ def login_page():
58
  conn = get_db_connection()
59
  user = conn.execute("SELECT * FROM users WHERE username = ?", (username,)).fetchone()
60
  conn.close()
61
-
62
  if user and bcrypt.verify(password, user["password"]):
63
  st.session_state.auth = {
64
  "is_logged_in": True,
@@ -70,69 +70,63 @@ def login_page():
70
  st.error("❌ كلمة المرور غير صحيحة")
71
  else:
72
  st.warning("❌ اسم المستخدم غير موجود")
 
 
73
  def receiver_page():
74
  st.title("📬 إدارة الشكاوى")
75
-
76
  conn = get_db_connection()
77
  try:
78
- # حساب عدد الشكاوى المعلقة
79
  pending_count = conn.execute(
80
  "SELECT COUNT(*) FROM complaints WHERE status = 'Pending'"
81
  ).fetchone()[0]
82
-
83
  if pending_count > 0:
84
  st.warning(f"🚨 يوجد {pending_count} شكوى جديدة لم يتم التعامل معها!")
85
-
86
- # جلب جميع الشكاوى وتحويلها إلى قواميس
87
  complaints = conn.execute(
88
  "SELECT * FROM complaints ORDER BY id DESC"
89
  ).fetchall()
90
-
91
- # تحويل كل صف إلى قاموس قابل للتخزين
92
  complaints_list = [
93
  {
94
  "complaint_id": row["complaint_id"],
95
  "username": row["username"],
96
  "complaint_text": row["complaint_text"],
97
  "status": row["status"],
98
- "response": row["response"] or "", # ضمان عدم وجود None
99
  "timestamp": row["timestamp"]
100
  }
101
  for row in complaints
102
  ]
103
-
104
  if not complaints_list:
105
  st.info("لا توجد شكاوى حالياً.")
106
  else:
107
  st.subheader("📋 جميع الشكاوى")
108
-
109
- # عرض قائمة اختيار الشكاوى
110
  selected_complaint = st.selectbox(
111
  "اختر شكوى للتعامل معها",
112
  complaints_list,
113
  format_func=lambda x: f"{x['complaint_id']} - {x['username']}"
114
  )
115
-
116
- # عرض تفاصيل الشكوى المحددة
117
  st.subheader(f"📨 معرف الشكوى: {selected_complaint['complaint_id']}")
118
  st.write(f"👤 مقدم الشكوى: {selected_complaint['username']}")
119
  st.write(f"📝 نص الشكوى: {selected_complaint['complaint_text']}")
120
  st.write(f"📅 وقت الإرسال: {selected_complaint['timestamp']}")
121
-
122
- # تحديث حالة الشكوى
123
  new_status = st.selectbox(
124
  "🔄 تحديث الحالة",
125
  ["Pending", "In Progress", "Resolved"],
126
  index=["Pending", "In Progress", "Resolved"].index(selected_complaint["status"])
127
  )
128
-
129
- # حقل الرد على الشكوى
130
  new_response = st.text_area(
131
  "💬 الرد على الشكوى",
132
  value=selected_complaint["response"]
133
  )
134
-
135
- # زر حفظ التعديلات
136
  if st.button("💾 حفظ التعديلات"):
137
  try:
138
  conn.execute(
@@ -141,103 +135,13 @@ def receiver_page():
141
  )
142
  conn.commit()
143
  st.success("✅ تم حفظ التعديلات بنجاح")
144
- st.rerun() # إعادة تحميل الصفحة لتحديث البيانات
145
  except sqlite3.Error as e:
146
  st.error(f"❌ حدث خطأ أثناء الحفظ: {str(e)}")
147
-
148
  except sqlite3.Error as e:
149
  st.error(f"❌ حدث خطأ في قاعدة البيانات: {str(e)}")
150
  finally:
151
- conn.close() # إغلاق الاتصال في جميع الحالات
152
- # عرض الشكاوى السابقة
153
- st.subheader("📋 الشكاوى السابقة")
154
- conn = get_db_connection()
155
- complaints = conn.execute(
156
- "SELECT * FROM complaints WHERE username = ? ORDER BY id DESC",
157
- (st.session_state.auth["username"],)
158
- ).fetchall()
159
- conn.close()
160
-
161
- for complaint in complaints:
162
- with st.expander(f"📨 {complaint['complaint_id']} | الحالة: {complaint['status']}"):
163
- st.write(f"📅 تم الإرسال في: {complaint['timestamp']}")
164
- st.write(f"📄 الشكوى: {complaint['complaint_text']}")
165
-
166
- if complaint["status"].lower() == "pending":
167
- new_text = st.text_area(
168
- "📝 تعديل الشكوى",
169
- value=complaint["complaint_text"],
170
- key=f"edit_{complaint['complaint_id']}"
171
- )
172
- if st.button("💾 حفظ التعديل", key=f"save_{complaint['complaint_id']}"):
173
- if new_text.strip():
174
- conn = get_db_connection()
175
- conn.execute(
176
- "UPDATE complaints SET complaint_text = ? WHERE complaint_id = ?",
177
- (new_text.strip(), complaint["complaint_id"])
178
- )
179
- conn.commit()
180
- conn.close()
181
- st.success("✅ تم تعديل الشكوى")
182
- st.rerun()
183
- else:
184
- st.warning("⚠️ لا يمكن حفظ شكوى فارغة")
185
- elif complaint["response"]:
186
- st.success(f"💬 الرد: {complaint['response']}")
187
-
188
- # صفحة مستقبل الشكاوى
189
- def receiver_page():
190
- st.title("📬 إدارة الشكاوى")
191
-
192
- conn = get_db_connection()
193
- pending_count = conn.execute(
194
- "SELECT COUNT(*) FROM complaints WHERE status = 'Pending'"
195
- ).fetchone()[0]
196
-
197
- if pending_count > 0:
198
- st.warning(f"🚨 يوجد {pending_count} شكوى جديدة لم يتم التعامل معها!")
199
-
200
- complaints = conn.execute(
201
- "SELECT * FROM complaints ORDER BY id DESC"
202
- ).fetchall()
203
- conn.close()
204
-
205
- if not complaints:
206
- st.info("لا توجد شكاوى حالياً.")
207
- else:
208
- st.subheader("📋 جميع الشكاوى")
209
- selected_complaint = st.selectbox(
210
- "اختر شكوى للتعامل معها",
211
- complaints,
212
- format_func=lambda x: f"{x['complaint_id']} - {x['username']}"
213
- )
214
-
215
- st.subheader(f"📨 معرف الشكوى: {selected_complaint['complaint_id']}")
216
- st.write(f"👤 مقدم الشكوى: {selected_complaint['username']}")
217
- st.write(f"📝 الشكوى: {selected_complaint['complaint_text']}")
218
- st.write(f"📅 الوقت: {selected_complaint['timestamp']}")
219
-
220
- new_status = st.selectbox(
221
- "🔄 تحديث الحالة",
222
- ["Pending", "In Progress", "Resolved"],
223
- index=["Pending", "In Progress", "Resolved"].index(selected_complaint["status"])
224
- )
225
-
226
- new_response = st.text_area(
227
- "💬 الرد على الشكوى",
228
- value=selected_complaint["response"] or ""
229
- )
230
-
231
- if st.button("💾 حفظ التعديلات"):
232
- conn = get_db_connection()
233
- conn.execute(
234
- "UPDATE complaints SET status = ?, response = ? WHERE complaint_id = ?",
235
- (new_status, new_response.strip(), selected_complaint["complaint_id"])
236
- )
237
- conn.commit()
238
- conn.close()
239
- st.success("✅ تم حفظ التعديلات")
240
- st.rerun()
241
 
242
  # الواجهة الرئيسية
243
  def main():
@@ -245,7 +149,7 @@ def main():
245
  login_page()
246
  else:
247
  if st.session_state.auth["role"] == "sender":
248
- sender_page()
249
  else:
250
  receiver_page()
251
 
@@ -258,7 +162,6 @@ def main():
258
  }
259
  st.rerun()
260
 
261
- # زر تحميل قاعدة البيانات
262
  if os.path.exists("complaints_system.db"):
263
  with open("complaints_system.db", "rb") as db_file:
264
  st.download_button(
@@ -269,4 +172,4 @@ def main():
269
  )
270
 
271
  if __name__ == "__main__":
272
- main()
 
22
  password TEXT,
23
  role TEXT
24
  )''')
25
+
26
  conn.execute('''CREATE TABLE IF NOT EXISTS complaints (
27
  id INTEGER PRIMARY KEY AUTOINCREMENT,
28
  complaint_id TEXT UNIQUE,
 
58
  conn = get_db_connection()
59
  user = conn.execute("SELECT * FROM users WHERE username = ?", (username,)).fetchone()
60
  conn.close()
61
+
62
  if user and bcrypt.verify(password, user["password"]):
63
  st.session_state.auth = {
64
  "is_logged_in": True,
 
70
  st.error("❌ كلمة المرور غير صحيحة")
71
  else:
72
  st.warning("❌ اسم المستخدم غير موجود")
73
+
74
+ # صفحة مستقبل الشكاوى
75
  def receiver_page():
76
  st.title("📬 إدارة الشكاوى")
77
+
78
  conn = get_db_connection()
79
  try:
 
80
  pending_count = conn.execute(
81
  "SELECT COUNT(*) FROM complaints WHERE status = 'Pending'"
82
  ).fetchone()[0]
83
+
84
  if pending_count > 0:
85
  st.warning(f"🚨 يوجد {pending_count} شكوى جديدة لم يتم التعامل معها!")
86
+
 
87
  complaints = conn.execute(
88
  "SELECT * FROM complaints ORDER BY id DESC"
89
  ).fetchall()
90
+
 
91
  complaints_list = [
92
  {
93
  "complaint_id": row["complaint_id"],
94
  "username": row["username"],
95
  "complaint_text": row["complaint_text"],
96
  "status": row["status"],
97
+ "response": row["response"] or "",
98
  "timestamp": row["timestamp"]
99
  }
100
  for row in complaints
101
  ]
102
+
103
  if not complaints_list:
104
  st.info("لا توجد شكاوى حالياً.")
105
  else:
106
  st.subheader("📋 جميع الشكاوى")
107
+
 
108
  selected_complaint = st.selectbox(
109
  "اختر شكوى للتعامل معها",
110
  complaints_list,
111
  format_func=lambda x: f"{x['complaint_id']} - {x['username']}"
112
  )
113
+
 
114
  st.subheader(f"📨 معرف الشكوى: {selected_complaint['complaint_id']}")
115
  st.write(f"👤 مقدم الشكوى: {selected_complaint['username']}")
116
  st.write(f"📝 نص الشكوى: {selected_complaint['complaint_text']}")
117
  st.write(f"📅 وقت الإرسال: {selected_complaint['timestamp']}")
118
+
 
119
  new_status = st.selectbox(
120
  "🔄 تحديث الحالة",
121
  ["Pending", "In Progress", "Resolved"],
122
  index=["Pending", "In Progress", "Resolved"].index(selected_complaint["status"])
123
  )
124
+
 
125
  new_response = st.text_area(
126
  "💬 الرد على الشكوى",
127
  value=selected_complaint["response"]
128
  )
129
+
 
130
  if st.button("💾 حفظ التعديلات"):
131
  try:
132
  conn.execute(
 
135
  )
136
  conn.commit()
137
  st.success("✅ تم حفظ التعديلات بنجاح")
138
+ st.rerun()
139
  except sqlite3.Error as e:
140
  st.error(f"❌ حدث خطأ أثناء الحفظ: {str(e)}")
 
141
  except sqlite3.Error as e:
142
  st.error(f"❌ حدث خطأ في قاعدة البيانات: {str(e)}")
143
  finally:
144
+ conn.close()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
  # الواجهة الرئيسية
147
  def main():
 
149
  login_page()
150
  else:
151
  if st.session_state.auth["role"] == "sender":
152
+ st.warning("🛠️ صفحة المرسل لم تُنفذ بعد.")
153
  else:
154
  receiver_page()
155
 
 
162
  }
163
  st.rerun()
164
 
 
165
  if os.path.exists("complaints_system.db"):
166
  with open("complaints_system.db", "rb") as db_file:
167
  st.download_button(
 
172
  )
173
 
174
  if __name__ == "__main__":
175
+ main()