Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
-
|
| 2 |
import streamlit as st
|
| 3 |
import sqlite3
|
| 4 |
from datetime import datetime
|
| 5 |
from passlib.hash import bcrypt
|
| 6 |
import os
|
|
|
|
| 7 |
|
| 8 |
# تهيئة الصفحة
|
| 9 |
st.set_page_config(page_title="نظام إدارة الشكاوى", layout="centered")
|
|
@@ -33,27 +33,13 @@ def init_db():
|
|
| 33 |
complaint_text TEXT,
|
| 34 |
status TEXT,
|
| 35 |
response TEXT,
|
| 36 |
-
timestamp TEXT
|
|
|
|
| 37 |
)
|
| 38 |
""")
|
| 39 |
conn.commit()
|
| 40 |
conn.close()
|
| 41 |
|
| 42 |
-
# التأكد من وجود عمود sender_comment
|
| 43 |
-
def ensure_sender_comment_column():
|
| 44 |
-
conn = get_db_connection()
|
| 45 |
-
cursor = conn.cursor()
|
| 46 |
-
cursor.execute("PRAGMA table_info(complaints)")
|
| 47 |
-
columns = [col[1] for col in cursor.fetchall()]
|
| 48 |
-
if "sender_comment" not in columns:
|
| 49 |
-
cursor.execute("ALTER TABLE complaints ADD COLUMN sender_comment TEXT")
|
| 50 |
-
conn.commit()
|
| 51 |
-
conn.close()
|
| 52 |
-
|
| 53 |
-
# استدعاء التهيئة
|
| 54 |
-
init_db()
|
| 55 |
-
ensure_sender_comment_column()
|
| 56 |
-
|
| 57 |
# إدارة حالة الجلسة
|
| 58 |
def init_session():
|
| 59 |
if "auth" not in st.session_state:
|
|
@@ -148,79 +134,116 @@ def sender_page():
|
|
| 148 |
|
| 149 |
conn.close()
|
| 150 |
|
| 151 |
-
# صفحة مستقبل الشكاوى
|
| 152 |
def receiver_page():
|
| 153 |
st.title("📬 إدارة الشكاوى")
|
| 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 |
-
conn.close()
|
| 221 |
|
| 222 |
# الواجهة الرئيسية
|
| 223 |
def main():
|
|
|
|
|
|
|
| 224 |
if not st.session_state.auth["is_logged_in"]:
|
| 225 |
login_page()
|
| 226 |
else:
|
|
@@ -238,14 +261,5 @@ def main():
|
|
| 238 |
}
|
| 239 |
st.rerun()
|
| 240 |
|
| 241 |
-
if os.path.exists("complaintsBD.db"):
|
| 242 |
-
with open("complaintsBD.db", "rb") as db_file:
|
| 243 |
-
st.download_button(
|
| 244 |
-
label="📥 تحميل قاعدة البيانات",
|
| 245 |
-
data=db_file,
|
| 246 |
-
file_name="complaintsBD.db",
|
| 247 |
-
mime="application/octet-stream"
|
| 248 |
-
)
|
| 249 |
-
|
| 250 |
if __name__ == "__main__":
|
| 251 |
-
main()
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import sqlite3
|
| 3 |
from datetime import datetime
|
| 4 |
from passlib.hash import bcrypt
|
| 5 |
import os
|
| 6 |
+
import shutil
|
| 7 |
|
| 8 |
# تهيئة الصفحة
|
| 9 |
st.set_page_config(page_title="نظام إدارة الشكاوى", layout="centered")
|
|
|
|
| 33 |
complaint_text TEXT,
|
| 34 |
status TEXT,
|
| 35 |
response TEXT,
|
| 36 |
+
timestamp TEXT,
|
| 37 |
+
sender_comment TEXT
|
| 38 |
)
|
| 39 |
""")
|
| 40 |
conn.commit()
|
| 41 |
conn.close()
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
# إدارة حالة الجلسة
|
| 44 |
def init_session():
|
| 45 |
if "auth" not in st.session_state:
|
|
|
|
| 134 |
|
| 135 |
conn.close()
|
| 136 |
|
| 137 |
+
# صفحة مستقبل الشكاوى مع لوحة التحكم الإدارية المحمية
|
| 138 |
def receiver_page():
|
| 139 |
st.title("📬 إدارة الشكاوى")
|
| 140 |
+
|
| 141 |
+
# تبويب للاختيار بين إدارة الشكاوى والتحكم الإداري
|
| 142 |
+
tab1, tab2 = st.tabs(["إدارة الشكاوى", "لوحة التحكم الإدارية"])
|
| 143 |
+
|
| 144 |
+
with tab1:
|
| 145 |
+
conn = get_db_connection()
|
| 146 |
+
try:
|
| 147 |
+
pending_count = conn.execute(
|
| 148 |
+
"SELECT COUNT(*) FROM complaints WHERE status = 'Pending'"
|
| 149 |
+
).fetchone()[0]
|
| 150 |
+
|
| 151 |
+
if pending_count > 0:
|
| 152 |
+
st.warning(f"🚨 يوجد {pending_count} شكوى جديدة لم يتم التعامل معها!")
|
| 153 |
+
|
| 154 |
+
complaints = conn.execute(
|
| 155 |
+
"SELECT * FROM complaints ORDER BY id DESC"
|
| 156 |
+
).fetchall()
|
| 157 |
+
|
| 158 |
+
complaints_list = [
|
| 159 |
+
{
|
| 160 |
+
"complaint_id": row["complaint_id"],
|
| 161 |
+
"username": row["username"],
|
| 162 |
+
"complaint_text": row["complaint_text"],
|
| 163 |
+
"status": row["status"],
|
| 164 |
+
"response": row["response"] or "",
|
| 165 |
+
"timestamp": row["timestamp"]
|
| 166 |
+
}
|
| 167 |
+
for row in complaints
|
| 168 |
+
]
|
| 169 |
+
|
| 170 |
+
if not complaints_list:
|
| 171 |
+
st.info("لا توجد شكاوى حالياً.")
|
| 172 |
+
else:
|
| 173 |
+
st.subheader("📋 جميع الشكاوى")
|
| 174 |
+
|
| 175 |
+
selected_complaint = st.selectbox(
|
| 176 |
+
"اختر شكوى للتعامل معها",
|
| 177 |
+
complaints_list,
|
| 178 |
+
format_func=lambda x: f"{x['complaint_id']} - {x['username']}"
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
st.subheader(f"📨 معرف الشكوى: {selected_complaint['complaint_id']}")
|
| 182 |
+
st.write(f"👤 مقدم الشكوى: {selected_complaint['username']}")
|
| 183 |
+
st.write(f"📝 نص الشكوى: {selected_complaint['complaint_text']}")
|
| 184 |
+
st.write(f"📅 وقت الإرسال: {selected_complaint['timestamp']}")
|
| 185 |
+
|
| 186 |
+
new_status = st.selectbox(
|
| 187 |
+
"🔄 تحديث الحالة",
|
| 188 |
+
["Pending", "In Progress", "Resolved"],
|
| 189 |
+
index=["Pending", "In Progress", "Resolved"].index(selected_complaint["status"])
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
new_response = st.text_area(
|
| 193 |
+
"💬 الرد على الشكوى",
|
| 194 |
+
value=selected_complaint["response"]
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
if st.button("💾 حفظ التعديلات"):
|
| 198 |
+
try:
|
| 199 |
+
conn.execute(
|
| 200 |
+
"UPDATE complaints SET status = ?, response = ? WHERE complaint_id = ?",
|
| 201 |
+
(new_status, new_response.strip(), selected_complaint["complaint_id"])
|
| 202 |
+
)
|
| 203 |
+
conn.commit()
|
| 204 |
+
st.success("✅ تم حفظ التعديلات بنجاح")
|
| 205 |
+
st.rerun()
|
| 206 |
+
except sqlite3.Error as e:
|
| 207 |
+
st.error(f"❌ حدث خطأ أثناء الحفظ: {str(e)}")
|
| 208 |
+
except sqlite3.Error as e:
|
| 209 |
+
st.error(f"❌ حدث خطأ في قاعدة البيانات: {str(e)}")
|
| 210 |
+
finally:
|
| 211 |
+
conn.close()
|
| 212 |
+
|
| 213 |
+
with tab2:
|
| 214 |
+
st.title("⚙️ لوحة التحكم الإدارية")
|
| 215 |
+
|
| 216 |
+
# كلمة المرور الإدارية (يجب تغييرها في البيئة الإنتاجية)
|
| 217 |
+
ADMIN_DB_PASSWORD = "Admin@1234"
|
| 218 |
+
|
| 219 |
+
# حقل لإدخال كلمة المرور الإدارية
|
| 220 |
+
admin_pass = st.text_input("أدخل كلمة المرور الإدارية:", type="password", key="admin_pass")
|
| 221 |
+
|
| 222 |
+
if admin_pass == ADMIN_DB_PASSWORD:
|
| 223 |
+
st.success("✅ تم المصادقة بنجاح")
|
| 224 |
+
|
| 225 |
+
if os.path.exists("complaintsBD.db"):
|
| 226 |
+
with open("complaintsBD.db", "rb") as db_file:
|
| 227 |
+
st.download_button(
|
| 228 |
+
label="📥 تحميل نسخة احتياطية من قاعدة البيانات",
|
| 229 |
+
data=db_file,
|
| 230 |
+
file_name=f"complaints_backup_{datetime.now().strftime('%Y%m%d')}.db",
|
| 231 |
+
mime="application/octet-stream",
|
| 232 |
+
key="db_download"
|
| 233 |
)
|
| 234 |
+
|
| 235 |
+
if st.button("🔄 إنشاء نسخة احتياطية جديدة", key="create_backup"):
|
| 236 |
+
backup_name = f"backup_{datetime.now().strftime('%Y%m%d_%H%M')}.db"
|
| 237 |
+
shutil.copy2("complaintsBD.db", backup_name)
|
| 238 |
+
st.success(f"تم إنشاء النسخة الاحتياطية: {backup_name}")
|
| 239 |
+
|
| 240 |
+
elif admin_pass: # إذا أدخل كلمة مرور ولكنها خاطئة
|
| 241 |
+
st.error("❌ كلمة المرور الإدارية غير صحيحة")
|
|
|
|
| 242 |
|
| 243 |
# الواجهة الرئيسية
|
| 244 |
def main():
|
| 245 |
+
init_db() # التأكد من وجود الجداول
|
| 246 |
+
|
| 247 |
if not st.session_state.auth["is_logged_in"]:
|
| 248 |
login_page()
|
| 249 |
else:
|
|
|
|
| 261 |
}
|
| 262 |
st.rerun()
|
| 263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
if __name__ == "__main__":
|
| 265 |
+
main()
|