Spaces:
Sleeping
Sleeping
Update app.py
Browse files2 unmodified lines3456789101112131415168 unmodified lines2526272829303132333435363738394041425 unmodified lines4849505152535455565758593 unmodified lines636465666768692 unmodified lines727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301 unmodified line1321331341351361371382 unmodified lines1411421431441451461471481491 unmodified line1511521531542 unmodified linesimport osimport sqlite3from datetime import datetimefrom passlib.hash import bcrypt # ← بديل bcryptst.set_page_config(page_title="نظام إدارة الشكاوى", layout="centered")# الاتصال بقاعدة البيانات وإنشاء الجداول إذا لم تكن موجودةconn = sqlite3.connect("complaints_system (1).db", check_same_thread=False)c = conn.cursor()c.execute('''CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT,username TEXT UNIQUE,8 unmodified linescomplaint_text TEXT,status TEXT,response TEXT,timestamp TEXT)''')conn.commit()# تهيئة session stateif "is_logged_in" not in st.session_state:st.session_state.is_logged_in = Falseif "user_type" not in st.session_state:st.session_state.user_type = Noneif "username" not in st.session_state:st.session_state.username = ""# تسجيل الدخولif not st.session_state.is_logged_in:5 unmodified linesc.execute("SELECT * FROM users WHERE username=?", (username,))user = c.fetchone()if user:stored_hashed_password = user[2]role = user[3]if bcrypt.verify(password, stored_hashed_password): # ✅ التحقق بكلمة السرst.session_state.is_logged_in = Truest.session_state.username = usernamest.session_state.user_type = "sender" if role == "sender" else "receiver"st.rerun()else:st.error("❌ كلمة المرور غير صحيحة.")3 unmodified lines# مقدم الشكوىelif st.session_state.user_type == "sender":st.title("📨 إرسال شكوى جديدة")complaint_text = st.text_area("📝 وصف الشكوى")if st.button("📤 إرسال الشكوى"):c.execute("SELECT * FROM complaints WHERE username=?", (st.session_state.username,))2 unmodified linesnext_id = len(user_complaints) + 1complaint_id = f"{prefix}_{next_id:07}"c.execute("SELECT * FROM complaints WHERE complaint_id=?", (complaint_id,))if c.fetchone():st.warning("⚠️ تم إرسال هذه الشكوى مسبقًا بناءً على المعرف!")elif complaint_text.strip() == "":st.warning("⚠️ الرجاء إدخال نص الشكوى.")else:timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")c.execute("INSERT INTO complaints (complaint_id, username, complaint_text, status, response, timestamp) VALUES (?, ?, ?, ?, ?, ?)",(complaint_id, st.session_state.username, complaint_text.strip(), "Pending", "", timestamp))conn.commit()st.success("✅ تم تقديم الشكوى بنجاح")st.rerun()st.subheader("📋 الشكاوى السابقة")c.execute("SELECT * FROM complaints WHERE username=? ORDER BY id DESC", (st.session_state.username,))complaints = c.fetchall()for row in complaints:comp_id, username, text, status, response, timestamp = row[1], row[2], row[3], row[4], row[5], row[6]with st.expander(f"📨 {comp_id} | {status}"):if status.lower() == "pending" and (not response or response.strip() == ""):new_text = st.text_area("📝 تعديل الشكوى", value=text, key=f"edit_{comp_id}")if st.button("💾 حفظ التعديل", key=f"save_{comp_id}"):c.execute("UPDATE complaints SET complaint_text=? WHERE complaint_id=?", (new_text.strip(), comp_id))conn.commit()st.success("✅ تم تعديل الشكوى بنجاح")st.rerun()else:st.info(f"📢 تم تحديث حالة الشكوى إلى: {status}")if response and response.strip():st.success(f"💬 الرد: {response}")# مستقبل الشكوىelif st.session_state.user_type == "receiver":st.title("📬 إدارة الشكاوى")c.execute("SELECT * FROM complaints")complaints = c.fetchall()if not complaints:st.info("لا توجد شكاوى حالياً للتعامل معها.")else:st.subheader("📋 جميع الشكاوى")options = {f"{row[1]} - {row[2]}": row for row in complaints}selected = st.selectbox("اختر شكوى للتعامل معها", list(options.keys()))selected_row = options[selected]st.write(f"### ✉️ شكوى: {selected_row[1]}")st.subheader(f"📨 معرف الشكوى: {selected_row[1]}")st.write(f"**مقدم الشكوى:** {selected_row[2]}")st.write(f"**الوصف:** {selected_row[3]}")new_status = st.selectbox("🔄 تحديث الحالة", ["Pending", "In Progress", "Resolved"], index=["Pending", "In Progress", "Resolved"].index(selected_row[4]))new_response = st.text_area("📝 الرد على الشكوى", value=selected_row[5] or "")if st.button("💾 حفظ التعديلات"):c.execute("UPDATE complaints SET status=?, response=? WHERE complaint_id=?", (new_status, new_response.strip(), selected_row[1]))1 unmodified linest.success("✅ تم حفظ التعديلات")st.rerun()# ⛔ زر تسجيل الخروج يظهر دائمًا لأي مستخدم مسجلif st.session_state.get("is_logged_in"):st.markdown("---")if st.button("🚪 تسجيل الخروج"):2 unmodified linesst.session_state.username = ""st.rerun()# 📥 زر لتحميل قاعدة البيانات (لـ admin أو أي مستخدم)if os.path.exists("complaints_system.db"):with open("complaints_system.db", "rb") as db_file:st.download_button(label="📥 تحميل complaints_system.db",data=db_file,1 unmodified linemime="application/octet-stream")else:st.warning("⚠️ ملف قاعدة البيانات غير موجود في هذا المسار.")2 unmodified lines345678910111213141516178 unmodified lines2627282930313233343536373839404142434445465 unmodified lines525354555657585960613 unmodified lines656667686970712 unmodified lines74757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531 unmodified line1551561571581591601612 unmodified lines1641651661671681691701711721 unmodified line1741751761772 unmodified linesimport osimport sqlite3from datetime import datetimefrom passlib.hash import bcryptst.set_page_config(page_title="نظام إدارة الشكاوى", layout="centered")# الاتصال بقاعدة البياناتconn = sqlite3.connect("complaints_system (1).db", check_same_thread=False)c = conn.cursor()# إنشاء الجداولc.execute('''CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT,username TEXT UNIQUE,8 unmodified linescomplaint_text TEXT,status TEXT,response TEXT,timestamp TEXT,user_comment TEXT)''')conn.commit()# تهيئة الجلسةif "is_logged_in" not in st.session_state:st.session_state.is_logged_in = Falseif "user_type" not in st.session_state:st.session_state.user_type = Noneif "username" not in st.session_state:st.session_state.username = ""if "complaint_input" not in st.session_state:st.session_state.complaint_input = ""# تسجيل الدخولif not st.session_state.is_logged_in:5 unmodified linesc.execute("SELECT * FROM users WHERE username=?", (username,))user = c.fetchone()if user:if bcrypt.verify(password, user[2]):st.session_state.is_logged_in = Truest.session_state.username = usernamest.session_state.user_type = "sender" if user[3] == "sender" else "receiver"st.rerun()else:st.error("❌ كلمة المرور غير صحيحة.")3 unmodified lines# مقدم الشكوىelif st.session_state.user_type == "sender":st.title("📨 إرسال شكوى جديدة")st.session_state.complaint_input = st.text_area("📝 وصف الشكوى", value=st.session_state.complaint_input, key="complaint_input")if st.button("📤 إرسال الشكوى"):c.execute("SELECT * FROM complaints WHERE username=?", (st.session_state.username,))2 unmodified linesnext_id = len(user_complaints) + 1complaint_id = f"{prefix}_{next_id:07}"c.execute("SELECT * FROM complaints WHERE complaint_id=?", (complaint_id,))if c.fetchone():st.warning("⚠️ تم إرسال هذه الشكوى مسبقًا بناءً على المعرف!")elif st.session_state.complaint_input.strip() == "":st.warning("⚠️ الرجاء إدخال نص الشكوى.")else:timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")c.execute("INSERT INTO complaints (complaint_id, username, complaint_text, status, response, timestamp, user_comment) VALUES (?, ?, ?, ?, ?, ?, ?)",(complaint_id, st.session_state.username, st.session_state.complaint_input.strip(), "Pending", "", timestamp, ""))conn.commit()st.success("✅ تم تقديم الشكوى بنجاح")st.session_state.complaint_input = "" # مسح الصندوقst.rerun()st.subheader("📋 الشكاوى السابقة")c.execute("SELECT * FROM complaints WHERE username=? ORDER BY id DESC", (st.session_state.username,))complaints = c.fetchall()for row in complaints:comp_id, username, text, status, response, timestamp, user_comment = row[1:]with st.expander(f"📨 {comp_id} | الحالة: {status}"):st.write(f"📅 تم الإرسال في: {timestamp}")st.write(f"📄 الشكوى: {text}")if status.lower() == "pending":new_text = st.text_area("📝 تعديل الشكوى", value=text, key=f"edit_{comp_id}")if st.button("💾 حفظ التعديل", key=f"save_{comp_id}"):c.execute("UPDATE complaints SET complaint_text=? WHERE complaint_id=?", (new_text.strip(), comp_id))conn.commit()st.success("✅ تم تعديل الشكوى")st.rerun()else:if response:st.success(f"💬 الرد: {response}")if not user_comment:comment = st.text_area("✏️ تعليقك على الرد", key=f"comment_{comp_id}")if st.button("📩 إرسال التعليق", key=f"send_comment_{comp_id}"):c.execute("UPDATE complaints SET user_comment=? WHERE complaint_id=?", (comment.strip(), comp_id))conn.commit()st.success("✅ تم إرسال التعليق بنجاح")st.rerun()else:st.info(f"🗣️ تعليقك السابق: {user_comment}")# مستقبل الشكوى (الـ admin أو الموظف)elif st.session_state.user_type == "receiver":st.title("📬 إدارة الشكاوى")# تنبيه بوجود شكاوى جديدةc.execute("SELECT COUNT(*) FROM complaints WHERE status = 'Pending'")pending_count = c.fetchone()[0]if pending_count > 0:st.warning(f"🚨 يوجد {pending_count} شكوى جديدة لم يتم التعامل معها!")c.execute("SELECT * FROM complaints ORDER BY id DESC")complaints = c.fetchall()if not complaints:st.info("لا توجد شكاوى حالياً.")else:st.subheader("📋 جميع الشكاوى")options = {f"{row[1]} - {row[2]}": row for row in complaints}selected = st.selectbox("اختر شكوى للتعامل معها", list(options.keys()))selected_row = options[selected]st.subheader(f"📨 معرف الشكوى: {selected_row[1]}")st.write(f"👤 مقدم الشكوى: {selected_row[2]}")st.write(f"📝 الشكوى: {selected_row[3]}")st.write(f"📅 الوقت: {selected_row[6]}")if selected_row[7]:st.info(f"🗣️ تعليق المستخدم على الرد: {selected_row[7]}")new_status = st.selectbox("🔄 تحديث الحالة", ["Pending", "In Progress", "Resolved"], index=["Pending", "In Progress", "Resolved"].index(selected_row[4]))new_response = st.text_area("💬 الرد على الشكوى", value=selected_row[5] or "")if st.button("💾 حفظ التعديلات"):c.execute("UPDATE complaints SET status=?, response=? WHERE complaint_id=?", (new_status, new_response.strip(), selected_row[1]))1 unmodified linest.success("✅ تم حفظ التعديلات")st.rerun()# زر تسجيل الخروج دائمif st.session_state.get("is_logged_in"):st.markdown("---")if st.button("🚪 تسجيل الخروج"):2 unmodified linesst.session_state.username = ""st.rerun()# زر لتحميل قاعدة البياناتif os.path.exists("complaints_system (1).db"):with open("complaints_system (1).db", "rb") as db_file:st.download_button(label="📥 تحميل complaints_system.db",data=db_file,1 unmodified linemime="application/octet-stream")else:st.warning("⚠️ ملف قاعدة البيانات غير موجود.")
app.py+55-32